added some functions: cvtColor, split, merge, equalizeHist

This commit is contained in:
Paul 2013-08-22 23:43:41 +04:00
parent 146925ee6d
commit 187b6d073a
2 changed files with 136 additions and 7 deletions

View File

@ -63,7 +63,7 @@ Matrix::Init(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor, "addWeighted", AddWeighted); NODE_SET_PROTOTYPE_METHOD(constructor, "addWeighted", AddWeighted);
NODE_SET_PROTOTYPE_METHOD(constructor, "bitwiseXor", BitwiseXor); NODE_SET_PROTOTYPE_METHOD(constructor, "bitwiseXor", BitwiseXor);
NODE_SET_PROTOTYPE_METHOD(constructor, "countNonZero", CountNonZero); NODE_SET_PROTOTYPE_METHOD(constructor, "countNonZero", CountNonZero);
NODE_SET_PROTOTYPE_METHOD(constructor, "split", Split); //NODE_SET_PROTOTYPE_METHOD(constructor, "split", Split);
NODE_SET_PROTOTYPE_METHOD(constructor, "canny", Canny); NODE_SET_PROTOTYPE_METHOD(constructor, "canny", Canny);
NODE_SET_PROTOTYPE_METHOD(constructor, "dilate", Dilate); NODE_SET_PROTOTYPE_METHOD(constructor, "dilate", Dilate);
NODE_SET_PROTOTYPE_METHOD(constructor, "erode", Erode); NODE_SET_PROTOTYPE_METHOD(constructor, "erode", Erode);
@ -82,6 +82,11 @@ Matrix::Init(Handle<Object> target) {
NODE_SET_PROTOTYPE_METHOD(constructor, "threshold", Threshold); NODE_SET_PROTOTYPE_METHOD(constructor, "threshold", Threshold);
NODE_SET_PROTOTYPE_METHOD(constructor, "meanStdDev", MeanStdDev); NODE_SET_PROTOTYPE_METHOD(constructor, "meanStdDev", MeanStdDev);
NODE_SET_PROTOTYPE_METHOD(constructor, "cvtColor", CvtColor);
NODE_SET_PROTOTYPE_METHOD(constructor, "split", Split);
NODE_SET_PROTOTYPE_METHOD(constructor, "merge", Merge);
NODE_SET_PROTOTYPE_METHOD(constructor, "equalizeHist", EqualizeHist);
NODE_SET_METHOD(constructor, "Eye", Eye); NODE_SET_METHOD(constructor, "Eye", Eye);
@ -129,6 +134,10 @@ Matrix::Matrix(cv::Mat m, cv::Rect roi): ObjectWrap() {
mat = cv::Mat(m, roi); mat = cv::Mat(m, roi);
} }
Matrix::Matrix(cv::Mat m): ObjectWrap() {
mat = cv::Mat(m);
}
Handle<Value> Handle<Value>
Matrix::Empty(const Arguments& args){ Matrix::Empty(const Arguments& args){
SETUP_FUNCTION(Matrix) SETUP_FUNCTION(Matrix)
@ -861,14 +870,14 @@ Matrix::CountNonZero(const v8::Arguments& args) {
return scope.Close(v8::Number::New(count)); return scope.Close(v8::Number::New(count));
} }
Handle<Value> /*Handle<Value>
Matrix::Split(const v8::Arguments& args) { Matrix::Split(const v8::Arguments& args) {
HandleScope scope; HandleScope scope;
//Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This()); //Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
return scope.Close(v8::Null()); return scope.Close(v8::Null());
} }*/
Handle<Value> Handle<Value>
@ -1049,7 +1058,7 @@ Matrix::Resize(const v8::Arguments& args){
CV_INTER_AREA =3, CV_INTER_AREA =3,
CV_INTER_LANCZOS4 =4 CV_INTER_LANCZOS4 =4
*/ */
int interpolation = (args.Length() < 3) ? cv::INTER_LINEAR : args[2]->Uint32Value(); int interpolation = (args.Length() < 3) ? (int)cv::INTER_LINEAR : args[2]->Uint32Value();
Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This()); Matrix *self = ObjectWrap::Unwrap<Matrix>(args.This());
cv::Mat res = cv::Mat(x, y, CV_32FC3); cv::Mat res = cv::Mat(x, y, CV_32FC3);
@ -1257,7 +1266,7 @@ Matrix::MeanStdDev(const v8::Arguments& args) {
// our.width + x <= destination.width (and the same for y and height) // our.width + x <= destination.width (and the same for y and height)
// both x and y must be >= 0 // both x and y must be >= 0
Handle<Value> Handle<Value>
Matrix::CopyTo(const v8::Arguments& args){ Matrix::CopyTo(const v8::Arguments& args) {
HandleScope scope; HandleScope scope;
Matrix * self = ObjectWrap::Unwrap<Matrix>(args.This()); Matrix * self = ObjectWrap::Unwrap<Matrix>(args.This());
@ -1276,3 +1285,119 @@ Matrix::CopyTo(const v8::Arguments& args){
return scope.Close(Undefined()); return scope.Close(Undefined());
} }
// @author SergeMv
// Does in-place color transformation
// img.cvtColor('CV_BGR2YCrCb');
Handle<Value>
Matrix::CvtColor(const v8::Arguments& args) {
HandleScope scope;
Matrix * self = ObjectWrap::Unwrap<Matrix>(args.This());
v8::String::Utf8Value str (args[0]->ToString());
std::string str2 = std::string(*str);
const char * sTransform = (const char *) str2.c_str();
int iTransform;
//
if (!strcmp(sTransform, "CV_BGR2GRAY")) { iTransform = CV_BGR2GRAY; }
else if (!strcmp(sTransform, "CV_GRAY2BGR")) { iTransform = CV_GRAY2BGR; }
//
else if (!strcmp(sTransform, "CV_BGR2XYZ")) { iTransform = CV_BGR2XYZ; }
else if (!strcmp(sTransform, "CV_XYZ2BGR")) { iTransform = CV_XYZ2BGR; }
//
else if (!strcmp(sTransform, "CV_BGR2YCrCb")) { iTransform = CV_BGR2YCrCb; }
else if (!strcmp(sTransform, "CV_YCrCb2BGR")) { iTransform = CV_YCrCb2BGR; }
//
else if (!strcmp(sTransform, "CV_BGR2HSV")) { iTransform = CV_BGR2HSV; }
else if (!strcmp(sTransform, "CV_HSV2BGR")) { iTransform = CV_HSV2BGR; }
//
else if (!strcmp(sTransform, "CV_BGR2HLS")) { iTransform = CV_BGR2HLS; }
else if (!strcmp(sTransform, "CV_HLS2BGR")) { iTransform = CV_HLS2BGR; }
//
else if (!strcmp(sTransform, "CV_BGR2Lab")) { iTransform = CV_BGR2Lab; }
else if (!strcmp(sTransform, "CV_Lab2BGR")) { iTransform = CV_Lab2BGR; }
//
else if (!strcmp(sTransform, "CV_BGR2Luv")) { iTransform = CV_BGR2Luv; }
else if (!strcmp(sTransform, "CV_Luv2BGR")) { iTransform = CV_Luv2BGR; }
//
else if (!strcmp(sTransform, "CV_BayerBG2BGR")) { iTransform = CV_BayerBG2BGR; }
else if (!strcmp(sTransform, "CV_BayerGB2BGR")) { iTransform = CV_BayerGB2BGR; }
else if (!strcmp(sTransform, "CV_BayerRG2BGR")) { iTransform = CV_BayerRG2BGR; }
else if (!strcmp(sTransform, "CV_BayerGR2BGR")) { iTransform = CV_BayerGR2BGR; }
else {
iTransform = 0; // to avoid compiler warning
return v8::ThrowException(Exception::TypeError(String::New(
"Conversion code is unsupported")));
}
cv::cvtColor(self->mat, self->mat, iTransform);
return scope.Close(Undefined());
}
// @author SergeMv
// arrChannels = img.split();
Handle<Value>
Matrix::Split(const v8::Arguments& args) {
HandleScope scope;
Matrix * self = ObjectWrap::Unwrap<Matrix>(args.This());
vector<cv::Mat> channels;
cv::split(self->mat, channels);
unsigned int size = channels.size();
v8::Local<v8::Array> arrChannels = v8::Array::New(size);
for (unsigned int i = 0; i < size; i++) {
Local<Object> matObject = Matrix::constructor->GetFunction()->NewInstance();
Matrix * m = ObjectWrap::Unwrap<Matrix>(matObject);
m->mat = channels[i];
arrChannels->Set(i, matObject);
}
return scope.Close(arrChannels);
}
// @author SergeMv
// img.merge(arrChannels);
Handle<Value>
Matrix::Merge(const v8::Arguments& args) {
HandleScope scope;
Matrix * self = ObjectWrap::Unwrap<Matrix>(args.This());
if (!args[0]->IsArray()) {
return v8::ThrowException(Exception::TypeError(String::New(
"The argument must be an array")));
}
v8::Handle<v8::Array> jsChannels = v8::Handle<v8::Array>::Cast(args[0]);
unsigned int L = jsChannels->Length();
vector<cv::Mat> vChannels(L);
for (unsigned int i = 0; i < L; i++) {
Matrix * matObject = ObjectWrap::Unwrap<Matrix>(jsChannels->Get(i)->ToObject());
vChannels[i] = matObject->mat;
}
cv::merge(vChannels, self->mat);
return scope.Close(Undefined());
}
// @author SergeMv
// Equalizes histogram
// img.equalizeHist()
Handle<Value>
Matrix::EqualizeHist(const v8::Arguments& args) {
HandleScope scope;
Matrix * self = ObjectWrap::Unwrap<Matrix>(args.This());
cv::equalizeHist(self->mat, self->mat);
return scope.Close(Undefined());
}

View File

@ -9,6 +9,7 @@ class Matrix: public node::ObjectWrap {
static Handle<Value> New(const Arguments &args); static Handle<Value> New(const Arguments &args);
Matrix(); Matrix();
Matrix(cv::Mat other, cv::Rect roi); Matrix(cv::Mat other, cv::Rect roi);
Matrix(cv::Mat other);
Matrix(int rows, int cols); Matrix(int rows, int cols);
Matrix(int rows, int cols, int typ); Matrix(int rows, int cols, int typ);
@ -29,7 +30,6 @@ class Matrix: public node::ObjectWrap {
JSFUNC(Height) JSFUNC(Height)
JSFUNC(Channels) JSFUNC(Channels)
JSFUNC(Clone) JSFUNC(Clone)
JSFUNC(Ellipse) JSFUNC(Ellipse)
JSFUNC(Rectangle) JSFUNC(Rectangle)
JSFUNC(Line) JSFUNC(Line)
@ -57,7 +57,7 @@ class Matrix: public node::ObjectWrap {
JSFUNC(AddWeighted) JSFUNC(AddWeighted)
JSFUNC(BitwiseXor) JSFUNC(BitwiseXor)
JSFUNC(CountNonZero) JSFUNC(CountNonZero)
JSFUNC(Split) //JSFUNC(Split)
JSFUNC(Canny) JSFUNC(Canny)
JSFUNC(Dilate) JSFUNC(Dilate)
JSFUNC(Erode) JSFUNC(Erode)
@ -79,6 +79,10 @@ class Matrix: public node::ObjectWrap {
JSFUNC(MeanStdDev) JSFUNC(MeanStdDev)
JSFUNC(CopyTo) JSFUNC(CopyTo)
JSFUNC(CvtColor)
JSFUNC(Split)
JSFUNC(Merge)
JSFUNC(EqualizeHist)
/* /*
static Handle<Value> Val(const Arguments& args); static Handle<Value> Val(const Arguments& args);