From c7037ef20c98b0178e880226a48a49e3db7d81f5 Mon Sep 17 00:00:00 2001 From: Anshul Jain Date: Thu, 8 Oct 2015 12:10:38 -0700 Subject: [PATCH 1/3] Fix formatting for CvtColor matrix function --- src/Matrix.cc | 69 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 42 insertions(+), 27 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index fb6a145..1609f7e 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1739,38 +1739,53 @@ NAN_METHOD(Matrix::CvtColor) { Nan::HandleScope scope; Matrix * self = Nan::ObjectWrap::Unwrap(info.This()); + if (info.Length() < 1) { + Nan::ThrowTypeError("Invalid number of arguments"); + } + // Get transform string v8::String::Utf8Value str (info[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 { + + 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 Nan::ThrowTypeError("Conversion code is unsupported"); } From a2073757a264f084b29da08c065a21d7b2f6523f Mon Sep 17 00:00:00 2001 From: Anshul Jain Date: Thu, 8 Oct 2015 12:14:18 -0700 Subject: [PATCH 2/3] Add support for CV_BGR2RGB color transformation --- src/Matrix.cc | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index 1609f7e..8c63ab0 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1785,6 +1785,8 @@ NAN_METHOD(Matrix::CvtColor) { iTransform = CV_BayerRG2BGR; } else if (!strcmp(sTransform, "CV_BayerGR2BGR")) { iTransform = CV_BayerGR2BGR; + } else if (!strcmp(sTransform, "CV_BGR2RGB")) { + iTransform = CV_BGR2RGB; } else { iTransform = 0; // to avoid compiler warning Nan::ThrowTypeError("Conversion code is unsupported"); From dc12c4f2ed202b2c2a1d0251fd8891b97a751cea Mon Sep 17 00:00:00 2001 From: Anshul Jain Date: Thu, 8 Oct 2015 12:16:15 -0700 Subject: [PATCH 3/3] Prevent a copy during grayscale conversion --- src/Matrix.cc | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 8c63ab0..0645c95 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -998,10 +998,7 @@ NAN_METHOD(Matrix::ConvertGrayscale) { Nan::ThrowError("Image is no 3-channel"); } - cv::Mat gray; - - cv::cvtColor(self->mat, gray, CV_BGR2GRAY); - gray.copyTo(self->mat); + cv::cvtColor(self->mat, self->mat, CV_BGR2GRAY); info.GetReturnValue().Set(Nan::Null()); }