From 088b44bdeb31d780defc23304e4cc0aac1ac1c30 Mon Sep 17 00:00:00 2001 From: Max Ehrlich Date: Tue, 20 Jan 2015 10:19:28 -0500 Subject: [PATCH] Added drawChessboardCorners --- src/Calib3D.cc | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/Calib3D.h | 2 ++ 2 files changed, 72 insertions(+) diff --git a/src/Calib3D.cc b/src/Calib3D.cc index 240c431..512b524 100644 --- a/src/Calib3D.cc +++ b/src/Calib3D.cc @@ -8,6 +8,7 @@ void Calib3D::Init(Handle target) NanAssignPersistent(inner, obj); NODE_SET_METHOD(obj, "findChessboardCorners", FindChessboardCorners); + NODE_SET_METHOD(obj, "drawChessboardCorners", DrawChessboardCorners); target->Set(NanNew("calib3d"), obj); } @@ -86,3 +87,72 @@ NAN_METHOD(Calib3D::FindChessboardCorners) } }; + +// cv::drawChessboardCorners +NAN_METHOD(Calib3D::DrawChessboardCorners) +{ + NanEscapableScope(); + + try { + // Get the arguments + + // 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 is the corners array + std::vector corners; + if(args[2]->IsArray()) { + Local cornersArray = Local::Cast(args[2]); + + for(unsigned int i = 0; i < cornersArray->Length(); i++) + { + Local pt = cornersArray->Get(i)->ToObject(); + corners.push_back(cv::Point2f(pt->Get(NanNew("x"))->ToNumber()->Value(), + pt->Get(NanNew("y"))->ToNumber()->Value())); + } + } else { + JSTHROW_TYPE("Must pass corners array"); + } + + // Arg 3, pattern found boolean + bool patternWasFound = args[3]->ToBoolean()->Value(); + + // Final argument is the callback + REQ_FUN_ARG(4, cb); + + // Draw the corners + cv::drawChessboardCorners(mat, patternSize, corners, patternWasFound); + + // Make the callback arguments (same image that was passed, now with corners drawn on it) + Local argv[2]; + argv[0] = NanNull(); + argv[1] = args[0]; + + // 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 index e8bba46..14ecc99 100644 --- a/src/Calib3D.h +++ b/src/Calib3D.h @@ -10,6 +10,8 @@ public: static void Init(Handle target); static NAN_METHOD(FindChessboardCorners); + + static NAN_METHOD(DrawChessboardCorners); }; #endif