From 5801b953263d48793eefd639ad18c268a6143faf Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 2 Mar 2018 11:44:56 +0000 Subject: [PATCH 1/3] Update Features2d to OCV 3 --- src/Features2d.cc | 43 ++++++++++++++++++++++++++++++------------- src/Features2d.h | 2 +- 2 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/Features2d.cc b/src/Features2d.cc index f2b75cb0..1fd4c5f8 100644 --- a/src/Features2d.cc +++ b/src/Features2d.cc @@ -1,6 +1,6 @@ #include "OpenCV.h" -#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) +#if (((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) || (CV_MAJOR_VERSION == 3)) #include "Features2d.h" #include "Matrix.h" #include @@ -16,10 +16,10 @@ void Features::Init(Local target) { class AsyncDetectSimilarity: public Nan::AsyncWorker { public: - AsyncDetectSimilarity(Nan::Callback *callback, cv::Mat image1, cv::Mat image2) : + AsyncDetectSimilarity(Nan::Callback *callback, Matrix *image1, Matrix *image2) : Nan::AsyncWorker(callback), - image1(image1), - image2(image2), + image1(new Matrix(image1)), + image2(new Matrix(image2)), dissimilarity(0) { } @@ -28,9 +28,15 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker { void Execute() { +#if (CV_MAJOR_VERSION == 3) + cv::Ptr detector = cv::ORB::create(); +#else cv::Ptr detector = cv::FeatureDetector::create("ORB"); cv::Ptr extractor = cv::DescriptorExtractor::create("ORB"); +#endif + + cv::Ptr matcher = cv::DescriptorMatcher::create( "BruteForce-Hamming"); @@ -42,11 +48,17 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker { std::vector keypoints1; std::vector keypoints2; - detector->detect(image1, keypoints1); - detector->detect(image2, keypoints2); - - extractor->compute(image1, keypoints1, descriptors1); - extractor->compute(image2, keypoints2, descriptors2); +#if (CV_MAJOR_VERSION == 3) + detector->detect(image1->mat, keypoints1); + detector->detect(image2->mat, keypoints2); + detector->compute(image1->mat, keypoints1, descriptors1); + detector->compute(image2->mat, keypoints2, descriptors2); +#else + detector->detect(image1->mat, keypoints1); + detector->detect(image2->mat, keypoints2); + extractor->compute(image1->mat, keypoints1, descriptors1); + extractor->compute(image2->mat, keypoints2, descriptors2); +#endif matcher->match(descriptors1, descriptors2, matches); @@ -85,6 +97,11 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker { void HandleOKCallback() { Nan::HandleScope scope; + delete image1; + delete image2; + image1 = NULL; + image2 = NULL; + Local argv[2]; argv[0] = Nan::Null(); @@ -94,8 +111,8 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker { } private: - cv::Mat image1; - cv::Mat image2; + Matrix *image1; + Matrix *image2; double dissimilarity; }; @@ -104,8 +121,8 @@ NAN_METHOD(Features::Similarity) { REQ_FUN_ARG(2, cb); - cv::Mat image1 = Nan::ObjectWrap::Unwrap(info[0]->ToObject())->mat; - cv::Mat image2 = Nan::ObjectWrap::Unwrap(info[1]->ToObject())->mat; + Matrix *image1 = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); + Matrix *image2 = Nan::ObjectWrap::Unwrap(info[1]->ToObject()); Nan::Callback *callback = new Nan::Callback(cb.As()); diff --git a/src/Features2d.h b/src/Features2d.h index 3d029779..41d56ed4 100644 --- a/src/Features2d.h +++ b/src/Features2d.h @@ -1,6 +1,6 @@ #include "OpenCV.h" -#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) +#if (((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) || (CV_MAJOR_VERSION == 3)) #ifdef HAVE_OPENCV_FEATURES2D From 7875fe1cc7e41d5af7d890b840fcc82969b60b90 Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 2 Mar 2018 11:59:21 +0000 Subject: [PATCH 2/3] Add Matrix(Matrix*) constructor (comes for memory stuff). --- src/Matrix.cc | 5 +++++ src/Matrix.h | 1 + 2 files changed, 6 insertions(+) diff --git a/src/Matrix.cc b/src/Matrix.cc index 01b4443d..238c9b29 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -176,6 +176,11 @@ Matrix::Matrix(int rows, int cols, int type) : mat = cv::Mat(rows, cols, type); } +Matrix::Matrix(Matrix *m) : + node_opencv::Matrix() { + mat = cv::Mat(m->mat); +} + Matrix::Matrix(cv::Mat m, cv::Rect roi) : node_opencv::Matrix() { mat = cv::Mat(m, roi); diff --git a/src/Matrix.h b/src/Matrix.h index 81e89b21..c57a5eda 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -11,6 +11,7 @@ class Matrix: public node_opencv::Matrix{ Matrix(cv::Mat other, cv::Rect roi); Matrix(int rows, int cols); Matrix(int rows, int cols, int type); + Matrix(Matrix *m); Matrix(int rows, int cols, int type, Local scalarObj); static double DblGet(cv::Mat mat, int i, int j); From 943fb71d2da57b2c8c9be21933e7f0234892ac7a Mon Sep 17 00:00:00 2001 From: Simon Hailes Date: Fri, 2 Mar 2018 12:26:09 +0000 Subject: [PATCH 3/3] include features2d if ocv 3 --- src/Features2d.h | 2 ++ src/init.cc | 7 +++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/src/Features2d.h b/src/Features2d.h index 41d56ed4..2ce94925 100644 --- a/src/Features2d.h +++ b/src/Features2d.h @@ -4,6 +4,8 @@ #ifdef HAVE_OPENCV_FEATURES2D +#define HAVE_NODE_OPENCV_FEATURES2D + #include #include diff --git a/src/init.cc b/src/init.cc index 5b1d78af..48adabd0 100755 --- a/src/init.cc +++ b/src/init.cc @@ -50,15 +50,14 @@ extern "C" void init(Local target) { ImgProc::Init(target); Histogram::Init(target); #endif +#ifdef HAVE_NODE_OPENCV_FEATURES2D + Features::Init(target); +#endif #if CV_MAJOR_VERSION < 3 StereoBM::Init(target); StereoSGBM::Init(target); StereoGC::Init(target); - #if CV_MAJOR_VERSION == 2 && CV_MINOR_VERSION >=4 - #ifdef HAVE_OPENCV_FEATURES2D - Features::Init(target); - #endif LDAWrap::Init(target); #endif #endif