Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ocv3 features2d #615

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 30 additions & 13 deletions src/Features2d.cc
Original file line number Diff line number Diff line change
@@ -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 <nan.h>
Expand All @@ -16,10 +16,10 @@ void Features::Init(Local<Object> 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) {
}

Expand All @@ -28,9 +28,15 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker {

void Execute() {

#if (CV_MAJOR_VERSION == 3)
cv::Ptr<cv::Feature2D> detector = cv::ORB::create();
#else
cv::Ptr<cv::FeatureDetector> detector = cv::FeatureDetector::create("ORB");
cv::Ptr<cv::DescriptorExtractor> extractor =
cv::DescriptorExtractor::create("ORB");
#endif


cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(
"BruteForce-Hamming");

Expand All @@ -42,11 +48,17 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker {
std::vector<cv::KeyPoint> keypoints1;
std::vector<cv::KeyPoint> 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);

Expand Down Expand Up @@ -85,6 +97,11 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker {
void HandleOKCallback() {
Nan::HandleScope scope;

delete image1;
delete image2;
image1 = NULL;
image2 = NULL;

Local<Value> argv[2];

argv[0] = Nan::Null();
Expand All @@ -94,8 +111,8 @@ class AsyncDetectSimilarity: public Nan::AsyncWorker {
}

private:
cv::Mat image1;
cv::Mat image2;
Matrix *image1;
Matrix *image2;
double dissimilarity;
};

Expand All @@ -104,8 +121,8 @@ NAN_METHOD(Features::Similarity) {

REQ_FUN_ARG(2, cb);

cv::Mat image1 = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject())->mat;
cv::Mat image2 = Nan::ObjectWrap::Unwrap<Matrix>(info[1]->ToObject())->mat;
Matrix *image1 = Nan::ObjectWrap::Unwrap<Matrix>(info[0]->ToObject());
Matrix *image2 = Nan::ObjectWrap::Unwrap<Matrix>(info[1]->ToObject());

Nan::Callback *callback = new Nan::Callback(cb.As<Function>());

Expand Down
4 changes: 3 additions & 1 deletion src/Features2d.h
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#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

#define HAVE_NODE_OPENCV_FEATURES2D

#include <opencv2/core/core.hpp>
#include <opencv2/features2d/features2d.hpp>

Expand Down
5 changes: 5 additions & 0 deletions src/Matrix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/Matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object> scalarObj);

static double DblGet(cv::Mat mat, int i, int j);
Expand Down
7 changes: 3 additions & 4 deletions src/init.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,14 @@ extern "C" void init(Local<Object> 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
Expand Down