From a1975866737743f04a87db654446155d7f70a139 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sun, 10 Feb 2013 14:58:06 -0800 Subject: [PATCH 1/3] Expose point data from Contours Add in a Point method to the Contours class to access the underlying point coordinates from the JavaScript side. --- src/Contours.cc | 18 ++++++++++++++++++ src/Contours.h | 1 + 2 files changed, 19 insertions(+) diff --git a/src/Contours.cc b/src/Contours.cc index c5818e0..a3cd6a0 100755 --- a/src/Contours.cc +++ b/src/Contours.cc @@ -23,6 +23,7 @@ Contour::Init(Handle target) { //Local proto = constructor->PrototypeTemplate(); + NODE_SET_PROTOTYPE_METHOD(constructor, "point", Point); NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size); NODE_SET_PROTOTYPE_METHOD(constructor, "cornerCount", CornerCount); NODE_SET_PROTOTYPE_METHOD(constructor, "area", Area); @@ -53,6 +54,23 @@ Contour::Contour(): ObjectWrap() { } +Handle +Contour::Point(const Arguments &args) { + HandleScope scope; + + Contour *self = ObjectWap::Unwrap(args.This()); + int pos = args[0]->NumberValue(); + int index = args[1]->NumberValue(); + + cv::Point point = self->contours[pos][index]; + + Local data = Object::New(); + data->Set(String::NewSymbol("x"), Number::New(point.x)); + data->Set(String::NewSymbol("y"), Number::New(point.y)); + + return scope.Close(data); +} + // FIXME: this sould better be called "Length" as ``Contours`` is an Array like structure // also, this would allow to use ``Size`` for the function returning the number of corners // in the contour for better consistency with OpenCV. diff --git a/src/Contours.h b/src/Contours.h index 259503a..0d69726 100755 --- a/src/Contours.h +++ b/src/Contours.h @@ -14,6 +14,7 @@ class Contour: public node::ObjectWrap { Contour(); //JSFUNC(Size) + static Handle Point(const v8::Arguments&); static Handle Size(const v8::Arguments&); static Handle CornerCount(const v8::Arguments&); static Handle Area(const v8::Arguments&); From 4f7968e56748ba1da176f27f368ec13aff4eee56 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sun, 10 Feb 2013 15:11:59 -0800 Subject: [PATCH 2/3] Expose MinAreaRect binding for Contours --- src/Contours.cc | 42 +++++++++++++++++++++++++++++++++++++++++- src/Contours.h | 1 + 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/src/Contours.cc b/src/Contours.cc index a3cd6a0..e702abc 100755 --- a/src/Contours.cc +++ b/src/Contours.cc @@ -30,6 +30,7 @@ Contour::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor, "arcLength", ArcLength); NODE_SET_PROTOTYPE_METHOD(constructor, "approxPolyDP", ApproxPolyDP); NODE_SET_PROTOTYPE_METHOD(constructor, "boundingRect", BoundingRect); + NODE_SET_PROTOTYPE_METHOD(constructor, "minAreaRect", BoundingRect); NODE_SET_PROTOTYPE_METHOD(constructor, "isConvex", IsConvex); target->Set(String::NewSymbol("Contours"), m->GetFunction()); }; @@ -58,7 +59,7 @@ Handle Contour::Point(const Arguments &args) { HandleScope scope; - Contour *self = ObjectWap::Unwrap(args.This()); + Contour *self = ObjectWrap::Unwrap(args.This()); int pos = args[0]->NumberValue(); int index = args[1]->NumberValue(); @@ -153,6 +154,45 @@ Contour::BoundingRect(const Arguments &args) { } +Handle +Contour::MinAreaRect(const Arguments &args) { + HandleScope scope; + + Contour *self = ObjectWrap::Unwrap(args.This()); + int pos = args[0]->NumberValue(); + + cv::RotatedRect minimum = cv::minAreaRect(cv::Mat(self->contours[pos])); + + Local rect = Object::New(); + rect->Set(String::NewSymbol("angle"), Number::New(minimum.angle)); + + Local size = Object::New(); + size->Set(String::NewSymbol("height"), Number::New(minimum.size.height)); + size->Set(String::NewSymbol("width"), Number::New(minimum.size.width)); + rect->Set(String::NewSymbol("size"), size); + + Local center = Object::New(); + center->Set(String::NewSymbol("x"), Number::New(minimum.center.x)); + center->Set(String::NewSymbol("y"), Number::New(minimum.center.y)); + + v8::Local points = v8::Array::New(4); + + cv::Point2f rect_points[4]; + minimum.points(rect_points); + + for (unsigned int i=0; i<4; i++){ + Local point = Object::New(); + point->Set(String::NewSymbol("x"), Number::New(rect_points[i].x)); + point->Set(String::NewSymbol("y"), Number::New(rect_points[i].y)); + points->Set(i, point); + } + + rect->Set(String::NewSymbol("points"), points); + + return scope.Close(rect); +} + + Handle Contour::IsConvex(const Arguments &args) { HandleScope scope; diff --git a/src/Contours.h b/src/Contours.h index 0d69726..d8b3fa4 100755 --- a/src/Contours.h +++ b/src/Contours.h @@ -21,6 +21,7 @@ class Contour: public node::ObjectWrap { static Handle ArcLength(const v8::Arguments&); static Handle ApproxPolyDP(const v8::Arguments&); static Handle BoundingRect(const v8::Arguments&); + static Handle MinAreaRect(const v8::Arguments&); static Handle IsConvex(const v8::Arguments&); }; From 84e5f255df1f1f07324ed72c5a4588ecb65893a0 Mon Sep 17 00:00:00 2001 From: Michael Smith Date: Sun, 10 Feb 2013 15:03:13 -0800 Subject: [PATCH 3/3] Add ConvexHull binding for Contours class --- src/Contours.cc | 18 ++++++++++++++++++ src/Contours.h | 1 + 2 files changed, 19 insertions(+) diff --git a/src/Contours.cc b/src/Contours.cc index e702abc..c586b3d 100755 --- a/src/Contours.cc +++ b/src/Contours.cc @@ -29,6 +29,7 @@ Contour::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(constructor, "area", Area); NODE_SET_PROTOTYPE_METHOD(constructor, "arcLength", ArcLength); NODE_SET_PROTOTYPE_METHOD(constructor, "approxPolyDP", ApproxPolyDP); + NODE_SET_PROTOTYPE_METHOD(constructor, "convexHull", ConvexHull); NODE_SET_PROTOTYPE_METHOD(constructor, "boundingRect", BoundingRect); NODE_SET_PROTOTYPE_METHOD(constructor, "minAreaRect", BoundingRect); NODE_SET_PROTOTYPE_METHOD(constructor, "isConvex", IsConvex); @@ -135,6 +136,23 @@ Contour::ApproxPolyDP(const Arguments &args) { } +Handle +Contour::ConvexHull(const Arguments &args) { + HandleScope scope; + + Contour *self = ObjectWrap::Unwrap(args.This()); + + int pos = args[0]->NumberValue(); + bool clockwise = args[1]->BooleanValue(); + + cv::Mat hull; + cv::convexHull(cv::Mat(self->contours[pos]), hull, clockwise); + hull.copyTo(self->contours[pos]); + + return scope.Close(v8::Null()); +} + + Handle Contour::BoundingRect(const Arguments &args) { HandleScope scope; diff --git a/src/Contours.h b/src/Contours.h index d8b3fa4..c0f8879 100755 --- a/src/Contours.h +++ b/src/Contours.h @@ -20,6 +20,7 @@ class Contour: public node::ObjectWrap { static Handle Area(const v8::Arguments&); static Handle ArcLength(const v8::Arguments&); static Handle ApproxPolyDP(const v8::Arguments&); + static Handle ConvexHull(const v8::Arguments&); static Handle BoundingRect(const v8::Arguments&); static Handle MinAreaRect(const v8::Arguments&); static Handle IsConvex(const v8::Arguments&);