#include "BackgroundSubtractor.h" #include "Matrix.h" #include #include #ifdef HAVE_OPENCV_VIDEO #if CV_MAJOR_VERSION >= 3 #warning TODO: port me to OpenCV 3 #endif #if ((CV_MAJOR_VERSION == 2) && (CV_MINOR_VERSION >=4)) Nan::Persistent BackgroundSubtractorWrap::constructor; void BackgroundSubtractorWrap::Init(Local target) { Nan::HandleScope scope; // Constructor Local ctor = Nan::New(BackgroundSubtractorWrap::New); constructor.Reset(ctor); ctor->InstanceTemplate()->SetInternalFieldCount(1); ctor->SetClassName(Nan::New("BackgroundSubtractor").ToLocalChecked()); Nan::SetMethod(ctor, "createMOG", CreateMOG); Nan::SetPrototypeMethod(ctor, "applyMOG", ApplyMOG); target->Set(Nan::New("BackgroundSubtractor").ToLocalChecked(), ctor->GetFunction()); } NAN_METHOD(BackgroundSubtractorWrap::New) { Nan::HandleScope scope; if (info.This()->InternalFieldCount() == 0) { JSTHROW_TYPE("Cannot Instantiate without new") } // Create MOG by default cv::Ptr bg; BackgroundSubtractorWrap *pt = new BackgroundSubtractorWrap(bg); pt->Wrap(info.This()); info.GetReturnValue().Set(info.This()); } NAN_METHOD(BackgroundSubtractorWrap::CreateMOG) { Nan::HandleScope scope; // int history = 200; // int nmixtures = 5; // double backgroundRatio = 0.7; // double noiseSigma = 0; // // if(info.Length() > 1){ // INT_FROM_ARGS(history, 0) // INT_FROM_ARGS(nmixtures, 1) // DOUBLE_FROM_ARGS(backgroundRatio, 2) // DOUBLE_FROM_ARGS(noiseSigma, 3) // } Local n = Nan::NewInstance(Nan::GetFunction(Nan::New(BackgroundSubtractorWrap::constructor)).ToLocalChecked()).ToLocalChecked(); cv::Ptr bg; BackgroundSubtractorWrap *pt = new BackgroundSubtractorWrap(bg); pt->Wrap(n); info.GetReturnValue().Set( n ); } // Fetch foreground mask NAN_METHOD(BackgroundSubtractorWrap::ApplyMOG) { SETUP_FUNCTION(BackgroundSubtractorWrap); REQ_FUN_ARG(1, cb); Local argv[2]; if (info.Length() == 0) { argv[0] = Nan::New("Input image missing").ToLocalChecked(); argv[1] = Nan::Null(); cb->Call(Nan::GetCurrentContext()->Global(), 2, argv); return; } try { Local fgMask = Nan::NewInstance(Nan::GetFunction(Nan::New(Matrix::constructor)).ToLocalChecked()).ToLocalChecked(); Matrix *img = Nan::ObjectWrap::Unwrap(fgMask); cv::Mat mat; if (Buffer::HasInstance(info[0])) { uint8_t *buf = (uint8_t *) Buffer::Data(info[0]->ToObject()); unsigned len = Buffer::Length(info[0]->ToObject()); cv::Mat *mbuf = new cv::Mat(len, 1, CV_64FC1, buf); mat = cv::imdecode(*mbuf, -1); //mbuf->release(); } else { Matrix *_img = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); mat = (_img->mat).clone(); } if (mat.empty()) { return Nan::ThrowTypeError("Error loading file"); } cv::Mat _fgMask; self->subtractor->operator()(mat, _fgMask); img->mat = _fgMask; mat.release(); argv[0] = Nan::Null(); argv[1] = fgMask; Nan::TryCatch try_catch; cb->Call(Nan::GetCurrentContext()->Global(), 2, argv); if (try_catch.HasCaught()) { Nan::FatalException(try_catch); } return; } catch (cv::Exception& e) { const char* err_msg = e.what(); Nan::ThrowError(err_msg); return; } } BackgroundSubtractorWrap::BackgroundSubtractorWrap( cv::Ptr _subtractor) { subtractor = _subtractor; } #endif #endif