forked from peterbraden/node-opencv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Contours.cc
executable file
·244 lines (169 loc) · 6.72 KB
/
Contours.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include "Contours.h"
#include "OpenCV.h"
#include <iostream>
v8::Persistent<FunctionTemplate> Contour::constructor;
void
Contour::Init(Handle<Object> target) {
HandleScope scope;
//Class
v8::Local<v8::FunctionTemplate> m = v8::FunctionTemplate::New(New);
m->SetClassName(v8::String::NewSymbol("Contours"));
// Constructor
constructor = Persistent<FunctionTemplate>::New(m);
constructor->InstanceTemplate()->SetInternalFieldCount(1);
constructor->SetClassName(String::NewSymbol("Contours"));
// Prototype
//Local<ObjectTemplate> proto = constructor->PrototypeTemplate();
NODE_SET_PROTOTYPE_METHOD(constructor, "point", Point);
NODE_SET_PROTOTYPE_METHOD(constructor, "size", Size);
NODE_SET_PROTOTYPE_METHOD(constructor, "cornerCount", CornerCount);
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, "convexHull", ConvexHull);
NODE_SET_PROTOTYPE_METHOD(constructor, "boundingRect", BoundingRect);
NODE_SET_PROTOTYPE_METHOD(constructor, "minAreaRect", BoundingRect);
NODE_SET_PROTOTYPE_METHOD(constructor, "isConvex", IsConvex);
NODE_SET_PROTOTYPE_METHOD(constructor, "moments", Moments);
target->Set(String::NewSymbol("Contours"), m->GetFunction());
};
Handle<Value>
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<Value>
Contour::Point(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
int index = args[1]->NumberValue();
cv::Point point = self->contours[pos][index];
Local<Object> data = Object::New();
data->Set(String::NewSymbol("x"), Number::New(point.x));
data->Set(String::NewSymbol("y"), Number::New(point.y));
return scope.Close(data);
}
// FIXME: this sould better be called "Length" as ``Contours`` is an Array like structure
// also, this would allow to use ``Size`` for the function returning the number of corners
// in the contour for better consistency with OpenCV.
Handle<Value>
Contour::Size(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
return scope.Close(Number::New(self->contours.size()));
}
Handle<Value>
Contour::CornerCount(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
return scope.Close(Number::New(self->contours[pos].size()));
}
Handle<Value>
Contour::Area(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(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<Value>
Contour::ArcLength(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(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<Value>
Contour::ApproxPolyDP(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
double 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<Value>
Contour::ConvexHull(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
bool clockwise = args[1]->BooleanValue();
cv::Mat hull;
cv::convexHull(cv::Mat(self->contours[pos]), hull, clockwise);
hull.copyTo(self->contours[pos]);
return scope.Close(v8::Null());
}
Handle<Value>
Contour::BoundingRect(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
cv::Rect bounding = cv::boundingRect(cv::Mat(self->contours[pos]));
Local<Object> rect = Object::New();
rect->Set(String::NewSymbol("x"), Number::New(bounding.x));
rect->Set(String::NewSymbol("y"), Number::New(bounding.y));
rect->Set(String::NewSymbol("width"), Number::New(bounding.width));
rect->Set(String::NewSymbol("height"), Number::New(bounding.height));
return scope.Close(rect);
}
Handle<Value>
Contour::MinAreaRect(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
cv::RotatedRect minimum = cv::minAreaRect(cv::Mat(self->contours[pos]));
Local<Object> rect = Object::New();
rect->Set(String::NewSymbol("angle"), Number::New(minimum.angle));
Local<Object> size = Object::New();
size->Set(String::NewSymbol("height"), Number::New(minimum.size.height));
size->Set(String::NewSymbol("width"), Number::New(minimum.size.width));
rect->Set(String::NewSymbol("size"), size);
Local<Object> center = Object::New();
center->Set(String::NewSymbol("x"), Number::New(minimum.center.x));
center->Set(String::NewSymbol("y"), Number::New(minimum.center.y));
v8::Local<v8::Array> points = v8::Array::New(4);
cv::Point2f rect_points[4];
minimum.points(rect_points);
for (unsigned int i=0; i<4; i++){
Local<Object> point = Object::New();
point->Set(String::NewSymbol("x"), Number::New(rect_points[i].x));
point->Set(String::NewSymbol("y"), Number::New(rect_points[i].y));
points->Set(i, point);
}
rect->Set(String::NewSymbol("points"), points);
return scope.Close(rect);
}
Handle<Value>
Contour::IsConvex(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
return scope.Close(Boolean::New(isContourConvex(cv::Mat(self->contours[pos]))));
}
Handle<Value>
Contour::Moments(const Arguments &args) {
HandleScope scope;
Contour *self = ObjectWrap::Unwrap<Contour>(args.This());
int pos = args[0]->NumberValue();
/// Get the moments
cv::Moments mu = moments( self->contours[pos], false );
Local<Object> res = Object::New();
res->Set(String::NewSymbol("m00"), Number::New(mu.m00));
res->Set(String::NewSymbol("m10"), Number::New(mu.m10));
res->Set(String::NewSymbol("m01"), Number::New(mu.m01));
res->Set(String::NewSymbol("m11"), Number::New(mu.m11));
return scope.Close(res);
}