From d083e71d71c0fcd18fc5428b688ca6e6fce0b1cd Mon Sep 17 00:00:00 2001 From: "Ying-Chun Liu (PaulLiu)" Date: Sun, 9 Feb 2020 01:25:06 +0800 Subject: [PATCH] Porting to OpenCV4 Signed-off-by: Ying-Chun Liu (PaulLiu) --- src/CamShift.cc | 4 ++++ src/CascadeClassifierWrap.cc | 6 +++++- src/Constants.cc | 6 ++++++ src/FaceRecognizer.cc | 16 ++++++++-------- src/Matrix.cc | 9 ++++++++- src/OpenCV.h | 2 ++ src/Point.h | 4 ++++ src/VideoCaptureWrap.cc | 4 ++++ src/VideoWriterWrap.cc | 4 ++++ utils/find-opencv.js | 1 + 10 files changed, 46 insertions(+), 10 deletions(-) diff --git a/src/CamShift.cc b/src/CamShift.cc index 638bddc..34830aa 100644 --- a/src/CamShift.cc +++ b/src/CamShift.cc @@ -8,6 +8,10 @@ #include #endif +#if CV_MAJOR_VERSION >= 4 +#include +#endif + #define CHANNEL_HUE 0 #define CHANNEL_SATURATION 1 #define CHANNEL_VALUE 2 diff --git a/src/CascadeClassifierWrap.cc b/src/CascadeClassifierWrap.cc index de00274..113c27a 100755 --- a/src/CascadeClassifierWrap.cc +++ b/src/CascadeClassifierWrap.cc @@ -3,6 +3,10 @@ #include "Matrix.h" #include +#if CV_MAJOR_VERSION >= 4 +#include +#endif + #ifdef HAVE_OPENCV_OBJDETECT Nan::Persistent CascadeClassifierWrap::constructor; @@ -73,7 +77,7 @@ public: gray = this->im->mat; } this->cc->cc.detectMultiScale(gray, objects, this->scale, this->neighbors, - 0 | CV_HAAR_SCALE_IMAGE, cv::Size(this->minw, this->minh)); + 0 | cv::CASCADE_SCALE_IMAGE, cv::Size(this->minw, this->minh)); res = objects; } catch (cv::Exception& e) { SetErrorMessage(e.what()); diff --git a/src/Constants.cc b/src/Constants.cc index 41ea703..ab3d62e 100644 --- a/src/Constants.cc +++ b/src/Constants.cc @@ -1,6 +1,10 @@ #include "OpenCV.h" #include "Constants.h" +#if CV_MAJOR_VERSION >= 4 +#include +#endif + #define CONST(C) \ obj->Set(Nan::New(#C).ToLocalChecked(), Nan::New(C)); @@ -25,7 +29,9 @@ void Constants::Init(Local target) { CONST(CV_32S); CONST(CV_32F); CONST(CV_64F); +#if CV_MAJOR_VERSION <= 3 CONST(CV_USRTYPE1); +#endif CONST(CV_8UC1); CONST(CV_8UC2); diff --git a/src/FaceRecognizer.cc b/src/FaceRecognizer.cc index 5a0e034..f4b75e5 100644 --- a/src/FaceRecognizer.cc +++ b/src/FaceRecognizer.cc @@ -4,9 +4,9 @@ #include "FaceRecognizer.h" #include "Matrix.h" #include +#include -#if CV_MAJOR_VERSION < 3 -#elif CV_MINOR_VERSION < 3 +#if CV_MAJOR_VERSION < 3 || (CV_MAJOR_VERSION == 3 && CV_MINOR_VERSION < 3) namespace cv { using std::vector; using cv::face::createEigenFaceRecognizer; @@ -92,7 +92,7 @@ NAN_METHOD(FaceRecognizerWrap::New) { } // By default initialize LBPH -#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3 +#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3) cv::Ptr f = cv::LBPHFaceRecognizer::create(1, 8, 8, 8, 80.0); #else cv::Ptr f = cv::createLBPHFaceRecognizer(1, 8, 8, 8, 80.0); @@ -119,7 +119,7 @@ NAN_METHOD(FaceRecognizerWrap::CreateLBPH) { DOUBLE_FROM_ARGS(threshold, 4) Local n = Nan::NewInstance(Nan::GetFunction(Nan::New(FaceRecognizerWrap::constructor)).ToLocalChecked()).ToLocalChecked(); -#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3 +#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3) cv::Ptr f = cv::LBPHFaceRecognizer::create(radius, neighbors, grid_x, grid_y, threshold); #else cv::Ptr f = cv::createLBPHFaceRecognizer(radius, neighbors, grid_x, grid_y, threshold); @@ -140,7 +140,7 @@ NAN_METHOD(FaceRecognizerWrap::CreateEigen) { DOUBLE_FROM_ARGS(threshold, 1) Local n = Nan::NewInstance(Nan::GetFunction(Nan::New(FaceRecognizerWrap::constructor)).ToLocalChecked()).ToLocalChecked(); -#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3 +#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3) cv::Ptr f = cv::EigenFaceRecognizer::create(components, threshold); #else cv::Ptr f = cv::createEigenFaceRecognizer(components, threshold); @@ -161,7 +161,7 @@ NAN_METHOD(FaceRecognizerWrap::CreateFisher) { DOUBLE_FROM_ARGS(threshold, 1) Local n = Nan::NewInstance(Nan::GetFunction(Nan::New(FaceRecognizerWrap::constructor)).ToLocalChecked()).ToLocalChecked(); -#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3 +#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3) cv::Ptr f = cv::FisherFaceRecognizer::create(components, threshold); #else cv::Ptr f = cv::createFisherFaceRecognizer(components, threshold); @@ -423,7 +423,7 @@ NAN_METHOD(FaceRecognizerWrap::SaveSync) { JSTHROW("Save takes a filename") } std::string filename = std::string(*Nan::Utf8String(info[0]->ToString())); -#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3 +#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3) self->rec->write(filename); #else self->rec->save(filename); @@ -437,7 +437,7 @@ NAN_METHOD(FaceRecognizerWrap::LoadSync) { JSTHROW("Load takes a filename") } std::string filename = std::string(*Nan::Utf8String(info[0]->ToString())); -#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3 +#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3) self->rec->read(filename); #else self->rec->load(filename); diff --git a/src/Matrix.cc b/src/Matrix.cc index 681815e..99decc0 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -4,6 +4,11 @@ #include #include +#if CV_MAJOR_VERSION >= 4 +#include +#include +#endif + Nan::Persistent Matrix::constructor; cv::Scalar setColor(Local objColor); @@ -1609,7 +1614,9 @@ NAN_METHOD(Matrix::Canny) { int lowThresh = info[0]->NumberValue(); int highThresh = info[1]->NumberValue(); - cv::Canny(self->mat, self->mat, lowThresh, highThresh); + cv::Mat newMat; + cv::Canny(self->mat, newMat, lowThresh, highThresh); + newMat.copyTo(self->mat); info.GetReturnValue().Set(Nan::Null()); } diff --git a/src/OpenCV.h b/src/OpenCV.h index 7b1d254..1e83b79 100755 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -24,6 +24,8 @@ #if ((CV_MAJOR_VERSION <= 2) && (CV_MINOR_VERSION <= 4)) #include +#elif CV_MAJOR_VERSION >= 4 +#include #else #include #endif diff --git a/src/Point.h b/src/Point.h index 3a5dc06..0b08204 100755 --- a/src/Point.h +++ b/src/Point.h @@ -2,6 +2,10 @@ #include "OpenCV.h" +#if CV_MAJOR_VERSION >= 4 +#include +#endif + class Point: public Nan::ObjectWrap { public: CvPoint2D32f point; diff --git a/src/VideoCaptureWrap.cc b/src/VideoCaptureWrap.cc index 0e1f4cc..23d5cda 100755 --- a/src/VideoCaptureWrap.cc +++ b/src/VideoCaptureWrap.cc @@ -4,6 +4,10 @@ #include +#if CV_MAJOR_VERSION >= 4 +#include +#endif + #ifdef HAVE_OPENCV_VIDEOIO Nan::Persistent VideoCaptureWrap::constructor; diff --git a/src/VideoWriterWrap.cc b/src/VideoWriterWrap.cc index 32adb2d..986f463 100755 --- a/src/VideoWriterWrap.cc +++ b/src/VideoWriterWrap.cc @@ -2,6 +2,10 @@ #include "Matrix.h" #include "OpenCV.h" +#if CV_MAJOR_VERSION >= 4 +#include +#endif + #include #ifdef HAVE_OPENCV_VIDEOIO diff --git a/utils/find-opencv.js b/utils/find-opencv.js index be1cfa8..9aed69a 100644 --- a/utils/find-opencv.js +++ b/utils/find-opencv.js @@ -16,6 +16,7 @@ var flag = flags[process.argv[2]] || '--exists' // the same machine, the opencv.pc for 3.y can be installed as opencv3.pc and // then selected by |export PKG_CONFIG_OPENCV3=1| before building node-opencv. var opencv = process.env.PKG_CONFIG_OPENCV3 === "1" ? "opencv3" : ' "opencv >= 2.3.1"'; +opencv = process.env.PKG_CONFIG_OPENCV4 === "1" ? "opencv4" : opencv; function main(){ //Try using pkg-config, but if it fails and it is on Windows, try the fallback