Added support for the fitEllipse method on contours

This commit is contained in:
Paul Morris-Hill 2015-06-13 02:24:03 +01:00
parent 585790f06e
commit 9a21b9ea86
2 changed files with 32 additions and 0 deletions

View File

@ -29,6 +29,7 @@ Contour::Init(Handle<Object> 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<Contour>(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<Object> jsEllipse = NanNew<Object>();
jsEllipse->Set(NanNew("angle"), NanNew<Number>(ellipse.angle));
Local<Object> size = NanNew<Object>();
size->Set(NanNew("height"), NanNew<Number>(ellipse.size.height));
size->Set(NanNew("width"), NanNew<Number>(ellipse.size.width));
jsEllipse->Set(NanNew("size"), size);
Local<Object> center = NanNew<Object>();
center->Set(NanNew("x"), NanNew<Number>(ellipse.center.x));
center->Set(NanNew("y"), NanNew<Number>(ellipse.center.y));
jsEllipse->Set(NanNew("center"), center);
NanReturnValue(jsEllipse);
}
NanReturnNull();
}
NAN_METHOD(Contour::IsConvex) {
NanScope();

View File

@ -25,6 +25,7 @@ class Contour: public node::ObjectWrap {
JSFUNC(ConvexHull)
JSFUNC(BoundingRect)
JSFUNC(MinAreaRect)
JSFUNC(FitEllipse)
JSFUNC(IsConvex)
JSFUNC(Moments)
JSFUNC(Hierarchy)