-
Notifications
You must be signed in to change notification settings - Fork 831
Expand file tree
/
Copy pathimgprocBindings.h
More file actions
333 lines (284 loc) · 9.6 KB
/
Copy pathimgprocBindings.h
File metadata and controls
333 lines (284 loc) · 9.6 KB
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "imgproc.h"
#include "CvBinding.h"
#include <opencv2/imgproc.hpp>
#ifndef __FF_IMGPROCBINDINGS_H_
#define __FF_IMGPROCBINDINGS_H_
namespace ImgprocBindings {
struct GetTextSizeWorker : public CatchCvExceptionWorker {
public:
std::string text;
int fontFace;
double fontScale;
int thickness;
cv::Size2d returnValue;
int baseLine;
std::string executeCatchCvExceptionWorker() {
returnValue = cv::getTextSize(text, fontFace, fontScale, thickness, &baseLine);
return "";
}
v8::Local<v8::Value> getReturnValue() {
v8::Local<v8::Object> ret = Nan::New<v8::Object>();
Nan::Set(ret, Nan::New("size").ToLocalChecked(), Size::Converter::wrap(returnValue));
Nan::Set(ret, Nan::New("baseLine").ToLocalChecked(), FF::IntConverter::wrap(baseLine));
return ret;
}
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return (
FF::StringConverter::arg(0, &text, info) ||
FF::IntConverter::arg(1, &fontFace, info) ||
FF::DoubleConverter::arg(2, &fontScale, info) ||
FF::IntConverter::arg(3, &thickness, info)
);
}
};
#if CV_VERSION_GREATER_EQUAL(3, 2, 0)
struct CannyWorker : public CatchCvExceptionWorker {
public:
cv::Mat dx;
cv::Mat dy;
double threshold1;
double threshold2;
bool L2gradient = false;
cv::Mat returnValue;
std::string executeCatchCvExceptionWorker() {
cv::Canny(dx, dy, returnValue, threshold1, threshold2, L2gradient);
return "";
}
v8::Local<v8::Value> getReturnValue() {
return Mat::Converter::wrap(returnValue);
}
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return (
Mat::Converter::arg(0, &dx, info) ||
Mat::Converter::arg(1, &dy, info) ||
FF::DoubleConverter::arg(2, &threshold1, info) ||
FF::DoubleConverter::arg(3, &threshold2, info)
);
}
bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
return (
FF::BoolConverter::optArg(4, &L2gradient, info)
);
}
};
#endif
struct ApplyColorMapWorker : public CatchCvExceptionWorker {
public:
cv::Mat src;
cv::Mat dst;
cv::Mat userColor;
int colormap;
bool useUserColor = 0;
bool unwrapRequiredArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
#if CV_VERSION_GREATER_EQUAL(3, 3, 0)
if (info[1]->IsNumber()) {
return (Mat::Converter::arg(0, &src, info) ||
FF::IntConverter::optArg(1, &colormap, info));
}
useUserColor = 1;
return (Mat::Converter::arg(0, &src, info) ||
Mat::Converter::arg(1, &userColor, info));
#else
return (Mat::Converter::arg(0, &src, info) ||
FF::IntConverter::optArg(1, &colormap, info));
#endif
}
std::string executeCatchCvExceptionWorker() {
#if CV_VERSION_GREATER_EQUAL(3, 3, 0)
if (useUserColor) {
cv::applyColorMap(src, dst, userColor);
} else {
cv::applyColorMap(src, dst, colormap);
}
#else
cv::applyColorMap(src, dst, colormap);
#endif
return "";
}
v8::Local<v8::Value> getReturnValue() { return Mat::Converter::wrap(dst); }
};
#if CV_VERSION_LOWER_THAN(4, 0, 0)
// since 4.0.0 cv::undistortPoints has been moved from imgproc to calib3d
class UndistortPoints : public CvBinding {
public:
void setup() {
auto srcPoints = req<Point2::ArrayWithCastConverter<cv::Point2f>>();
auto cameraMatrix = req<Mat::Converter>();
auto distCoeffs = req<Mat::Converter>();
auto destPoints = ret<Point2::ArrayWithCastConverter<cv::Point2f>>("destPoints");
executeBinding = [=]() {
cv::undistortPoints(srcPoints->ref(), destPoints->ref(), cameraMatrix->ref(), distCoeffs->ref(), cameraMatrix->ref());
};
};
};
#endif
class GoodFeaturesToTrack : public CvClassMethodBinding<Mat> {
public:
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
auto maxCorners = req<FF::IntConverter>();
auto qualityLevel = req<FF::DoubleConverter>();
auto minDistance = req<FF::DoubleConverter>();
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
auto blockSize = opt<FF::IntConverter>("blockSize", 3);
auto gradientSize = opt<FF::IntConverter>("gradientSize", 3);
auto useHarrisDetector = opt<FF::BoolConverter>("useHarrisDetector", false);
auto harrisK = opt<FF::DoubleConverter>("harrisK", 0.04);
auto corners = ret<Point2::ArrayWithCastConverter<cv::Point2f>>("corners");
executeBinding = [=]() {
cv::goodFeaturesToTrack(
self->ref(), corners->ref(), maxCorners->ref(), qualityLevel->ref(), minDistance->ref(), mask->ref(), blockSize->ref(),
#if CV_VERSION_GREATER_EQUAL(3, 4, 0)
gradientSize->ref(),
#endif
useHarrisDetector->ref(), harrisK->ref()
);
};
};
};
class Blur : public CvClassMethodBinding<Mat> {
public:
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
auto kSize = req<Size::Converter>();
auto anchor = opt<Point2::Converter>("anchor", cv::Point2d());
auto borderType = opt<FF::IntConverter>("borderType", cv::BORDER_CONSTANT);
auto blurMat = ret<Mat::Converter>("blurMat");
executeBinding = [=]() {
cv::blur(self->ref(), blurMat->ref(), kSize->ref(), anchor->ref(), borderType->ref());
};
};
bool hasOptArgsObject(Nan::NAN_METHOD_ARGS_TYPE info) {
return FF::isArgObject(info, 1) && !Point2::hasInstance(info[1]);
}
};
class GaussianBlur : public CvClassMethodBinding<Mat> {
public:
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
auto kSize = req<Size::Converter>();
auto sigmaX = req<FF::DoubleConverter>();
auto sigmaY = opt<FF::DoubleConverter>("sigmaY", 0);
auto borderType = opt<FF::IntConverter>("borderType", cv::BORDER_CONSTANT);
auto blurMat = ret<Mat::Converter>("blurMat");
executeBinding = [=]() {
cv::GaussianBlur(self->ref(), blurMat->ref(), kSize->ref(), sigmaX->ref(), sigmaY->ref(), borderType->ref());
};
};
};
class MedianBlur : public CvClassMethodBinding<Mat> {
public:
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
auto kSize = req<FF::IntConverter>();
auto blurMat = ret<Mat::Converter>("blurMat");
executeBinding = [=]() {
cv::medianBlur(self->ref(), blurMat->ref(), kSize->ref());
};
};
};
class Accumulate : public CvBinding {
public:
void setup() {
auto src = req<Mat::Converter>();
auto dst = req<Mat::Converter>();
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
executeBinding = [=]() {
auto depth = dst->ref().depth();
if (depth != CV_32F && depth != CV_64F)
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
cv::accumulate(src->ref(), dst->ref(), mask->ref());
};
};
};
class AccumulateProduct : public CvBinding {
public:
void setup() {
auto src1 = req<Mat::Converter>();
auto src2 = req<Mat::Converter>();
auto dst = req<Mat::Converter>();
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
executeBinding = [=]() {
auto depth = dst->ref().depth();
if (depth != CV_32F && depth != CV_64F)
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
cv::accumulateProduct(src1->ref(), src2->ref(), dst->ref(), mask->ref());
};
};
};
class AccumulateSquare : public CvBinding {
public:
void setup() {
auto src = req<Mat::Converter>();
auto dst = req<Mat::Converter>();
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
executeBinding = [=]() {
auto depth = dst->ref().depth();
if (depth != CV_32F && depth != CV_64F)
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
cv::accumulateSquare(src->ref(), dst->ref(), mask->ref());
};
};
};
class AccumulateWeighted : public CvBinding {
public:
void setup() {
auto src = req<Mat::Converter>();
auto dst = req<Mat::Converter>();
auto alpha = req<FF::DoubleConverter>();
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
executeBinding = [=]() {
auto depth = dst->ref().depth();
if (depth != CV_32F && depth != CV_64F)
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
cv::accumulateWeighted(src->ref(), dst->ref(), alpha->ref(), mask->ref());
};
};
};
class CalcHist : public CvBinding {
public:
void setup() {
auto src = req<Mat::Converter>();
auto jsHistAxes = req<HistAxes::ArrayConverter>();
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
auto retHist = ret<Mat::Converter>("hist");
executeBinding = [=]() {
auto histAxes = jsHistAxes->ref();
auto img = src->ref();
cv::MatND hist;
const int dims = histAxes.size();
auto **ranges = new float*[dims];
int *channels = new int[dims];
int *bins = new int[dims];
for (int i = 0; i < dims; i++) {
auto entry = histAxes.at(i);
ranges[i] = new float[2];
ranges[i][0] = entry.range[0];
ranges[i][1] = entry.range[1];
channels[i] = entry.channel;
bins[i] = entry.bins;
}
cv::calcHist(
&img,
1,
channels,
mask->ref(),
hist,
dims,
bins,
(const float **)(ranges),
true,
false
);
for (int i = 0; i < dims; ++i) {
delete[] ranges[i];
}
delete[] ranges;
delete[] channels;
delete[] bins;
int outputType = CV_MAKETYPE(CV_64F, img.channels());
if (outputType != hist.type()) {
hist.convertTo(hist, outputType);
}
retHist->ref() = hist;
};
}
};
}
#endif