Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit ffccccf

Browse files
Merge pull request justadudewhohacks#395 from taeyoonwf/master
Point2[] to Contour, number[][] to Contour
2 parents adea9c0 + ba2ef9e commit ffccccf

File tree

4 files changed

+51
-13
lines changed

4 files changed

+51
-13
lines changed

cc/modules/imgproc/Contour.cc

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77

88
Nan::Persistent<v8::FunctionTemplate> Contour::constructor;
99

10-
void Contour::Init() {
11-
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Contour::New);
12-
constructor.Reset(ctor);
13-
ctor->InstanceTemplate()->SetInternalFieldCount(1);
14-
ctor->SetClassName(Nan::New("Contour").ToLocalChecked());
10+
NAN_MODULE_INIT(Contour::Init) {
11+
v8::Local<v8::FunctionTemplate> ctor = Nan::New<v8::FunctionTemplate>(Contour::New);
12+
constructor.Reset(ctor);
13+
ctor->InstanceTemplate()->SetInternalFieldCount(1);
14+
ctor->SetClassName(Nan::New("Contour").ToLocalChecked());
1515

1616
Nan::SetAccessor(ctor->InstanceTemplate(), FF_NEW_STRING("isConvex"), GetIsConvex);
1717
Nan::SetAccessor(ctor->InstanceTemplate(), FF_NEW_STRING("area"), GetArea);
@@ -33,8 +33,48 @@ void Contour::Init() {
3333
Nan::SetPrototypeMethod(ctor, "matchShapes", MatchShapes);
3434
Nan::SetPrototypeMethod(ctor, "fitEllipse", FitEllipse);
3535
Nan::SetPrototypeMethod(ctor, "moments", _Moments);
36+
37+
target->Set(Nan::New("Contour").ToLocalChecked(), ctor->GetFunction());
3638
};
3739

40+
NAN_METHOD(Contour::New) {
41+
if (info.Length() > 1) {
42+
return Nan::ThrowError("Contour::New - expected one or zero argument");
43+
}
44+
if (info.Length() == 1 && !info[0]->IsArray()) {
45+
return Nan::ThrowError("Contour::New - expected arg0 to be an array");
46+
}
47+
48+
Contour* self = new Contour();
49+
if (info.Length() == 1) {
50+
FF_ARR jsPts = FF_ARR::Cast(info[0]);
51+
self->contour.reserve(jsPts->Length());
52+
for (int i = 0; i < jsPts->Length(); i++) {
53+
cv::Point2d cv_pt;
54+
auto jsPt = jsPts->Get(i);
55+
if (jsPt->IsArray()) {
56+
FF_ARR jsObj = FF_ARR::Cast(jsPt);
57+
if (jsObj->Length() != 2)
58+
return Nan::ThrowError("Contour::New - expected arg0 to consist of only Point2 or array of length 2");
59+
double x = jsObj->Get(0)->NumberValue();
60+
double y = jsObj->Get(1)->NumberValue();
61+
cv_pt = cv::Point2d(x, y);
62+
}
63+
else if (FF_IS_INSTANCE(Point2::constructor, jsPt)) {
64+
FF_OBJ jsObj = FF_CAST_OBJ(jsPt);
65+
cv_pt = FF_UNWRAP_PT2_AND_GET(jsObj);
66+
}
67+
else {
68+
return Nan::ThrowError("Contour::New - expected arg0 to consist of only Point2 or array of length 2");
69+
}
70+
self->contour.emplace_back(cv::Point2i(cv_pt.x, cv_pt.y));
71+
}
72+
}
73+
self->hierarchy = cv::Vec4i(-1, -1, -1, -1);
74+
self->Wrap(info.Holder());
75+
info.GetReturnValue().Set(info.Holder());
76+
}
77+
3878
NAN_METHOD(Contour::GetPoints) {
3979
info.GetReturnValue().Set(Point::packJSPoint2Array(FF_UNWRAP_CONTOUR_AND_GET(info.This())));
4080
}

cc/modules/imgproc/Contour.h

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,8 @@ class Contour: public Nan::ObjectWrap {
1111
std::vector<cv::Point2i> contour;
1212
cv::Vec4i hierarchy;
1313

14-
static void Init();
15-
16-
static NAN_METHOD(New) {
17-
Contour* self = new Contour();
18-
self->Wrap(info.Holder());
19-
info.GetReturnValue().Set(info.Holder());
20-
}
14+
static NAN_MODULE_INIT(Init);
15+
static NAN_METHOD(New);
2116

2217
static NAN_GETTER(GetNumPoints) {
2318
info.GetReturnValue().Set(Nan::New((uint)FF_UNWRAP_CONTOUR_AND_GET(info.This()).size()));

cc/modules/imgproc/imgproc.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ NAN_MODULE_INIT(Imgproc::Init) {
3333
Nan::SetMethod(target, "canny", Canny);
3434
#endif
3535
Moments::Init(target);
36-
Contour::Init();
36+
Contour::Init(target);
3737
};
3838

3939
NAN_METHOD(Imgproc::GetStructuringElement) {

lib/typings/Contour.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ export class Contour {
99
readonly area: number;
1010
readonly isConvex: boolean;
1111
readonly hierarchy: Vec4;
12+
constructor();
13+
constructor(pts: Point2[]);
14+
constructor(pts: number[][]);
1215
approxPolyDP(epsilon: number, closed: boolean): Point2[];
1316
approxPolyDPContour(epsilon: number, closed: boolean): Contour;
1417
arcLength(closed?: boolean): number;

0 commit comments

Comments
 (0)