#include "Contours.h" #include "OpenCV.h" #include v8::Persistent Contour::constructor; void Contour::Init(Handle target) { HandleScope scope; //Class v8::Local m = v8::FunctionTemplate::New(New); m->SetClassName(v8::String::NewSymbol("Contours")); // Constructor constructor = Persistent::New(m); constructor->InstanceTemplate()->SetInternalFieldCount(1); constructor->SetClassName(String::NewSymbol("Contours")); // Prototype //Local proto = constructor->PrototypeTemplate(); NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size); 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, "isConvex", IsConvex); target->Set(String::NewSymbol("Contours"), m->GetFunction()); }; Handle Contour::New(const Arguments &args) { HandleScope scope; if (args.This()->InternalFieldCount() == 0) return v8::ThrowException(v8::Exception::TypeError(v8::String::New("Cannot instantiate without new"))); Contour *contours; contours = new Contour; contours->Wrap(args.Holder()); return scope.Close(args.Holder()); } Contour::Contour(): ObjectWrap() { } Handle Contour::Size(const Arguments &args) { HandleScope scope; Contour *self = ObjectWrap::Unwrap(args.This()); return scope.Close(Number::New(self->contours.size())); } Handle Contour::Area(const Arguments &args) { HandleScope scope; Contour *self = ObjectWrap::Unwrap(args.This()); int pos = args[0]->NumberValue(); //return scope.Close(Number::New(contourArea(self->contours))); return scope.Close(Number::New(contourArea(cv::Mat(self->contours[pos])))); } Handle Contour::ArcLength(const Arguments &args) { HandleScope scope; Contour *self = ObjectWrap::Unwrap(args.This()); int pos = args[0]->NumberValue(); bool isClosed = args[1]->BooleanValue(); return scope.Close(Number::New(arcLength(cv::Mat(self->contours[pos]), isClosed))); } Handle Contour::ApproxPolyDP(const Arguments &args) { HandleScope scope; Contour *self = ObjectWrap::Unwrap(args.This()); int pos = args[0]->NumberValue(); bool epsilon = args[1]->NumberValue(); bool isClosed = args[2]->BooleanValue(); cv::Mat approxed; approxPolyDP(cv::Mat(self->contours[pos]), approxed, epsilon, isClosed); approxed.copyTo(self->contours[pos]); return scope.Close(v8::Null()); } Handle Contour::IsConvex(const Arguments &args) { HandleScope scope; Contour *self = ObjectWrap::Unwrap(args.This()); int pos = args[0]->NumberValue(); return scope.Close(Boolean::New(isContourConvex(cv::Mat(self->contours[pos])))); }