diff --git a/src/Matrix.cc b/src/Matrix.cc index 0645c95..46be855 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; @@ -1629,25 +1630,62 @@ 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(); + + if (info.Length() >= 3) { Nan::Utf8String typstr(info[2]); + if (strcmp(*typstr, "Binary") == 0) { - typ = 0; + // Uses default value } - if (strcmp(*typstr, "Binary Inverted") == 0) { - typ = 1; + else if (strcmp(*typstr, "Binary Inverted") == 0) { + typ = cv::THRESH_BINARY_INV; } - if (strcmp(*typstr, "Threshold Truncated") == 0) { - typ = 2; + else if (strcmp(*typstr, "Threshold Truncated") == 0) { + typ = cv::THRESH_TRUNC; } - if (strcmp(*typstr, "Threshold to Zero") == 0) { - typ = 3; + else if (strcmp(*typstr, "Threshold to Zero") == 0) { + typ = cv::THRESH_TOZERO; } - if (strcmp(*typstr, "Threshold to Zero Inverted") == 0) { - typ = 4; + else if (strcmp(*typstr, "Threshold to Zero Inverted") == 0) { + typ = cv::THRESH_TOZERO_INV; + } + else { + char *typeString = *typstr; + char text[] = "\" is no supported binarization technique. " + "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); + strcat(errorMessage, text); + + Nan::ThrowError(errorMessage); + return; + } + } + + 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; } }