From 569a1143cdf66361ca46a5bc8aa7525666ac2638 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Fri, 15 May 2015 10:43:33 +0200 Subject: [PATCH 01/10] added getData and normailze --- src/Matrix.cc | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ src/Matrix.h | 3 +++ 2 files changed, 51 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index ff8297f..144f4bc 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -28,6 +28,8 @@ Matrix::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(ctor, "get", Get); NODE_SET_PROTOTYPE_METHOD(ctor, "set", Set); NODE_SET_PROTOTYPE_METHOD(ctor, "put", Put); + NODE_SET_PROTOTYPE_METHOD(ctor, "normalize", Normalize); + NODE_SET_PROTOTYPE_METHOD(ctor, "getData", GetData); NODE_SET_PROTOTYPE_METHOD(ctor, "pixel", Pixel); NODE_SET_PROTOTYPE_METHOD(ctor, "width", Width); NODE_SET_PROTOTYPE_METHOD(ctor, "height", Height); @@ -287,6 +289,52 @@ NAN_METHOD(Matrix::Put){ NanReturnUndefined(); } + +// @author tualo +// getData getting node buffer of image data +NAN_METHOD(Matrix::GetData) { + NanScope(); + + Matrix *self = ObjectWrap::Unwrap(args.This()); + int size = self->mat.rows * self->mat.cols * self->mat.elemSize1(); + Local buf = NanNewBufferHandle(size); + uchar* data = (uchar*) Buffer::Data(buf); + memcpy(data, self->mat.data, size); + + v8::Local globalObj = NanGetCurrentContext()->Global(); + v8::Local bufferConstructor = v8::Local::Cast(globalObj->Get(NanNew("Buffer"))); + v8::Handle constructorArgs[3] = {buf, NanNew((unsigned) size), NanNew(0)}; + v8::Local actualBuffer = bufferConstructor->NewInstance(3, constructorArgs); + + NanReturnValue(actualBuffer); + +} + +// @author tualo +// normalize wrapper +NAN_METHOD(Matrix::Normalize) { + NanScope(); + + if (!args[0]->IsNumber()) + NanThrowTypeError("min is required (argument 1)"); + + if (!args[1]->IsNumber()) + NanThrowTypeError("max is required (argument 2)"); + + int min = args[0]->Uint32Value(); + int max = args[1]->Uint32Value(); + + Matrix *self = ObjectWrap::Unwrap(args.This()); + cv::Mat norm; + cv::normalize(self->mat, norm,min,max, cv::NORM_MINMAX); + + norm.copyTo(self->mat); + + NanReturnNull(); +} + + + NAN_METHOD(Matrix::Size){ SETUP_FUNCTION(Matrix) diff --git a/src/Matrix.h b/src/Matrix.h index 04cf1d6..37477e1 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -22,6 +22,9 @@ class Matrix: public node::ObjectWrap { JSFUNC(Set) JSFUNC(Put) + JSFUNC(GetData) + JSFUNC(Normalize) + JSFUNC(Row) JSFUNC(PixelRow) JSFUNC(Col) From 2704d607feaecc5006d65a9229e23ddbb7deb0fc Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Fri, 15 May 2015 11:34:11 +0200 Subject: [PATCH 02/10] added mask parameter --- examples/mat-normalize.js | 26 ++++++++++++++++++++++++++ src/Matrix.cc | 37 ++++++++++++++++++++++++++++++++++--- 2 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 examples/mat-normalize.js diff --git a/examples/mat-normalize.js b/examples/mat-normalize.js new file mode 100644 index 0000000..b7f6da3 --- /dev/null +++ b/examples/mat-normalize.js @@ -0,0 +1,26 @@ +var cv = require('../lib/opencv'); + + + + +cv.readImage("./examples/files/mona.png", function(err, orig) { + if (err) throw err; + orig.convertGrayscale(); + + var mask = new cv.Matrix(orig.height(),orig.width(),cv.Constants.CV_8UC1); + var buf = Buffer(orig.height()*orig.width()); + buf.fill(0); + for(var i=0;iIsNumber()) NanThrowTypeError("max is required (argument 2)"); - int min = args[0]->Uint32Value(); - int max = args[1]->Uint32Value(); + int type = cv::NORM_MINMAX; + if (args[2]->IsNumber()){ + type = args[2]->Uint32Value(); + if ( + (type!=cv::NORM_MINMAX) || + (type!=cv::NORM_INF) || + (type!=cv::NORM_L1) || + (type!=cv::NORM_L2) || + (type!=cv::NORM_L2SQR) || + (type!=cv::NORM_HAMMING) || + (type!=cv::NORM_HAMMING2) || + (type!=cv::NORM_RELATIVE) || + (type!=cv::NORM_TYPE_MASK) + ){ + NanThrowTypeError("type value must be NORM_INF=1, NORM_L1=2, NORM_L2=4, NORM_L2SQR=5, NORM_HAMMING=6, NORM_HAMMING2=7, NORM_TYPE_MASK=7, NORM_RELATIVE=8, NORM_MINMAX=32 "); + } + } + int dtype = -1; + if (args[3]->IsNumber()){ + dtype = args[3]->IntegerValue(); + } + + double min = args[0]->NumberValue(); + double max = args[1]->NumberValue(); + Matrix *self = ObjectWrap::Unwrap(args.This()); cv::Mat norm; - cv::normalize(self->mat, norm,min,max, cv::NORM_MINMAX); + + cv::Mat mask; + if (args[4]->IsObject()){ + Matrix *mmask = ObjectWrap::Unwrap( args[4]->ToObject() ); + mask = mmask->mat; + } + + + cv::normalize(self->mat, norm,min,max, type, dtype, mask); norm.copyTo(self->mat); From f91caf8c69eee5d10875f7eed00420114d9c0fa9 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Fri, 15 May 2015 11:34:42 +0200 Subject: [PATCH 03/10] added normalize constants --- src/Constants.cc | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Constants.cc b/src/Constants.cc index 1d9696b..bf81507 100644 --- a/src/Constants.cc +++ b/src/Constants.cc @@ -63,6 +63,16 @@ Constants::Init(Handle target) { CONST_ENUM(INTER_CUBIC); CONST_ENUM(INTER_LANCZOS4); + CONST(cv::NORM_MINMAX); + CONST(cv::NORM_INF); + CONST(cv::NORM_L1); + CONST(cv::NORM_L2); + CONST(cv::NORM_L2SQR); + CONST(cv::NORM_HAMMING); + CONST(cv::NORM_HAMMING2); + CONST(cv::NORM_RELATIVE); + CONST(cv::NORM_TYPE_MASK); + target->Set(NanNew("Constants"), obj); } From 51d0c8db023219a3d84b22157f7075e510c1dcd3 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Fri, 15 May 2015 12:18:47 +0200 Subject: [PATCH 04/10] fixed enum error --- src/Constants.cc | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/Constants.cc b/src/Constants.cc index bf81507..523b96e 100644 --- a/src/Constants.cc +++ b/src/Constants.cc @@ -63,15 +63,15 @@ Constants::Init(Handle target) { CONST_ENUM(INTER_CUBIC); CONST_ENUM(INTER_LANCZOS4); - CONST(cv::NORM_MINMAX); - CONST(cv::NORM_INF); - CONST(cv::NORM_L1); - CONST(cv::NORM_L2); - CONST(cv::NORM_L2SQR); - CONST(cv::NORM_HAMMING); - CONST(cv::NORM_HAMMING2); - CONST(cv::NORM_RELATIVE); - CONST(cv::NORM_TYPE_MASK); + CONST_ENUM(NORM_MINMAX); + CONST_ENUM(NORM_INF); + CONST_ENUM(NORM_L1); + CONST_ENUM(NORM_L2); + CONST_ENUM(NORM_L2SQR); + CONST_ENUM(NORM_HAMMING); + CONST_ENUM(NORM_HAMMING2); + CONST_ENUM(NORM_RELATIVE); + CONST_ENUM(NORM_TYPE_MASK); target->Set(NanNew("Constants"), obj); } From b71d5ab293c776af53853694039b6f65d917da54 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Thu, 4 Jun 2015 10:41:12 +0200 Subject: [PATCH 05/10] commented out --- examples/mat-normalize.js | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/examples/mat-normalize.js b/examples/mat-normalize.js index b7f6da3..f5cd1be 100644 --- a/examples/mat-normalize.js +++ b/examples/mat-normalize.js @@ -1,9 +1,6 @@ var cv = require('../lib/opencv'); - - - - cv.readImage("./examples/files/mona.png", function(err, orig) { + /* if (err) throw err; orig.convertGrayscale(); @@ -15,7 +12,7 @@ cv.readImage("./examples/files/mona.png", function(err, orig) { } mask.put(buf); orig.normalize(10,250,cv.Constants.NORM_TYPE_MASK,-1,mask); - /* + var window = new cv.NamedWindow('Mat-Normalize', 0); window.show(orig); window.blockingWaitKey(0, 50); From f9d5c8a4d7b664897a4946c28be57afa1756b0be Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Thu, 11 Jun 2015 12:23:54 +0200 Subject: [PATCH 06/10] added brightness --- src/Matrix.cc | 29 ++++++++++++++++++++++++++++- src/Matrix.h | 1 + 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index a76d1b3..21e2996 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -28,6 +28,7 @@ Matrix::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(ctor, "get", Get); NODE_SET_PROTOTYPE_METHOD(ctor, "set", Set); NODE_SET_PROTOTYPE_METHOD(ctor, "put", Put); + NODE_SET_PROTOTYPE_METHOD(ctor, "brightness", Brightness); NODE_SET_PROTOTYPE_METHOD(ctor, "normalize", Normalize); NODE_SET_PROTOTYPE_METHOD(ctor, "getData", GetData); NODE_SET_PROTOTYPE_METHOD(ctor, "pixel", Pixel); @@ -310,10 +311,36 @@ NAN_METHOD(Matrix::GetData) { } +NAN_METHOD(Matrix::Brightness){ + NanScope(); + + if (args.Length() == 2){ + Matrix *self = ObjectWrap::Unwrap(args.This()); + cv::Mat image = self->mat; + cv::Mat new_image = cv::Mat::zeros( image.size(), image.type() ); + double alpha = args[0]->NumberValue(); + int beta = args[1]->IntegerValue(); + /// Do the operation new_image(i,j) = alpha*image(i,j) + beta + for( int y = 0; y < image.rows; y++ ){ + for( int x = 0; x < image.cols; x++ ){ + for( int c = 0; c < 3; c++ ){ + new_image.at(y,x)[c] = cv::saturate_cast( alpha*( image.at(y,x)[c] ) + beta ); + } + } + } + new_image.copyTo(self->mat); + }else{ + NanReturnValue(NanNew("Insufficient or wrong arguments")); + } + + + NanReturnNull(); +} + // @author tualo // normalize wrapper NAN_METHOD(Matrix::Normalize) { - NanScope(); + if (!args[0]->IsNumber()) NanThrowTypeError("min is required (argument 1)"); diff --git a/src/Matrix.h b/src/Matrix.h index 37477e1..fe24ebc 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -24,6 +24,7 @@ class Matrix: public node::ObjectWrap { JSFUNC(GetData) JSFUNC(Normalize) + JSFUNC(Brightness) JSFUNC(Row) JSFUNC(PixelRow) From d5441b93ffdc2eb97baaca9ff88015de2a4f44b2 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Thu, 11 Jun 2015 12:59:00 +0200 Subject: [PATCH 07/10] fixed channel check --- src/Matrix.cc | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index 21e2996..221b834 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -316,6 +316,10 @@ NAN_METHOD(Matrix::Brightness){ if (args.Length() == 2){ Matrix *self = ObjectWrap::Unwrap(args.This()); + + if(self->mat.channels() != 3) + NanThrowError("Image is no 3-channel"); + cv::Mat image = self->mat; cv::Mat new_image = cv::Mat::zeros( image.size(), image.type() ); double alpha = args[0]->NumberValue(); From 71da46939b660aaec92facd6b3073c7f32ee2908 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Thu, 11 Jun 2015 13:40:04 +0200 Subject: [PATCH 08/10] added brightness support for gray images --- src/Matrix.cc | 23 +++++++++++++++++++---- 1 file changed, 19 insertions(+), 4 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 221b834..b4ffacf 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -316,11 +316,18 @@ NAN_METHOD(Matrix::Brightness){ if (args.Length() == 2){ Matrix *self = ObjectWrap::Unwrap(args.This()); + cv::Mat image; + + if(self->mat.channels() == 3){ + image = self->mat; + }else if(self->mat.channels() == 1){ + cv::Mat myimg = self->mat; + cv::cvtColor(myimg, image, CV_GRAY2RGB); + }else{ + NanThrowError("those channels are not supported"); + } - if(self->mat.channels() != 3) - NanThrowError("Image is no 3-channel"); - cv::Mat image = self->mat; cv::Mat new_image = cv::Mat::zeros( image.size(), image.type() ); double alpha = args[0]->NumberValue(); int beta = args[1]->IntegerValue(); @@ -332,7 +339,15 @@ NAN_METHOD(Matrix::Brightness){ } } } - new_image.copyTo(self->mat); + + if(self->mat.channels() == 3){ + new_image.copyTo(self->mat); + }else if(self->mat.channels() == 1){ + cv::Mat gray; + cv::cvtColor(new_image, gray, CV_BGR2GRAY); + gray.copyTo(self->mat); + } + }else{ NanReturnValue(NanNew("Insufficient or wrong arguments")); } From ef8090f4b64a8e60d0adaed2ff859651d0a6f693 Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Wed, 1 Jul 2015 14:56:26 +0200 Subject: [PATCH 09/10] added simple brightness --- src/Matrix.cc | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index b4ffacf..950ada8 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -313,9 +313,10 @@ NAN_METHOD(Matrix::GetData) { NAN_METHOD(Matrix::Brightness){ NanScope(); + Matrix *self = ObjectWrap::Unwrap(args.This()); if (args.Length() == 2){ - Matrix *self = ObjectWrap::Unwrap(args.This()); + cv::Mat image; if(self->mat.channels() == 3){ @@ -349,7 +350,13 @@ NAN_METHOD(Matrix::Brightness){ } }else{ - NanReturnValue(NanNew("Insufficient or wrong arguments")); + if (args.Length() == 1){ + int diff = args[0]->IntegerValue(); + cv::Mat img = self->mat + diff; + img.copyTo(self->mat); + }else{ + NanReturnValue(NanNew("Insufficient or wrong arguments")); + } } From da55bda0c9121aa26049e32d303cebf4ff8f1a2d Mon Sep 17 00:00:00 2001 From: Thomas Hoffmann Date: Tue, 7 Jul 2015 20:37:23 +0200 Subject: [PATCH 10/10] added missed BackgroundSubtractor including --- src/init.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/init.cc b/src/init.cc index 8d9171e..a8a86c5 100755 --- a/src/init.cc +++ b/src/init.cc @@ -13,6 +13,7 @@ #include "Calib3D.h" #include "ImgProc.h" #include "Stereo.h" +#include "BackgroundSubtractor.h" extern "C" void init(Handle target) { @@ -33,8 +34,8 @@ init(Handle target) { StereoSGBM::Init(target); StereoGC::Init(target); - #if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4 + BackgroundSubtractorWrap::Init(target); Features::Init(target); FaceRecognizerWrap::Init(target); #endif