Skip to content

Commit d083e71

Browse files
committed
Porting to OpenCV4
Signed-off-by: Ying-Chun Liu (PaulLiu) <[email protected]>
1 parent 358899f commit d083e71

10 files changed

+46
-10
lines changed

src/CamShift.cc

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
#include <opencv2/video/tracking.hpp>
99
#endif
1010

11+
#if CV_MAJOR_VERSION >= 4
12+
#include <opencv2/imgproc/types_c.h>
13+
#endif
14+
1115
#define CHANNEL_HUE 0
1216
#define CHANNEL_SATURATION 1
1317
#define CHANNEL_VALUE 2

src/CascadeClassifierWrap.cc

+5-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
#include "Matrix.h"
44
#include <nan.h>
55

6+
#if CV_MAJOR_VERSION >= 4
7+
#include <opencv2/imgproc/types_c.h>
8+
#endif
9+
610
#ifdef HAVE_OPENCV_OBJDETECT
711

812
Nan::Persistent<FunctionTemplate> CascadeClassifierWrap::constructor;
@@ -73,7 +77,7 @@ class AsyncDetectMultiScale: public Nan::AsyncWorker {
7377
gray = this->im->mat;
7478
}
7579
this->cc->cc.detectMultiScale(gray, objects, this->scale, this->neighbors,
76-
0 | CV_HAAR_SCALE_IMAGE, cv::Size(this->minw, this->minh));
80+
0 | cv::CASCADE_SCALE_IMAGE, cv::Size(this->minw, this->minh));
7781
res = objects;
7882
} catch (cv::Exception& e) {
7983
SetErrorMessage(e.what());

src/Constants.cc

+6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
#include "OpenCV.h"
22
#include "Constants.h"
33

4+
#if CV_MAJOR_VERSION >= 4
5+
#include <opencv2/imgproc/imgproc_c.h>
6+
#endif
7+
48
#define CONST(C) \
59
obj->Set(Nan::New<String>(#C).ToLocalChecked(), Nan::New<Integer>(C));
610

@@ -25,7 +29,9 @@ void Constants::Init(Local<Object> target) {
2529
CONST(CV_32S);
2630
CONST(CV_32F);
2731
CONST(CV_64F);
32+
#if CV_MAJOR_VERSION <= 3
2833
CONST(CV_USRTYPE1);
34+
#endif
2935

3036
CONST(CV_8UC1);
3137
CONST(CV_8UC2);

src/FaceRecognizer.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
#include "FaceRecognizer.h"
55
#include "Matrix.h"
66
#include <nan.h>
7+
#include <opencv2/imgproc/types_c.h>
78

8-
#if CV_MAJOR_VERSION < 3
9-
#elif CV_MINOR_VERSION < 3
9+
#if CV_MAJOR_VERSION < 3 || (CV_MAJOR_VERSION == 3 && CV_MINOR_VERSION < 3)
1010
namespace cv {
1111
using std::vector;
1212
using cv::face::createEigenFaceRecognizer;
@@ -92,7 +92,7 @@ NAN_METHOD(FaceRecognizerWrap::New) {
9292
}
9393

9494
// By default initialize LBPH
95-
#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3
95+
#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3)
9696
cv::Ptr<cv::FaceRecognizer> f = cv::LBPHFaceRecognizer::create(1, 8, 8, 8, 80.0);
9797
#else
9898
cv::Ptr<cv::FaceRecognizer> f = cv::createLBPHFaceRecognizer(1, 8, 8, 8, 80.0);
@@ -119,7 +119,7 @@ NAN_METHOD(FaceRecognizerWrap::CreateLBPH) {
119119
DOUBLE_FROM_ARGS(threshold, 4)
120120

121121
Local<Object> n = Nan::NewInstance(Nan::GetFunction(Nan::New(FaceRecognizerWrap::constructor)).ToLocalChecked()).ToLocalChecked();
122-
#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3
122+
#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3)
123123
cv::Ptr<cv::FaceRecognizer> f = cv::LBPHFaceRecognizer::create(radius, neighbors, grid_x, grid_y, threshold);
124124
#else
125125
cv::Ptr<cv::FaceRecognizer> f = cv::createLBPHFaceRecognizer(radius, neighbors, grid_x, grid_y, threshold);
@@ -140,7 +140,7 @@ NAN_METHOD(FaceRecognizerWrap::CreateEigen) {
140140
DOUBLE_FROM_ARGS(threshold, 1)
141141

142142
Local<Object> n = Nan::NewInstance(Nan::GetFunction(Nan::New(FaceRecognizerWrap::constructor)).ToLocalChecked()).ToLocalChecked();
143-
#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3
143+
#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3)
144144
cv::Ptr<cv::FaceRecognizer> f = cv::EigenFaceRecognizer::create(components, threshold);
145145
#else
146146
cv::Ptr<cv::FaceRecognizer> f = cv::createEigenFaceRecognizer(components, threshold);
@@ -161,7 +161,7 @@ NAN_METHOD(FaceRecognizerWrap::CreateFisher) {
161161
DOUBLE_FROM_ARGS(threshold, 1)
162162

163163
Local<Object> n = Nan::NewInstance(Nan::GetFunction(Nan::New(FaceRecognizerWrap::constructor)).ToLocalChecked()).ToLocalChecked();
164-
#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3
164+
#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3)
165165
cv::Ptr<cv::FaceRecognizer> f = cv::FisherFaceRecognizer::create(components, threshold);
166166
#else
167167
cv::Ptr<cv::FaceRecognizer> f = cv::createFisherFaceRecognizer(components, threshold);
@@ -423,7 +423,7 @@ NAN_METHOD(FaceRecognizerWrap::SaveSync) {
423423
JSTHROW("Save takes a filename")
424424
}
425425
std::string filename = std::string(*Nan::Utf8String(info[0]->ToString()));
426-
#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3
426+
#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3)
427427
self->rec->write(filename);
428428
#else
429429
self->rec->save(filename);
@@ -437,7 +437,7 @@ NAN_METHOD(FaceRecognizerWrap::LoadSync) {
437437
JSTHROW("Load takes a filename")
438438
}
439439
std::string filename = std::string(*Nan::Utf8String(info[0]->ToString()));
440-
#if CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3
440+
#if CV_MAJOR_VERSION >= 4 || (CV_MAJOR_VERSION >= 3 && CV_MINOR_VERSION >= 3)
441441
self->rec->read(filename);
442442
#else
443443
self->rec->load(filename);

src/Matrix.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@
44
#include <string.h>
55
#include <nan.h>
66

7+
#if CV_MAJOR_VERSION >= 4
8+
#include <opencv2/imgproc/types_c.h>
9+
#include <opencv2/imgproc/imgproc_c.h>
10+
#endif
11+
712
Nan::Persistent<FunctionTemplate> Matrix::constructor;
813

914
cv::Scalar setColor(Local<Object> objColor);
@@ -1609,7 +1614,9 @@ NAN_METHOD(Matrix::Canny) {
16091614
int lowThresh = info[0]->NumberValue();
16101615
int highThresh = info[1]->NumberValue();
16111616

1612-
cv::Canny(self->mat, self->mat, lowThresh, highThresh);
1617+
cv::Mat newMat;
1618+
cv::Canny(self->mat, newMat, lowThresh, highThresh);
1619+
newMat.copyTo(self->mat);
16131620

16141621
info.GetReturnValue().Set(Nan::Null());
16151622
}

src/OpenCV.h

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424

2525
#if ((CV_MAJOR_VERSION <= 2) && (CV_MINOR_VERSION <= 4))
2626
#include <opencv/highgui.h>
27+
#elif CV_MAJOR_VERSION >= 4
28+
#include <opencv2/imgcodecs/legacy/constants_c.h>
2729
#else
2830
#include <opencv2/imgcodecs/imgcodecs_c.h>
2931
#endif

src/Point.h

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
#include "OpenCV.h"
44

5+
#if CV_MAJOR_VERSION >= 4
6+
#include <opencv2/core/types_c.h>
7+
#endif
8+
59
class Point: public Nan::ObjectWrap {
610
public:
711
CvPoint2D32f point;

src/VideoCaptureWrap.cc

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
#include <iostream>
66

7+
#if CV_MAJOR_VERSION >= 4
8+
#include <opencv2/videoio/legacy/constants_c.h>
9+
#endif
10+
711
#ifdef HAVE_OPENCV_VIDEOIO
812

913
Nan::Persistent<FunctionTemplate> VideoCaptureWrap::constructor;

src/VideoWriterWrap.cc

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
#include "Matrix.h"
33
#include "OpenCV.h"
44

5+
#if CV_MAJOR_VERSION >= 4
6+
#include <opencv2/videoio/legacy/constants_c.h>
7+
#endif
8+
59
#include <iostream>
610

711
#ifdef HAVE_OPENCV_VIDEOIO

utils/find-opencv.js

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ var flag = flags[process.argv[2]] || '--exists'
1616
// the same machine, the opencv.pc for 3.y can be installed as opencv3.pc and
1717
// then selected by |export PKG_CONFIG_OPENCV3=1| before building node-opencv.
1818
var opencv = process.env.PKG_CONFIG_OPENCV3 === "1" ? "opencv3" : ' "opencv >= 2.3.1"';
19+
opencv = process.env.PKG_CONFIG_OPENCV4 === "1" ? "opencv4" : opencv;
1920

2021
function main(){
2122
//Try using pkg-config, but if it fails and it is on Windows, try the fallback

0 commit comments

Comments
 (0)