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

Commit 8d29f80

Browse files
Point2[] to Contour, number[][] to Contour
1 parent adea9c0 commit 8d29f80

File tree

4 files changed

+48
-13
lines changed

4 files changed

+48
-13
lines changed

cc/modules/imgproc/Contour.cc

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

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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ export class Contour {
99
readonly area: number;
1010
readonly isConvex: boolean;
1111
readonly hierarchy: Vec4;
12+
constructor(pts: Point2[]);
13+
constructor(pts: number[][]);
1214
approxPolyDP(epsilon: number, closed: boolean): Point2[];
1315
approxPolyDPContour(epsilon: number, closed: boolean): Contour;
1416
arcLength(closed?: boolean): number;

0 commit comments

Comments
 (0)