From 97715a8048aa8be714451d578cc29cbf0190eff6 Mon Sep 17 00:00:00 2001 From: Max Ehrlich Date: Mon, 19 Jan 2015 15:15:05 -0500 Subject: [PATCH] Added findChessboardCorners function --- src/OpenCV.cc | 94 +++++++++++++++++++++++++++++++++++++++++++++------ src/OpenCV.h | 3 +- 2 files changed, 86 insertions(+), 11 deletions(-) diff --git a/src/OpenCV.cc b/src/OpenCV.cc index 4ed6c43..24f594b 100755 --- a/src/OpenCV.cc +++ b/src/OpenCV.cc @@ -5,48 +5,49 @@ void OpenCV::Init(Handle target) { NanScope(); - + // Version string. char out [21]; int n = sprintf(out, "%i.%i", CV_MAJOR_VERSION, CV_MINOR_VERSION); target->Set(NanNew("version"), NanNew(out, n)); NODE_SET_METHOD(target, "readImage", ReadImage); + NODE_SET_METHOD(target, "findChessboardCorners", FindChessboardCorners); -} +} NAN_METHOD(OpenCV::ReadImage) { NanEscapableScope(); try{ - + Local im_h = NanNew(Matrix::constructor)->GetFunction()->NewInstance(); Matrix *img = ObjectWrap::Unwrap(im_h); - + cv::Mat mat; REQ_FUN_ARG(1, cb); - + if (args[0]->IsNumber() && args[1]->IsNumber()){ int width, height; width = args[0]->Uint32Value(); - height = args[1]->Uint32Value(); + height = args[1]->Uint32Value(); mat = *(new cv::Mat(width, height, CV_64FC1)); } else if (args[0]->IsString()) { - + std::string filename = std::string(*NanAsciiString(args[0]->ToString())); mat = cv::imread(filename); } else if (Buffer::HasInstance(args[0])){ uint8_t *buf = (uint8_t *) Buffer::Data(args[0]->ToObject()); unsigned len = Buffer::Length(args[0]->ToObject()); - + cv::Mat *mbuf = new cv::Mat(len, 1, CV_64FC1, buf); mat = cv::imdecode(*mbuf, -1); - + if (mat.empty()){ NanThrowTypeError("Error loading file"); } @@ -74,4 +75,77 @@ NAN_METHOD(OpenCV::ReadImage) { NanThrowError(err_msg); 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 ea1ca83..b8a53ab 100755 --- a/src/OpenCV.h +++ b/src/OpenCV.h @@ -50,9 +50,10 @@ class OpenCV: public node::ObjectWrap{ static void Init(Handle target); static NAN_METHOD(ReadImage); + + static NAN_METHOD(FindChessboardCorners); }; #endif -