diff --git a/inc/Matrix.h b/inc/Matrix.h index beed998..c2490a6 100755 --- a/inc/Matrix.h +++ b/inc/Matrix.h @@ -11,7 +11,7 @@ */ #ifndef NODE_OPENCV_MATRIX_H #define NODE_OPENCV_MATRIX_H -#include +#include #include namespace node_opencv { diff --git a/lib/opencv.js b/lib/opencv.js index a594700..f4b3901 100755 --- a/lib/opencv.js +++ b/lib/opencv.js @@ -7,30 +7,34 @@ var Stream = require('stream').Stream var cv = module.exports = require('./bindings'); var Matrix = cv.Matrix - , VideoCapture = cv.VideoCapture - , VideoWriter = cv.VideoWriter , ImageStream , ImageDataStream , ObjectDetectionStream , VideoStream; -Matrix.prototype.detectObject = function(classifier, opts, cb){ - var face_cascade; - opts = opts || {}; - cv._detectObjectClassifiers = cv._detectObjectClassifiers || {}; +if (cv.CascadeClassifier) { + Matrix.prototype.detectObject = function(classifier, opts, cb){ + var face_cascade; + opts = opts || {}; + cv._detectObjectClassifiers = cv._detectObjectClassifiers || {}; - if (!(face_cascade = cv._detectObjectClassifiers[classifier])){ - face_cascade = new cv.CascadeClassifier(classifier); - cv._detectObjectClassifiers[classifier] = face_cascade; + if (!(face_cascade = cv._detectObjectClassifiers[classifier])){ + face_cascade = new cv.CascadeClassifier(classifier); + cv._detectObjectClassifiers[classifier] = face_cascade; + } + + face_cascade.detectMultiScale(this, cb, opts.scale, opts.neighbors + , opts.min && opts.min[0], opts.min && opts.min[1]); + } +} else { + cv.Matrix.prototype.detectObject = function() { + throw new Error("You need to install OpenCV with OBJDETECT module"); } - - face_cascade.detectMultiScale(this, cb, opts.scale, opts.neighbors - , opts.min && opts.min[0], opts.min && opts.min[1]); } -Matrix.prototype.inspect = function(){ +cv.Matrix.prototype.inspect = function(){ var size = (this.size()||[]).join('x'); return "[ Matrix " + size + " ]"; } @@ -189,69 +193,70 @@ ImageDataStream.prototype.end = function(b){ }); } - -ObjectDetectionStream = cv.ObjectDetectionStream = function(cascade, opts){ - this.classifier = new cv.CascadeClassifier(cascade); - this.opts = opts || {}; - this.readable = true; - this.writable = true; -} -util.inherits(ObjectDetectionStream, Stream); - - -ObjectDetectionStream.prototype.write = function(m){ - var self = this; - this.classifier.detectMultiScale(m, function(err, objs){ - if (err) return self.emit('error', err); - self.emit('data', objs, m); +if (cv.CascadeClassifier) { + ObjectDetectionStream = cv.ObjectDetectionStream = function(cascade, opts){ + this.classifier = new cv.CascadeClassifier(cascade); + this.opts = opts || {}; + this.readable = true; + this.writable = true; } - , this.opts.scale - , this.opts.neighbors - , this.opts.min && this.opts.min[0] - , this.opts.min && this.opts.min[1]); -} + util.inherits(ObjectDetectionStream, Stream); - -VideoStream = cv.VideoStream = function(src){ - if (!(src instanceof VideoCapture)) src = new VideoCapture(src); - this.video = src; - this.readable = true; - this.paused = false; -} -util.inherits(VideoStream, Stream); - - -VideoStream.prototype.read = function(){ - var self = this; - var frame = function(){ - self.video.read(function(err, mat){ + ObjectDetectionStream.prototype.write = function(m){ + var self = this; + this.classifier.detectMultiScale(m, function(err, objs){ if (err) return self.emit('error', err); - self.emit('data', mat); - if (!self.paused) process.nextTick(frame); - }) + self.emit('data', objs, m); + } + , this.opts.scale + , this.opts.neighbors + , this.opts.min && this.opts.min[0] + , this.opts.min && this.opts.min[1]); + } +} + + +if (cv.VideoCapture) { + var VideoCapture = cv.VideoCapture + + VideoStream = cv.VideoStream = function(src){ + if (!(src instanceof VideoCapture)) src = new VideoCapture(src); + this.video = src; + this.readable = true; + this.paused = false; + } + util.inherits(VideoStream, Stream); + + + VideoStream.prototype.read = function(){ + var self = this; + var frame = function(){ + self.video.read(function(err, mat){ + if (err) return self.emit('error', err); + self.emit('data', mat); + if (!self.paused) process.nextTick(frame); + }) + } + + frame(); } - frame(); + VideoStream.prototype.pause = function(){ + this.paused = true; + } + + + VideoStream.prototype.resume = function(){ + this.paused = false; + this.read(); + } + + VideoCapture.prototype.toStream = function(){ + return new VideoStream(this); + } } -VideoStream.prototype.pause = function(){ - this.paused = true; -} - - -VideoStream.prototype.resume = function(){ - this.paused = false; - this.read(); -} - - -VideoCapture.prototype.toStream = function(){ - return new VideoStream(this); -} - - - // Provide cascade data for faces etc. var CASCADES = { FACE_CASCADE: 'haarcascade_frontalface_alt.xml' diff --git a/src/BackgroundSubtractor.cc b/src/BackgroundSubtractor.cc index 7ddba75..1d601a1 100644 --- a/src/BackgroundSubtractor.cc +++ b/src/BackgroundSubtractor.cc @@ -3,6 +3,8 @@ #include #include +#ifdef HAVE_OPENCV_VIDEO + #if CV_MAJOR_VERSION >= 3 #warning TODO: port me to OpenCV 3 #endif @@ -129,3 +131,4 @@ BackgroundSubtractorWrap::BackgroundSubtractorWrap( #endif +#endif diff --git a/src/BackgroundSubtractor.h b/src/BackgroundSubtractor.h index d661e33..649fae9 100644 --- a/src/BackgroundSubtractor.h +++ b/src/BackgroundSubtractor.h @@ -1,5 +1,7 @@ #include "OpenCV.h" +#ifdef HAVE_OPENCV_VIDEO + #if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) #include @@ -19,3 +21,5 @@ public: }; #endif + +#endif diff --git a/src/Calib3D.cc b/src/Calib3D.cc index 53c84d0..e8e5ddb 100644 --- a/src/Calib3D.cc +++ b/src/Calib3D.cc @@ -1,6 +1,8 @@ #include "Calib3D.h" #include "Matrix.h" +#ifdef HAVE_OPENCV_CALIB3D + inline Local matrixFromMat(cv::Mat &input) { Local matrixWrap = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked(); @@ -564,3 +566,5 @@ NAN_METHOD(Calib3D::ReprojectImageTo3D) { return; } } + +#endif diff --git a/src/Calib3D.h b/src/Calib3D.h index a1846c8..ca5e3fe 100644 --- a/src/Calib3D.h +++ b/src/Calib3D.h @@ -3,6 +3,8 @@ #include "OpenCV.h" +#ifdef HAVE_OPENCV_CALIB3D + #if CV_MAJOR_VERSION >= 3 #include #endif @@ -25,3 +27,4 @@ public: }; #endif +#endif diff --git a/src/CamShift.cc b/src/CamShift.cc index bcf00ce..638bddc 100644 --- a/src/CamShift.cc +++ b/src/CamShift.cc @@ -2,6 +2,8 @@ #include "OpenCV.h" #include "Matrix.h" +#ifdef HAVE_OPENCV_VIDEO + #if CV_MAJOR_VERSION >= 3 #include #endif @@ -176,3 +178,5 @@ NAN_METHOD(TrackedObject::Track) { info.GetReturnValue().Set(arr); } + +#endif diff --git a/src/CamShift.h b/src/CamShift.h index 7524408..af1bde9 100644 --- a/src/CamShift.h +++ b/src/CamShift.h @@ -1,5 +1,6 @@ #include "OpenCV.h" +#ifdef HAVE_OPENCV_VIDEO class TrackedObject: public Nan::ObjectWrap { public: @@ -20,3 +21,5 @@ public: JSFUNC(Track); }; + +#endif diff --git a/src/CascadeClassifierWrap.cc b/src/CascadeClassifierWrap.cc index b8c5a49..40c04a1 100755 --- a/src/CascadeClassifierWrap.cc +++ b/src/CascadeClassifierWrap.cc @@ -3,6 +3,8 @@ #include "Matrix.h" #include +#ifdef HAVE_OPENCV_OBJDETECT + Nan::Persistent CascadeClassifierWrap::constructor; void CascadeClassifierWrap::Init(Local target) { @@ -54,7 +56,7 @@ public: minw(minw), minh(minh) { } - + ~AsyncDetectMultiScale() { } @@ -149,3 +151,5 @@ NAN_METHOD(CascadeClassifierWrap::DetectMultiScale) { neighbors, minw, minh)); return; } + +#endif diff --git a/src/CascadeClassifierWrap.h b/src/CascadeClassifierWrap.h index d21c370..dee320f 100755 --- a/src/CascadeClassifierWrap.h +++ b/src/CascadeClassifierWrap.h @@ -1,6 +1,9 @@ #include "OpenCV.h" + +#ifdef HAVE_OPENCV_OBJDETECT + #if CV_MAJOR_VERSION >= 3 -#include + #include #endif class CascadeClassifierWrap: public Nan::ObjectWrap { @@ -20,3 +23,5 @@ public: static void EIO_DetectMultiScale(uv_work_t *req); static int EIO_AfterDetectMultiScale(uv_work_t *req); }; + +#endif diff --git a/src/Features2d.cc b/src/Features2d.cc index fa8f1cb..f2b75cb 100644 --- a/src/Features2d.cc +++ b/src/Features2d.cc @@ -6,6 +6,8 @@ #include #include +#ifdef HAVE_OPENCV_FEATURES2D + void Features::Init(Local target) { Nan::HandleScope scope; @@ -112,3 +114,4 @@ NAN_METHOD(Features::Similarity) { } #endif +#endif diff --git a/src/Features2d.h b/src/Features2d.h index e793f86..3d02977 100644 --- a/src/Features2d.h +++ b/src/Features2d.h @@ -2,6 +2,8 @@ #if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) +#ifdef HAVE_OPENCV_FEATURES2D + #include #include @@ -14,3 +16,5 @@ public: }; #endif + +#endif diff --git a/src/HighGUI.cc b/src/HighGUI.cc index b61b1fa..139fe94 100644 --- a/src/HighGUI.cc +++ b/src/HighGUI.cc @@ -2,6 +2,8 @@ #include "OpenCV.h" #include "Matrix.h" +#ifdef HAVE_OPENCV_HIGHGUI + Nan::Persistent NamedWindow::constructor; void NamedWindow::Init(Local target) { @@ -82,3 +84,5 @@ NAN_METHOD(NamedWindow::BlockingWaitKey) { info.GetReturnValue().Set(Nan::New(res)); } + +#endif diff --git a/src/HighGUI.h b/src/HighGUI.h index d47bc4b..3994892 100644 --- a/src/HighGUI.h +++ b/src/HighGUI.h @@ -1,5 +1,7 @@ #include "OpenCV.h" +#ifdef HAVE_OPENCV_HIGHGUI + class NamedWindow: public Nan::ObjectWrap { public: std::string winname; @@ -17,3 +19,5 @@ public: ; }; + +#endif diff --git a/src/ImgProc.cc b/src/ImgProc.cc index f181683..4dc4efb 100644 --- a/src/ImgProc.cc +++ b/src/ImgProc.cc @@ -1,6 +1,8 @@ #include "ImgProc.h" #include "Matrix.h" +#ifdef HAVE_OPENCV_IMGPROC + void ImgProc::Init(Local target) { Nan::Persistent inner; Local obj = Nan::New(); @@ -233,3 +235,5 @@ NAN_METHOD(ImgProc::GetStructuringElement) { return; } } + +#endif diff --git a/src/ImgProc.h b/src/ImgProc.h index 8754fdd..ca83782 100644 --- a/src/ImgProc.h +++ b/src/ImgProc.h @@ -3,6 +3,8 @@ #include "OpenCV.h" +#ifdef HAVE_OPENCV_IMGPROC + /** * Implementation of imgproc.hpp functions */ @@ -17,3 +19,5 @@ public: }; #endif + +#endif diff --git a/src/OpenCV.h b/src/OpenCV.h index cc9a2b9..60739de 100755 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -10,22 +10,41 @@ #endif +#include +#if !((((CV_MAJOR_VERSION <= 2) && (CV_MINOR_VERSION <= 4)) && (CV_MINOR_VERSION < 13)) || ((CV_MAJOR_VERSION >= 3) && (CV_MINOR_VERSION < 1))) + #define INCLUDE_AVAILABLE_MODULES_ONLY +#endif #include #include #include #include #include -#include +#include + +#if ((CV_MAJOR_VERSION <= 2) && (CV_MINOR_VERSION <= 4) && (CV_MINOR_VERSION < 10)) #include -#if CV_MAJOR_VERSION >= 3 -#include -#include -#include -#include +#else +#include #endif -#if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4) && (CV_SUBMINOR_VERSION>=4)) -#define HAVE_OPENCV_FACE + +#ifndef INCLUDE_AVAILABLE_MODULES_ONLY + #define HAVE_OPENCV_CALIB3D + #define HAVE_OPENCV_FEATURES2D + #define HAVE_OPENCV_FLANN + #define HAVE_OPENCV_HIGHGUI + // #define HAVE_OPENCV_IMGCODECS + #define HAVE_OPENCV_IMGPROC + #define HAVE_OPENCV_ML + #define HAVE_OPENCV_OBJDETECT + #define HAVE_OPENCV_PHOTO + #define HAVE_OPENCV_SHAPE + #define HAVE_OPENCV_STITCHING + #define HAVE_OPENCV_SUPERRES + #define HAVE_OPENCV_VIDEO + #define HAVE_OPENCV_VIDEOIO + #define HAVE_OPENCV_VIDEOSTAB + #define HAVE_OPENCV_VIZ #endif #include diff --git a/src/VideoCaptureWrap.cc b/src/VideoCaptureWrap.cc index 6b24d65..30113b7 100755 --- a/src/VideoCaptureWrap.cc +++ b/src/VideoCaptureWrap.cc @@ -4,6 +4,8 @@ #include +#ifdef HAVE_OPENCV_VIDEOIO + Nan::Persistent VideoCaptureWrap::constructor; struct videocapture_baton { @@ -311,3 +313,5 @@ NAN_METHOD(VideoCaptureWrap::Retrieve) { return; } + +#endif diff --git a/src/VideoCaptureWrap.h b/src/VideoCaptureWrap.h index a8a2dc2..bb7d11f 100755 --- a/src/VideoCaptureWrap.h +++ b/src/VideoCaptureWrap.h @@ -1,5 +1,7 @@ #include "OpenCV.h" +#ifdef HAVE_OPENCV_VIDEOIO + class VideoCaptureWrap: public Nan::ObjectWrap { public: cv::VideoCapture cap; @@ -36,3 +38,5 @@ public: // release the stream static NAN_METHOD(Release); }; + +#endif diff --git a/src/init.cc b/src/init.cc index ffbc286..453d076 100755 --- a/src/init.cc +++ b/src/init.cc @@ -24,23 +24,39 @@ extern "C" void init(Local target) { Point::Init(target); Matrix::Init(target); +#ifdef HAVE_OPENCV_OBJDETECT CascadeClassifierWrap::Init(target); +#endif +#ifdef HAVE_OPENCV_VIDEOIO VideoCaptureWrap::Init(target); VideoWriterWrap::Init(target); +#endif Contour::Init(target); +#ifdef HAVE_OPENCV_VIDEO TrackedObject::Init(target); +#endif +#ifdef HAVE_OPENCV_HIGHGUI NamedWindow::Init(target); +#endif Constants::Init(target); +#ifdef HAVE_OPENCV_CALIB3D Calib3D::Init(target); +#endif +#ifdef HAVE_OPENCV_IMGPROC ImgProc::Init(target); Histogram::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 - BackgroundSubtractorWrap::Init(target); - Features::Init(target); + #ifdef HAVE_OPENCV_VIDEO + BackgroundSubtractorWrap::Init(target); + #endif + #ifdef HAVE_OPENCV_FEATURES2D + Features::Init(target); + #endif LDAWrap::Init(target); #endif #endif