diff --git a/README.md b/README.md index e55afed..5f654e9 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ ## Install +You'll need OpenCV installed. I'm using v2.2 because I couldn't get 2.3 to compile, but it should theoretically work with 2.3 + +Then: + node-waf configure && node-waf build + + TODO: npm install opencv diff --git a/lib/opencv.js b/lib/opencv.js index 5d3c668..0642b1a 100644 --- a/lib/opencv.js +++ b/lib/opencv.js @@ -1,5 +1,9 @@ var opencv = require('./bindings') , Point = opencv.Point , Image = opencv.Image + , Matrix = opencv.Matrix + + + var oc = exports = module.exports = opencv; \ No newline at end of file diff --git a/smoke.sh b/smoke.sh index 47839c1..928a220 100755 --- a/smoke.sh +++ b/smoke.sh @@ -3,4 +3,4 @@ node-waf configure && node-waf build && echo '-- Compiled OK -- ' && node smoketest.js && echo '-- Smoke Done, running tests -- -' && node test/smoke.js +' && node test/smoke.js -v diff --git a/smoketest.js b/smoketest.js index 3f71020..6cab6b1 100755 --- a/smoketest.js +++ b/smoketest.js @@ -3,5 +3,6 @@ var opencv = require('./lib/opencv') console.log(opencv.version) +console.log(new opencv.Matrix().empty()) -console.log(new opencv.Image('./examples/mona.jpg').save('t1.jpg')) \ No newline at end of file +console.log(new opencv.Image('./examples/mona.jpg').empty()) \ No newline at end of file diff --git a/src/CascadeClassifierWrap.cc b/src/CascadeClassifierWrap.cc new file mode 100644 index 0000000..815e4e4 --- /dev/null +++ b/src/CascadeClassifierWrap.cc @@ -0,0 +1,41 @@ +#include "OpenCV.h" +#include "CascadeClassifierWrap.h" + + +v8::Persistent CascadeClassifierWrap::constructor; + +void +CascadeClassifierWrap::Init(Handle target) { + HandleScope scope; + + // Constructor + constructor = Persistent::New(FunctionTemplate::New(CascadeClassifierWrap::New)); + constructor->InstanceTemplate()->SetInternalFieldCount(1); + constructor->SetClassName(String::NewSymbol("CascadeClassifier")); + + // Prototype + Local proto = constructor->PrototypeTemplate(); + + target->Set(String::NewSymbol("CascadeClassifier"), constructor->GetFunction()); +}; + +Handle +CascadeClassifierWrap::New(const Arguments &args) { + HandleScope scope; + + if (args.This()->InternalFieldCount() == 0) + return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot Instantiate without new"))); + + CascadeClassifierWrap *pt = new CascadeClassifierWrap(*args[0]); + pt->Wrap(args.This()); + return args.This(); +} + + +CascadeClassifierWrap::CascadeClassifierWrap(v8::Value* fileName){ + std::string filename; + filename = std::string(*v8::String::AsciiValue(fileName->ToString())); + + cc.load(filename.c_str()); + +} diff --git a/src/CascadeClassifierWrap.h b/src/CascadeClassifierWrap.h new file mode 100644 index 0000000..0d7f0ae --- /dev/null +++ b/src/CascadeClassifierWrap.h @@ -0,0 +1,15 @@ +#include "OpenCV.h" + +class CascadeClassifierWrap: public node::ObjectWrap { + public: + cv::CascadeClassifier cc; + + + static Persistent constructor; + static void Init(Handle target); + static Handle New(const Arguments &args); + + CascadeClassifierWrap(v8::Value* fileName); + + //static Handle LoadHaarClassifierCascade(const v8::Arguments&); +} \ No newline at end of file diff --git a/src/Image.cc b/src/Image.cc index 0615098..75d98a1 100644 --- a/src/Image.cc +++ b/src/Image.cc @@ -1,5 +1,6 @@ #include "Image.h" #include "OpenCV.h" +#include "Matrix.h" v8::Persistent Image::constructor; @@ -11,6 +12,7 @@ Image::Init(Handle target) { constructor = Persistent::New(FunctionTemplate::New(Image::New)); constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->SetClassName(String::NewSymbol("Image")); + constructor->Inherit(Matrix::constructor); // Prototype Local proto = constructor->PrototypeTemplate(); @@ -63,7 +65,7 @@ Image::Image(int width, int height): ObjectWrap() { Image::Image(v8::Value* fileName): ObjectWrap() { filename = std::string(*v8::String::AsciiValue(fileName->ToString())); - image = cv::imread(filename, -1); + mat = cv::imread(filename, -1); }; @@ -71,14 +73,14 @@ Handle Image::GetWidth(Local, const AccessorInfo &info) { HandleScope scope; Image *img = ObjectWrap::Unwrap(info.This()); - return scope.Close(Number::New(img->image.size().width)); + 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->image.size().height)); + return scope.Close(Number::New(img->mat.size().height)); } @@ -91,7 +93,7 @@ Image::Save(const v8::Arguments& args){ Image *self = ObjectWrap::Unwrap(args.This()); String::AsciiValue filename(args[0]); - int res = cv::imwrite(*filename, self->image); + int res = cv::imwrite(*filename, self->mat); return scope.Close(Number::New(res)); } diff --git a/src/Image.h b/src/Image.h index 8e05adf..292321d 100644 --- a/src/Image.h +++ b/src/Image.h @@ -3,7 +3,7 @@ class Image: public node::ObjectWrap { public: std::string filename; - cv::Mat image; + cv::Mat mat; static Persistent constructor; static void Init(Handle target); diff --git a/src/Matrix.cc b/src/Matrix.cc new file mode 100644 index 0000000..34fb11b --- /dev/null +++ b/src/Matrix.cc @@ -0,0 +1,49 @@ +#include "Matrix.h" +#include "OpenCV.h" + +v8::Persistent Matrix::constructor; + + +void +Matrix::Init(Handle target) { + HandleScope scope; + + // Constructor + constructor = Persistent::New(FunctionTemplate::New(Matrix::New)); + constructor->InstanceTemplate()->SetInternalFieldCount(1); + constructor->SetClassName(String::NewSymbol("Matrix")); + + // Prototype + Local proto = constructor->PrototypeTemplate(); + + + NODE_SET_PROTOTYPE_METHOD(constructor, "empty", Empty); + + target->Set(String::NewSymbol("Matrix"), constructor->GetFunction()); +}; + + +Handle +Matrix::New(const Arguments &args) { + HandleScope scope; + + if (args.This()->InternalFieldCount() == 0) + return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot instantiate without new"))); + + + Matrix *mat = new Matrix; + mat->Wrap(args.This()); + return args.This(); +} + +Matrix::Matrix(): ObjectWrap() { + mat = cv::Mat(); +} + +Handle +Matrix::Empty(const Arguments& args){ + HandleScope scope; + + Matrix *self = ObjectWrap::Unwrap(args.This()); + return scope.Close(Boolean::New(self->mat.empty())); +} diff --git a/src/Matrix.h b/src/Matrix.h new file mode 100644 index 0000000..e0c8bce --- /dev/null +++ b/src/Matrix.h @@ -0,0 +1,17 @@ +#include "OpenCV.h" + +class Matrix: public node::ObjectWrap { + public: + + cv::Mat mat; + static Persistent constructor; + static void Init(Handle target); + static Handle New(const Arguments &args); + Matrix(); + Matrix(int rows, int cols, int typ); + + static Handle Empty(const Arguments& args); + + +}; + diff --git a/src/OpenCV.cc b/src/OpenCV.cc index d9292dc..e43d58b 100644 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -9,4 +9,4 @@ OpenCV::Init(Handle target) { char out [21]; int n = sprintf(out, "%i.%i", CV_MAJOR_VERSION, CV_MINOR_VERSION); target->Set(String::NewSymbol("version"), String::New(out, n)); -} \ No newline at end of file +} diff --git a/src/init.cc b/src/init.cc index e0c7c38..e86e92d 100644 --- a/src/init.cc +++ b/src/init.cc @@ -1,11 +1,15 @@ #include "OpenCV.h" #include "Point.h" +#include "Matrix.h" #include "Image.h" +#include "CascadeClassifierWrap.h" extern "C" void init(Handle target) { HandleScope scope; OpenCV::Init(target); Point::Init(target); + Matrix::Init(target); Image::Init(target); + CascadeClassifierWrap::Init(target); } \ No newline at end of file diff --git a/test/smoke.js b/test/smoke.js index 4bdf8a9..e7a57cf 100644 --- a/test/smoke.js +++ b/test/smoke.js @@ -59,6 +59,21 @@ vows.describe('Smoke Tests OpenCV').addBatch({ } + , "Matrix": { + topic : require('../lib/opencv') + + , "constructor" : function(cv){ + assert.ok(cv.Matrix); + assert.ok(new cv.Matrix); + assert.ok(new cv.Matrix(1,2,0)); + } + + , "empty": function(cv){ + assert.equal(new cv.Matrix(1,2).empty(), true); + } + + } + , "Image" : { topic : require('../lib/opencv') @@ -66,6 +81,10 @@ vows.describe('Smoke Tests OpenCV').addBatch({ assert.ok(new cv.Image("./examples/mona.jpg")) } + , "inherits from matrix": function(cv){ + assert.equal(new cv.Image("./examples/mona.jpg").empty(), false) + } + , ".width / .height" : function(cv){ assert.equal(new cv.Image("./examples/mona.jpg").width, 500) assert.equal(new cv.Image("./examples/mona.jpg").height, 756)