diff --git a/examples/mat-normalize.js b/examples/mat-normalize.js new file mode 100644 index 0000000..f5cd1be --- /dev/null +++ b/examples/mat-normalize.js @@ -0,0 +1,23 @@ +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;i target) { CONST_ENUM(INTER_CUBIC); CONST_ENUM(INTER_LANCZOS4); + 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); } diff --git a/src/Matrix.cc b/src/Matrix.cc index 06ff203..9ce18f3 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -28,6 +28,9 @@ 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); NODE_SET_PROTOTYPE_METHOD(ctor, "width", Width); NODE_SET_PROTOTYPE_METHOD(ctor, "height", Height); @@ -287,6 +290,135 @@ 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); + +} + +NAN_METHOD(Matrix::Brightness){ + NanScope(); + Matrix *self = ObjectWrap::Unwrap(args.This()); + + if (args.Length() == 2){ + + 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"); + } + + + 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 ); + } + } + } + + 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{ + 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")); + } + } + + + NanReturnNull(); +} + +// @author tualo +// normalize wrapper +NAN_METHOD(Matrix::Normalize) { + + + if (!args[0]->IsNumber()) + NanThrowTypeError("min is required (argument 1)"); + + if (!args[1]->IsNumber()) + NanThrowTypeError("max is required (argument 2)"); + + 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::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); + + NanReturnNull(); +} + + + NAN_METHOD(Matrix::Size){ SETUP_FUNCTION(Matrix) diff --git a/src/Matrix.h b/src/Matrix.h index 04cf1d6..fe24ebc 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -22,6 +22,10 @@ class Matrix: public node::ObjectWrap { JSFUNC(Set) JSFUNC(Put) + JSFUNC(GetData) + JSFUNC(Normalize) + JSFUNC(Brightness) + JSFUNC(Row) JSFUNC(PixelRow) JSFUNC(Col) 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