diff --git a/TODO b/TODO index d136f85..b513d3e 100644 --- a/TODO +++ b/TODO @@ -1,17 +1,32 @@ -[X] In C++ get Image to inherit Matrix, to mirror js inherit - - Don't think it's possible :( +Get rid of Image + - Replace with operations on Matrix and cv helper js functions -- CascadeClassifier::DetectMultiScale - - pass in properties - - Make async + - Matrix doesn't need to be eventemitter: + + instead create a cv.MatrixBuffer class that is an event emitter, and wraps matrix in js -== Image == -- make image from stream -- make image from node file object + ############ Ideas ##################### -- Image::Ellipse - - pass in color - - pass in thickness \ No newline at end of file + var mat = cv.readImage('foo.jpg'); + + //shortcut for cascade classifier + mat.detectFaces('data.xml', function(err, faces){ + for (var i=0;i target) { // Prototype Local proto = constructor->PrototypeTemplate(); - - - proto->SetAccessor(String::NewSymbol("width"), GetWidth); - proto->SetAccessor(String::NewSymbol("height"), GetHeight); NODE_SET_PROTOTYPE_METHOD(constructor, "save", Save); NODE_SET_PROTOTYPE_METHOD(constructor, "ellipse", Ellipse); @@ -92,19 +88,6 @@ Image::Image(v8::Value* fileName): ObjectWrap() { Image::~Image() { } -Handle -Image::GetWidth(Local, const AccessorInfo &info) { - HandleScope scope; - Image *img = ObjectWrap::Unwrap(info.This()); - return scope.Close(Number::New(img->mat.size().width)); -} - -Handle -Image::GetHeight(Local, const AccessorInfo &info) { - HandleScope scope; - Image *img = ObjectWrap::Unwrap(info.This()); - return scope.Close(Number::New(img->mat.size().height)); -} Handle @@ -127,8 +110,8 @@ Image::ToBuffer(const v8::Arguments& args){ Image *self = ObjectWrap::Unwrap(args.This()); - vector vec(0); - vector params(0);//CV_IMWRITE_JPEG_QUALITY 90 + std::vector vec(0); + std::vector params(0);//CV_IMWRITE_JPEG_QUALITY 90 cv::imencode(".jpg", self->mat, vec, params); diff --git a/src/Image.h b/src/Image.h index 7753fb1..ec96647 100644 --- a/src/Image.h +++ b/src/Image.h @@ -12,8 +12,7 @@ class Image: public node::ObjectWrap { Image(v8::Value* src); Image(uint8_t* buf, unsigned len); - static Handle GetWidth(Local prop, const AccessorInfo &info); - static Handle GetHeight(Local prop, const AccessorInfo &info); + //static Handle Convert(const v8::Arguments&); static Handle Save(const v8::Arguments&); diff --git a/src/Matrix.cc b/src/Matrix.cc index 7f75f24..bb75797 100644 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -15,13 +15,18 @@ Matrix::Init(Handle target) { // Constructor constructor = Persistent::New(m); constructor->InstanceTemplate()->SetInternalFieldCount(1); - //constructor->SetClassName(String::NewSymbol("Matrix")); + constructor->SetClassName(String::NewSymbol("Matrix")); // Prototype Local proto = constructor->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(constructor, "empty", Empty); + NODE_SET_PROTOTYPE_METHOD(constructor, "get", Get); + NODE_SET_PROTOTYPE_METHOD(constructor, "set", Set); + NODE_SET_PROTOTYPE_METHOD(constructor, "width", Width); + NODE_SET_PROTOTYPE_METHOD(constructor, "height", Height); + NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size); target->Set(String::NewSymbol("Matrix"), m->GetFunction()); }; @@ -34,8 +39,13 @@ Matrix::New(const Arguments &args) { if (args.This()->InternalFieldCount() == 0) return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot instantiate without new"))); + Matrix *mat; - Matrix *mat = new Matrix; + if (args.Length() == 0){ + mat = new Matrix; + } else if (args.Length() == 2 && args[0]->IsInt32() && args[1]->IsInt32()){ + mat = new Matrix(args[0]->IntegerValue(), args[1]->IntegerValue()); + } mat->Wrap(args.Holder()); return scope.Close(args.Holder()); } @@ -44,6 +54,10 @@ Matrix::Matrix(): ObjectWrap() { mat = cv::Mat(); } +Matrix::Matrix(int w, int h): ObjectWrap() { + mat = cv::Mat(w, h, CV_8UC1); +} + Handle Matrix::Empty(const Arguments& args){ HandleScope scope; @@ -51,3 +65,60 @@ Matrix::Empty(const Arguments& args){ Matrix *self = ObjectWrap::Unwrap(args.This()); return scope.Close(Boolean::New(self->mat.empty())); } + +Handle +Matrix::Get(const Arguments& args){ + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + int i = args[0]->IntegerValue(); + int j = args[1]->IntegerValue(); + + return scope.Close(Number::New(self->mat.at(i,j))); +} + + +Handle +Matrix::Set(const Arguments& args){ + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + int i = args[0]->IntegerValue(); + int j = args[1]->IntegerValue(); + double val = args[2]->NumberValue(); + + self->mat.at(i,j) = val; + + return scope.Close(Undefined()); +} + + +Handle +Matrix::Size(const Arguments& args){ + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + v8::Local arr = v8::Array::New(2); + arr->Set(0, Number::New(self->mat.size().height)); + arr->Set(1, Number::New(self->mat.size().width)); + + return scope.Close(arr); +} + + +Handle +Matrix::Width(const Arguments& args){ + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + return scope.Close(Number::New(self->mat.size().width)); +} + +Handle +Matrix::Height(const Arguments& args){ + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + return scope.Close(Number::New(self->mat.size().height)); +} + diff --git a/src/Matrix.h b/src/Matrix.h index e0c8bce..b6520fa 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -8,8 +8,58 @@ class Matrix: public node::ObjectWrap { static void Init(Handle target); static Handle New(const Arguments &args); Matrix(); + Matrix(int rows, int cols); Matrix(int rows, int cols, int typ); + +/* + static Handle Val(const Arguments& args); + + + static Handle Row(const Arguments& args); + static Handle Col(const Arguments& args); + static Handle RowRange(const Arguments& args); + static Handle ColRange(const Arguments& args); + static Handle Diag(const Arguments& args); + static Handle Clone(const Arguments& args); + static Handle CopyTo(const Arguments& args); + static Handle ConvertTo(const Arguments& args); + static Handle AssignTo(const Arguments& args); + static Handle SetTo(const Arguments& args); + static Handle Reshape(const Arguments& args); + static Handle Transpose(const Arguments& args); + static Handle Invert(const Arguments& args); + static Handle Multiply(const Arguments& args); + static Handle Cross(const Arguments& args); + static Handle Dot(const Arguments& args); + static Handle Zeroes(const Arguments& args); + static Handle Ones(const Arguments& args); + static Handle Eye(const Arguments& args); +// create, increment, release + static Handle Resize(const Arguments& args); + static Handle PushBack(const Arguments& args); + static Handle PopBack(const Arguments& args); + static Handle LocateROI(const Arguments& args); + static Handle AdjustROI(const Arguments& args); + static Handle Total(const Arguments& args); + static Handle IsContinous(const Arguments& args); + static Handle Type(const Arguments& args); + static Handle Depth(const Arguments& args); + static Handle Channels(const Arguments& args); + static Handle StepOne(const Arguments& args); + + +*/ + + + static Handle Get(const Arguments& args); // at + static Handle Set(const Arguments& args); + + static Handle Size(const Arguments& args); + static Handle Width(const Arguments& args); + static Handle Height(const Arguments& args); + + static Handle Empty(const Arguments& args); diff --git a/test/smoke.js b/test/smoke.js index 134777e..500f956 100644 --- a/test/smoke.js +++ b/test/smoke.js @@ -66,15 +66,37 @@ vows.describe('Smoke Tests OpenCV').addBatch({ , "constructor" : function(cv){ assert.ok(cv.Matrix); assert.ok(new cv.Matrix); - assert.ok(new cv.Matrix(1,2,0)); + assert.ok(new cv.Matrix(1,2)); + } + , "get/set" : function(cv){ + var mat = new cv.Matrix(1,2); + assert.equal(mat.set(0,0,3), undefined); + assert.equal(mat.get(0,0), 3); + } + + , ".width" : function(cv){ + var mat = new cv.Matrix(6,7); + assert.equal(mat.width(), 7); + } + + , ".height" : function(cv){ + var mat = new cv.Matrix(6,7); + assert.equal(mat.height(), 6); + } + + , ".size" : function(cv){ + var mat = new cv.Matrix(6,7); + assert.deepEqual(mat.size(), [6, 7]); } , "empty": function(cv){ - assert.equal(new cv.Matrix(1,2).empty(), true); + assert.equal(new cv.Matrix().empty(), true); } } + + , "Image" : { topic : require('../lib/opencv') @@ -85,8 +107,8 @@ vows.describe('Smoke Tests OpenCV').addBatch({ , "constructor(buffer)" : function(cv){ var im = new cv.Image(fs.readFileSync('./examples/mona.jpg')) assert.ok(im); - assert.equal(im.width, 500); - assert.equal(im.height, 756) + assert.equal(im.width(), 500); + assert.equal(im.height(), 756) assert.equal(im.empty(), false) } @@ -96,8 +118,8 @@ vows.describe('Smoke Tests OpenCV').addBatch({ } , ".width / .height" : function(cv){ - assert.equal(new cv.Image("./examples/mona.jpg").width, 500) - assert.equal(new cv.Image("./examples/mona.jpg").height, 756) + assert.equal(new cv.Image("./examples/mona.jpg").width(), 500) + assert.equal(new cv.Image("./examples/mona.jpg").height(), 756) } , ".ellipse": function(cv){