From d7873e2b4e62273b00e47222158c3e3c9826dc14 Mon Sep 17 00:00:00 2001 From: Peter Braden Date: Tue, 24 Jan 2012 19:36:46 -0800 Subject: [PATCH] row, col, eye --- smoke.sh | 2 +- src/Matrix.cc | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- src/Matrix.h | 7 +++++-- test/smoke.js | 19 ++++++++++++++++++- 4 files changed, 73 insertions(+), 5 deletions(-) diff --git a/smoke.sh b/smoke.sh index 928a220..b8ae04c 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 -v +' && npm test diff --git a/src/Matrix.cc b/src/Matrix.cc index c0579cd..d3bfdc4 100644 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -21,6 +21,10 @@ Matrix::Init(Handle target) { Local proto = constructor->PrototypeTemplate(); + NODE_SET_PROTOTYPE_METHOD(constructor, "row", Row); + NODE_SET_PROTOTYPE_METHOD(constructor, "col", Col); + + NODE_SET_PROTOTYPE_METHOD(constructor, "empty", Empty); NODE_SET_PROTOTYPE_METHOD(constructor, "get", Get); NODE_SET_PROTOTYPE_METHOD(constructor, "set", Set); @@ -31,6 +35,9 @@ Matrix::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor, "ellipse", Ellipse); NODE_SET_PROTOTYPE_METHOD(constructor, "save", Save); + NODE_SET_METHOD(constructor, "Eye", Eye); + + target->Set(String::NewSymbol("Matrix"), m->GetFunction()); }; @@ -58,7 +65,7 @@ Matrix::Matrix(): ObjectWrap() { } Matrix::Matrix(int w, int h): ObjectWrap() { - mat = cv::Mat(w, h, CV_8UC1); + mat = cv::Mat(w, h, CV_64FC1); } Handle @@ -104,6 +111,33 @@ Matrix::Size(const Arguments& args){ return scope.Close(arr); } +Handle +Matrix::Row(const Arguments& args){ + SETUP_FUNCTION(Matrix) + + int width = self->mat.size().width; + int j = args[0]->IntegerValue(); + v8::Local arr = v8::Array::New(width); + + for (int i=0; iSet(i, Number::New(self->mat.at(i, j))); + } + return scope.Close(arr); +} + +Handle +Matrix::Col(const Arguments& args){ + SETUP_FUNCTION(Matrix) + + int width = self->mat.size().width; + int j = args[0]->IntegerValue(); + v8::Local arr = v8::Array::New(width); + + for (int i=0; iSet(i, Number::New(self->mat.at(j, i))); + } + return scope.Close(arr); +} Handle Matrix::Width(const Arguments& args){ @@ -171,3 +205,17 @@ Matrix::Save(const v8::Arguments& args){ return scope.Close(Number::New(res)); } +Handle +Matrix::Eye(const v8::Arguments& args){ + HandleScope scope; + + int w = args[0]->Uint32Value(); + int h = args[1]->Uint32Value(); + + Local im_h = Matrix::constructor->GetFunction()->NewInstance(); + Matrix *img = ObjectWrap::Unwrap(im_h); + cv::Mat mat = cv::Mat::eye(w, h, CV_64FC1); + + img->mat = mat; + return scope.Close(im_h); +} diff --git a/src/Matrix.h b/src/Matrix.h index 0e5e0a1..cf715c0 100644 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -12,12 +12,15 @@ class Matrix: public node::ObjectWrap { Matrix(int rows, int cols, int typ); + JSFUNC(Row) + JSFUNC(Col) + JSFUNC(Eye) // factory + /* 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); diff --git a/test/smoke.js b/test/smoke.js index 520aff8..ab3d76b 100644 --- a/test/smoke.js +++ b/test/smoke.js @@ -2,7 +2,12 @@ var vows = require('vows') , assert = require('assert') , fs = require('fs'); - +assertDeepSimilar = function(res, exp){ + for (var i = 0; i < res.length; i++){ + // res[i] = Math.round(res[i]/100)*100; + } + assert.deepEqual(res, exp) +} vows.describe('Smoke Tests OpenCV').addBatch({ "Importing": { @@ -88,6 +93,18 @@ vows.describe('Smoke Tests OpenCV').addBatch({ assert.deepEqual(mat.size(), [6, 7]); } + , 'row' : function(cv){ + var mat = new cv.Matrix.Eye(4,4) + assertDeepSimilar(mat.row(1), [0,1,0,0]) + assertDeepSimilar(mat.row(2), [0,0,1,0]) + } + + , 'col' : function(cv){ + var mat = new cv.Matrix.Eye(4,4); + assertDeepSimilar(mat.col(1), [0,1,0,0]) + assertDeepSimilar(mat.col(2), [0,0,1,0]) + } + , "empty": function(cv){ assert.equal(new cv.Matrix().empty(), true); }