diff --git a/binding.gyp b/binding.gyp index aa089a6..6e56fc9 100755 --- a/binding.gyp +++ b/binding.gyp @@ -14,6 +14,7 @@ , "src/FaceRecognizer.cc" , "src/BackgroundSubtractor.cc" , "src/Constants.cc" + , "src/Calib3D.cc" ] , 'libraries': [ ' target) +{ + Persistent inner; + Local obj = NanNew(); + NanAssignPersistent(inner, obj); + + NODE_SET_METHOD(obj, "findChessboardCorners", FindChessboardCorners); + + target->Set(NanNew("calib3d"), obj); +} + +// cv::findChessboardCorners +NAN_METHOD(Calib3D::FindChessboardCorners) +{ + NanEscapableScope(); + + try { + // Get the arguments from javascript + + // Arg 0 is the image + Matrix* m = ObjectWrap::Unwrap(args[0]->ToObject()); + cv::Mat mat = m->mat; + + // Arg 1 is the pattern size + cv::Size patternSize; + if (args[1]->IsArray()) { + Local v8sz = args[1]->ToObject(); + + patternSize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue()); + } else { + JSTHROW_TYPE("Must pass pattern size"); + } + + // Arg 2 would normally be the flags, ignoring this for now and using the default flags + + // Final argument is the callback function + REQ_FUN_ARG(2, cb); + + // Find the corners + std::vector corners; + bool found = cv::findChessboardCorners(mat, patternSize, corners); + + // Make the callback arguments + Local argv[2]; + + argv[0] = NanNull(); + argv[1] = NanNull(); // This will be replaced by the corners array if corners were found + + // Further processing if we found corners + Local cornersArray; + if(found) + { + // Convert the return value to a javascript array + cornersArray = Array::New(corners.size()); + for(unsigned int i = 0; i < corners.size(); i++) + { + Local point_data = NanNew(); + point_data->Set(NanNew("x"), NanNew(corners[i].x)); + point_data->Set(NanNew("y"), NanNew(corners[i].y)); + + cornersArray->Set(Number::New(i), point_data); + } + + argv[1] = cornersArray; + } + + // Callback + TryCatch try_catch; + + cb->Call(NanGetCurrentContext()->Global(), 2, argv); + + if(try_catch.HasCaught()) { + FatalException(try_catch); + } + + NanReturnUndefined(); + + + } catch (cv::Exception &e) { + const char *err_msg = e.what(); + NanThrowError(err_msg); + NanReturnUndefined(); + } + +}; diff --git a/src/Calib3D.h b/src/Calib3D.h new file mode 100644 index 0000000..e8bba46 --- /dev/null +++ b/src/Calib3D.h @@ -0,0 +1,15 @@ +#ifndef __NODE_CALIB3D_H +#define __NODE_CALIB3D_H + +#include "OpenCV.h" + +// Implementation of calib3d.hpp functions + +class Calib3D: public node::ObjectWrap { +public: + static void Init(Handle target); + + static NAN_METHOD(FindChessboardCorners); +}; + +#endif diff --git a/src/OpenCV.cc b/src/OpenCV.cc index 24f594b..6ebdf01 100755 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -12,8 +12,6 @@ OpenCV::Init(Handle target) { target->Set(NanNew("version"), NanNew(out, n)); NODE_SET_METHOD(target, "readImage", ReadImage); - NODE_SET_METHOD(target, "findChessboardCorners", FindChessboardCorners); - } @@ -76,76 +74,3 @@ NAN_METHOD(OpenCV::ReadImage) { NanReturnUndefined(); } }; - -NAN_METHOD(OpenCV::FindChessboardCorners) { - NanEscapableScope(); - - try { - // Get the arguments from javascript - - // Arg 0 is the image - Matrix* m = ObjectWrap::Unwrap(args[0]->ToObject()); - cv::Mat mat = m->mat; - - // Arg 1 is the pattern size - cv::Size patternSize; - if (args[1]->IsArray()) { - Local v8sz = args[1]->ToObject(); - - patternSize = cv::Size(v8sz->Get(0)->IntegerValue(), v8sz->Get(1)->IntegerValue()); - } else { - JSTHROW_TYPE("Must pass pattern size"); - } - - // Arg 2 would normally be the flags, ignoring this for now and using the default flags - - // Final argument is the callback function - REQ_FUN_ARG(2, cb); - - // Find the corners - std::vector corners; - bool found = cv::findChessboardCorners(mat, patternSize, corners); - - // Make the callback arguments - Local argv[2]; - - argv[0] = NanNull(); - argv[1] = NanNull(); // This will be replaced by the corners array if corners were found - - // Further processing if we found corners - Local cornersArray; - if(found) - { - // Convert the return value to a javascript array - cornersArray = Array::New(corners.size()); - for(int i = 0; i < corners.size(); i++) - { - Local point_data = NanNew(); - point_data->Set(NanNew("x"), NanNew(corners[i].x)); - point_data->Set(NanNew("y"), NanNew(corners[i].y)); - - cornersArray->Set(Number::New(i), point_data); - } - - argv[1] = cornersArray; - } - - // Callback - TryCatch try_catch; - - cb->Call(NanGetCurrentContext()->Global(), 2, argv); - - if(try_catch.HasCaught()) { - FatalException(try_catch); - } - - NanReturnUndefined(); - - - } catch (cv::Exception &e) { - const char *err_msg = e.what(); - NanThrowError(err_msg); - NanReturnUndefined(); - } - -}; diff --git a/src/OpenCV.h b/src/OpenCV.h index b8a53ab..2dc32e5 100755 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -50,8 +50,6 @@ class OpenCV: public node::ObjectWrap{ static void Init(Handle target); static NAN_METHOD(ReadImage); - - static NAN_METHOD(FindChessboardCorners); }; diff --git a/src/init.cc b/src/init.cc index eb8b79a..2486e07 100755 --- a/src/init.cc +++ b/src/init.cc @@ -9,21 +9,22 @@ #include "HighGUI.h" #include "FaceRecognizer.h" #include "Constants.h" - +#include "Calib3D.h" extern "C" void init(Handle target) { NanScope(); OpenCV::Init(target); - + Point::Init(target); Matrix::Init(target); CascadeClassifierWrap::Init(target); VideoCaptureWrap::Init(target); Contour::Init(target); - TrackedObject::Init(target); + TrackedObject::Init(target); NamedWindow::Init(target); Constants::Init(target); + Calib3D::Init(target); #if CV_MAJOR_VERSION >= 2 && CV_MINOR_VERSION >=4