From 0737bdd85a355448066305eb66bd60cc2e7265d3 Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 8 Oct 2015 20:50:59 +0200 Subject: [PATCH 1/6] Use "else if" to clarify mutual exclusivity --- src/Matrix.cc | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index fb6a145..dd53681 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1632,24 +1632,24 @@ NAN_METHOD(Matrix::Threshold) { double threshold = info[0]->NumberValue(); double maxVal = info[1]->NumberValue(); - int typ = cv::THRESH_BINARY; + if (info.Length() == 3) { - // typ = info[2]->IntegerValue(); Nan::Utf8String typstr(info[2]); + if (strcmp(*typstr, "Binary") == 0) { typ = 0; } - if (strcmp(*typstr, "Binary Inverted") == 0) { + else if (strcmp(*typstr, "Binary Inverted") == 0) { typ = 1; } - if (strcmp(*typstr, "Threshold Truncated") == 0) { + else if (strcmp(*typstr, "Threshold Truncated") == 0) { typ = 2; } - if (strcmp(*typstr, "Threshold to Zero") == 0) { + else if (strcmp(*typstr, "Threshold to Zero") == 0) { typ = 3; } - if (strcmp(*typstr, "Threshold to Zero Inverted") == 0) { + else if (strcmp(*typstr, "Threshold to Zero Inverted") == 0) { typ = 4; } } From 769b3f912e02212b0a3be4062008329f12e2b18b Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 8 Oct 2015 21:43:05 +0200 Subject: [PATCH 2/6] Remove redundant variable assignment --- src/Matrix.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index dd53681..bc5f64f 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1638,7 +1638,7 @@ NAN_METHOD(Matrix::Threshold) { Nan::Utf8String typstr(info[2]); if (strcmp(*typstr, "Binary") == 0) { - typ = 0; + // Uses default value } else if (strcmp(*typstr, "Binary Inverted") == 0) { typ = 1; From 901edf4d5866c65f996e27d49f8cf9cdbd3d17e4 Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 8 Oct 2015 21:43:36 +0200 Subject: [PATCH 3/6] Throw error if invalid binarization technique is provided --- src/Matrix.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index bc5f64f..8af505d 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1,6 +1,7 @@ #include "Contours.h" #include "Matrix.h" #include "OpenCV.h" +#include #include Nan::Persistent Matrix::constructor; @@ -1652,6 +1653,19 @@ NAN_METHOD(Matrix::Threshold) { else if (strcmp(*typstr, "Threshold to Zero Inverted") == 0) { typ = 4; } + else { + char *typeString = *typstr; + char text[] = "\" is no supported binarization technique. " + "Use \"Binary\", \"Binary Inverted\", \"Threshold Truncated\", " + "\"Threshold to Zero\" or \"Threshold to Zero Inverted\""; + char errorMessage[strlen(typeString) + strlen(text) + 2]; + strcpy(errorMessage, "\""); + strcat(errorMessage, typeString); + strcat(errorMessage, text); + + Nan::ThrowError(errorMessage); + return; + } } Local < Object > img_to_return = From 828133004464ddacd7cb0b608385c2e0e701f817 Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 8 Oct 2015 21:46:03 +0200 Subject: [PATCH 4/6] Use correct datatype for binarization type --- src/Matrix.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 8af505d..ea2338b 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1642,16 +1642,16 @@ NAN_METHOD(Matrix::Threshold) { // Uses default value } else if (strcmp(*typstr, "Binary Inverted") == 0) { - typ = 1; + typ = cv::THRESH_BINARY_INV; } else if (strcmp(*typstr, "Threshold Truncated") == 0) { - typ = 2; + typ = cv::THRESH_TRUNC; } else if (strcmp(*typstr, "Threshold to Zero") == 0) { - typ = 3; + typ = cv::THRESH_TOZERO; } else if (strcmp(*typstr, "Threshold to Zero Inverted") == 0) { - typ = 4; + typ = cv::THRESH_TOZERO_INV; } else { char *typeString = *typstr; From 90fe184501ba15b3e29fb7706696c64e6a08652b Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 8 Oct 2015 21:58:32 +0200 Subject: [PATCH 5/6] Mark the default value in error message --- src/Matrix.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index ea2338b..06928c4 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1656,8 +1656,9 @@ NAN_METHOD(Matrix::Threshold) { else { char *typeString = *typstr; char text[] = "\" is no supported binarization technique. " - "Use \"Binary\", \"Binary Inverted\", \"Threshold Truncated\", " - "\"Threshold to Zero\" or \"Threshold to Zero Inverted\""; + "Use \"Binary\" (default), \"Binary Inverted\", " + "\"Threshold Truncated\", \"Threshold to Zero\" " + "or \"Threshold to Zero Inverted\""; char errorMessage[strlen(typeString) + strlen(text) + 2]; strcpy(errorMessage, "\""); strcat(errorMessage, typeString); From 9faf0f5a772a97da155d1f8979af09d38d1f427f Mon Sep 17 00:00:00 2001 From: Adrian Sieber Date: Thu, 8 Oct 2015 22:00:12 +0200 Subject: [PATCH 6/6] Add function argument to specify the threshold algorithm --- src/Matrix.cc | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index 06928c4..b2bd640 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -1635,7 +1635,7 @@ NAN_METHOD(Matrix::Threshold) { double maxVal = info[1]->NumberValue(); int typ = cv::THRESH_BINARY; - if (info.Length() == 3) { + if (info.Length() >= 3) { Nan::Utf8String typstr(info[2]); if (strcmp(*typstr, "Binary") == 0) { @@ -1669,6 +1669,29 @@ NAN_METHOD(Matrix::Threshold) { } } + if (info.Length() >= 4) { + Nan::Utf8String algorithm(info[3]); + + if (strcmp(*algorithm, "Simple") == 0) { + // Uses default + } + else if (strcmp(*algorithm, "Otsu") == 0) { + typ += 8; + } + else { + char *algo = *algorithm; + char text[] = "\" is no supported threshold algorithm. " + "Use \"Simple\" (default) or \"Otsu\"."; + char errorMessage[strlen(algo) + strlen(text) + 2]; + strcpy(errorMessage, "\""); + strcat(errorMessage, algo); + strcat(errorMessage, text); + + Nan::ThrowError(errorMessage); + return; + } + } + Local < Object > img_to_return = Nan::New(Matrix::constructor)->GetFunction()->NewInstance(); Matrix *img = Nan::ObjectWrap::Unwrap(img_to_return);