Moved findChessboardCorners to calib3d source file

This commit is contained in:
Max Ehrlich 2015-01-20 09:37:52 -05:00
parent 97715a8048
commit e35698ac16
6 changed files with 108 additions and 80 deletions

View File

@ -14,6 +14,7 @@
, "src/FaceRecognizer.cc"
, "src/BackgroundSubtractor.cc"
, "src/Constants.cc"
, "src/Calib3D.cc"
]
, 'libraries': [
'<!@(pkg-config --libs opencv)'

88
src/Calib3D.cc Normal file
View File

@ -0,0 +1,88 @@
#include "Calib3D.h"
#include "Matrix.h"
void Calib3D::Init(Handle<Object> target)
{
Persistent<Object> inner;
Local<Object> obj = NanNew<Object>();
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<Matrix>(args[0]->ToObject());
cv::Mat mat = m->mat;
// Arg 1 is the pattern size
cv::Size patternSize;
if (args[1]->IsArray()) {
Local<Object> 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<cv::Point2f> corners;
bool found = cv::findChessboardCorners(mat, patternSize, corners);
// Make the callback arguments
Local<Value> 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<Array> 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<Object> point_data = NanNew<Object>();
point_data->Set(NanNew<String>("x"), NanNew<Number>(corners[i].x));
point_data->Set(NanNew<String>("y"), NanNew<Number>(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();
}
};

15
src/Calib3D.h Normal file
View File

@ -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<Object> target);
static NAN_METHOD(FindChessboardCorners);
};
#endif

View File

@ -12,8 +12,6 @@ OpenCV::Init(Handle<Object> target) {
target->Set(NanNew<String>("version"), NanNew<String>(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<Matrix>(args[0]->ToObject());
cv::Mat mat = m->mat;
// Arg 1 is the pattern size
cv::Size patternSize;
if (args[1]->IsArray()) {
Local<Object> 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<cv::Point2f> corners;
bool found = cv::findChessboardCorners(mat, patternSize, corners);
// Make the callback arguments
Local<Value> 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<Array> 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<Object> point_data = NanNew<Object>();
point_data->Set(NanNew<String>("x"), NanNew<Number>(corners[i].x));
point_data->Set(NanNew<String>("y"), NanNew<Number>(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();
}
};

View File

@ -50,8 +50,6 @@ class OpenCV: public node::ObjectWrap{
static void Init(Handle<Object> target);
static NAN_METHOD(ReadImage);
static NAN_METHOD(FindChessboardCorners);
};

View File

@ -9,21 +9,22 @@
#include "HighGUI.h"
#include "FaceRecognizer.h"
#include "Constants.h"
#include "Calib3D.h"
extern "C" void
init(Handle<Object> 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