diff --git a/README.md b/README.md index 0dcd12a..c538350 100755 --- a/README.md +++ b/README.md @@ -194,6 +194,7 @@ contours.arcLength(index, isClosed); contours.boundingRect(index); contours.minAreaRect(index); contours.isConvex(index); +contours.fitEllipse(index); // Destructively alter contour `index` contours.approxPolyDP(index, epsilon, isClosed); diff --git a/src/Contours.cc b/src/Contours.cc index fe59d68..ddbbf5b 100755 --- a/src/Contours.cc +++ b/src/Contours.cc @@ -29,6 +29,7 @@ Contour::Init(Handle target) { NODE_SET_PROTOTYPE_METHOD(ctor, "convexHull", ConvexHull); NODE_SET_PROTOTYPE_METHOD(ctor, "boundingRect", BoundingRect); NODE_SET_PROTOTYPE_METHOD(ctor, "minAreaRect", MinAreaRect); + NODE_SET_PROTOTYPE_METHOD(ctor, "fitEllipse", FitEllipse); NODE_SET_PROTOTYPE_METHOD(ctor, "isConvex", IsConvex); NODE_SET_PROTOTYPE_METHOD(ctor, "moments", Moments); NODE_SET_PROTOTYPE_METHOD(ctor, "hierarchy", Hierarchy); @@ -224,6 +225,36 @@ NAN_METHOD(Contour::MinAreaRect) { } +NAN_METHOD(Contour::FitEllipse) { + NanScope(); + + Contour *self = ObjectWrap::Unwrap(args.This()); + int pos = args[0]->NumberValue(); + + if(self->contours[pos].size() >= 5){ //Minimum number for an ellipse + cv::RotatedRect ellipse = cv::fitEllipse(cv::Mat(self->contours[pos])); + + Local jsEllipse = NanNew(); + jsEllipse->Set(NanNew("angle"), NanNew(ellipse.angle)); + + Local size = NanNew(); + size->Set(NanNew("height"), NanNew(ellipse.size.height)); + size->Set(NanNew("width"), NanNew(ellipse.size.width)); + jsEllipse->Set(NanNew("size"), size); + + Local center = NanNew(); + center->Set(NanNew("x"), NanNew(ellipse.center.x)); + center->Set(NanNew("y"), NanNew(ellipse.center.y)); + jsEllipse->Set(NanNew("center"), center); + + NanReturnValue(jsEllipse); + } + + NanReturnNull(); +} + + + NAN_METHOD(Contour::IsConvex) { NanScope(); diff --git a/src/Contours.h b/src/Contours.h index 710485d..dd6c520 100755 --- a/src/Contours.h +++ b/src/Contours.h @@ -25,6 +25,7 @@ class Contour: public node::ObjectWrap { JSFUNC(ConvexHull) JSFUNC(BoundingRect) JSFUNC(MinAreaRect) + JSFUNC(FitEllipse) JSFUNC(IsConvex) JSFUNC(Moments) JSFUNC(Hierarchy)