Skip to content

Commit 0284d29

Browse files
author
Johan Junik Jo
committed
1. Mat class method binding was removed and matched with the orignal opencv signature.
2. Added a unit test for situations where dst depth exceptions occur.
1 parent 852a9e3 commit 0284d29

File tree

5 files changed

+77
-133
lines changed

5 files changed

+77
-133
lines changed

cc/imgproc/MatImgproc.cc

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ void MatImgproc::Init(v8::Local<v8::FunctionTemplate> ctor) {
120120
Nan::SetPrototypeMethod(ctor, "gaussianBlurAsync", GaussianBlurAsync);
121121
Nan::SetPrototypeMethod(ctor, "medianBlur", MedianBlur);
122122
Nan::SetPrototypeMethod(ctor, "medianBlurAsync", MedianBlurAsync);
123-
Nan::SetPrototypeMethod(ctor, "accumulate", Accumulate);
124-
Nan::SetPrototypeMethod(ctor, "accumulateAsync", AccumulateAsync);
125-
Nan::SetPrototypeMethod(ctor, "accumulateProduct", AccumulateProduct);
126-
Nan::SetPrototypeMethod(ctor, "accumulateProductAsync", AccumulateProductAsync);
127-
Nan::SetPrototypeMethod(ctor, "accumulateSquare", AccumulateSquare);
128-
Nan::SetPrototypeMethod(ctor, "accumulateSquareAsync", AccumulateSquareAsync);
129-
Nan::SetPrototypeMethod(ctor, "accumulateWeighted", AccumulateWeighted);
130-
Nan::SetPrototypeMethod(ctor, "accumulateWeightedAsync", AccumulateWeightedAsync);
131123
};
132124

133125
NAN_METHOD(MatImgproc::DrawContours) {
@@ -980,36 +972,4 @@ NAN_METHOD(MatImgproc::MedianBlurAsync) {
980972
Mat::asyncBinding<ImgprocBindings::MedianBlur>("MedianBlur", info);
981973
}
982974

983-
NAN_METHOD(MatImgproc::Accumulate) {
984-
Mat::syncBinding<ImgprocBindings::Accumulate>("Accumulate", info);
985-
}
986-
987-
NAN_METHOD(MatImgproc::AccumulateAsync) {
988-
Mat::asyncBinding<ImgprocBindings::Accumulate>("Accumulate", info);
989-
}
990-
991-
NAN_METHOD(MatImgproc::AccumulateProduct) {
992-
Mat::syncBinding<ImgprocBindings::AccumulateProduct>("AccumulateProduct", info);
993-
}
994-
995-
NAN_METHOD(MatImgproc::AccumulateProductAsync) {
996-
Mat::asyncBinding<ImgprocBindings::AccumulateProduct>("AccumulateProduct", info);
997-
}
998-
999-
NAN_METHOD(MatImgproc::AccumulateSquare) {
1000-
Mat::syncBinding<ImgprocBindings::AccumulateSquare>("AccumulateSquare", info);
1001-
}
1002-
1003-
NAN_METHOD(MatImgproc::AccumulateSquareAsync) {
1004-
Mat::asyncBinding<ImgprocBindings::AccumulateSquare>("AccumulateSquare", info);
1005-
}
1006-
1007-
NAN_METHOD(MatImgproc::AccumulateWeighted) {
1008-
Mat::syncBinding<ImgprocBindings::AccumulateWeighted>("AccumulateWeighted", info);
1009-
}
1010-
1011-
NAN_METHOD(MatImgproc::AccumulateWeightedAsync) {
1012-
Mat::asyncBinding<ImgprocBindings::AccumulateWeighted>("AccumulateWeighted", info);
1013-
}
1014-
1015975
#endif

cc/imgproc/MatImgproc.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,14 +124,6 @@ class MatImgproc {
124124
static NAN_METHOD(GaussianBlurAsync);
125125
static NAN_METHOD(MedianBlur);
126126
static NAN_METHOD(MedianBlurAsync);
127-
static NAN_METHOD(Accumulate);
128-
static NAN_METHOD(AccumulateAsync);
129-
static NAN_METHOD(AccumulateProduct);
130-
static NAN_METHOD(AccumulateProductAsync);
131-
static NAN_METHOD(AccumulateSquare);
132-
static NAN_METHOD(AccumulateSquareAsync);
133-
static NAN_METHOD(AccumulateWeighted);
134-
static NAN_METHOD(AccumulateWeightedAsync);
135127
};
136128

137129
#endif

cc/imgproc/imgprocBindings.h

Lines changed: 25 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -205,93 +205,69 @@ namespace ImgprocBindings {
205205
};
206206
};
207207

208-
class Accumulate : public CvClassMethodBinding<Mat> {
209-
public:
210-
cv::Mat dst;
211-
212-
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
208+
class Accumulate : public CvBinding {
209+
public:
210+
void setup() {
213211
auto src = req<Mat::Converter>();
212+
auto dst = req<Mat::Converter>();
214213
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
215214
executeBinding = [=]() {
216-
auto depth = self->ref().depth();
215+
auto depth = dst->ref().depth();
217216
if (depth != CV_32F && depth != CV_64F)
218-
throw std::runtime_error("dst must have a depth of CV_32F or CV_64F - mat depth:" + std::to_string(depth));
219-
cv::accumulate(src->ref(), self->ref(), mask->ref());
220-
dst = self->ref();
217+
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
218+
cv::accumulate(src->ref(), dst->ref(), mask->ref());
221219
};
222220
};
223-
224-
v8::Local<v8::Value> getReturnValue() {
225-
return Mat::Converter::wrap(dst);
226-
};
227221
};
228222

229-
class AccumulateProduct : public CvClassMethodBinding<Mat> {
223+
class AccumulateProduct : public CvBinding {
230224
public:
231-
cv::Mat dst;
232-
233-
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
225+
void setup() {
234226
auto src1 = req<Mat::Converter>();
235227
auto src2 = req<Mat::Converter>();
228+
auto dst = req<Mat::Converter>();
236229
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
237230

238231
executeBinding = [=]() {
239-
auto depth = self->ref().depth();
232+
auto depth = dst->ref().depth();
240233
if (depth != CV_32F && depth != CV_64F)
241-
throw std::runtime_error("dst must have a depth of CV_32F or CV_64F - mat depth:" + std::to_string(depth));
242-
cv::accumulateProduct(src1->ref(), src2->ref(), self->ref(), mask->ref());
243-
dst = self->ref();
234+
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
235+
cv::accumulateProduct(src1->ref(), src2->ref(), dst->ref(), mask->ref());
244236
};
245237
};
246-
247-
v8::Local<v8::Value> getReturnValue() {
248-
return Mat::Converter::wrap(dst);
249-
};
250238
};
251239

252-
class AccumulateSquare : public CvClassMethodBinding<Mat> {
240+
class AccumulateSquare : public CvBinding {
253241
public:
254-
cv::Mat dst;
255-
256-
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
242+
void setup() {
257243
auto src = req<Mat::Converter>();
244+
auto dst = req<Mat::Converter>();
258245
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
259246

260247
executeBinding = [=]() {
261-
auto depth = self->ref().depth();
248+
auto depth = dst->ref().depth();
262249
if (depth != CV_32F && depth != CV_64F)
263-
throw std::runtime_error("dst must have a depth of CV_32F or CV_64F - mat depth:" + std::to_string(depth));
264-
cv::accumulateSquare(src->ref(), self->ref(), mask->ref());
265-
dst = self->ref();
250+
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
251+
cv::accumulateSquare(src->ref(), dst->ref(), mask->ref());
266252
};
267253
};
268-
269-
v8::Local<v8::Value> getReturnValue() {
270-
return Mat::Converter::wrap(dst);
271-
};
272254
};
273255

274-
class AccumulateWeighted : public CvClassMethodBinding<Mat> {
256+
class AccumulateWeighted : public CvBinding {
275257
public:
276-
cv::Mat dst;
277-
278-
void createBinding(std::shared_ptr<FF::Value<cv::Mat>> self) {
258+
void setup() {
279259
auto src = req<Mat::Converter>();
260+
auto dst = req<Mat::Converter>();
280261
auto alpha = req<FF::DoubleConverter>();
281262
auto mask = opt<Mat::Converter>("mask", cv::noArray().getMat());
282263

283264
executeBinding = [=]() {
284-
auto depth = self->ref().depth();
265+
auto depth = dst->ref().depth();
285266
if (depth != CV_32F && depth != CV_64F)
286-
throw std::runtime_error("dst must have a depth of CV_32F or CV_64F - mat depth:" + std::to_string(depth));
287-
cv::accumulateWeighted(src->ref(), self->ref(), alpha->ref(), mask->ref());
288-
dst = self->ref();
267+
throw std::runtime_error("dst must has a depth of CV_32F or CV_64F");
268+
cv::accumulateWeighted(src->ref(), dst->ref(), alpha->ref(), mask->ref());
289269
};
290270
};
291-
292-
v8::Local<v8::Value> getReturnValue() {
293-
return Mat::Converter::wrap(dst);
294-
};
295271
};
296272
}
297273

lib/typings/cv.d.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ export interface HistAxes {
1919
ranges: number[];
2020
}
2121

22-
export function accumulate(dst: Mat, src: Mat, mask?: Mat): Mat;
23-
export function accumulateAsync(dst: Mat, src: Mat, mask?: Mat): Promise<Mat>;
24-
export function accumulateProduct(dst: Mat, src1: Mat, src2: Mat, mask?: Mat): Mat;
25-
export function accumulateProductAsync(dst: Mat, src1: Mat, src2:Mat, mask?: Mat): Promise<Mat>;
26-
export function accumulateSquare(dst: Mat, src: Mat, mask?: Mat): Mat;
27-
export function accumulateSquareAsync(dst: Mat, src: Mat, mask?: Mat): Promise<Mat>;
28-
export function accumulateWeighted(dst: Mat, src: Mat, alpha: number, mask?: Mat): Mat;
29-
export function accumulateWeightedAsync(dst: Mat, src: Mat, alpha: number, mask?: Mat): Promise<Mat>;
22+
export function accumulate(src: Mat, dst: Mat, mask?: Mat): void;
23+
export function accumulateAsync(src: Mat, dst: Mat, mask?: Mat): Promise<void>;
24+
export function accumulateProduct(src1: Mat, src2: Mat, dst: Mat, mask?: Mat): void;
25+
export function accumulateProductAsync(src1: Mat, src2: Mat, dst:Mat, mask?: Mat): Promise<void>;
26+
export function accumulateSquare(src: Mat, dst: Mat, mask?: Mat): void;
27+
export function accumulateSquareAsync(src: Mat, dst: Mat, mask?: Mat): Promise<void>;
28+
export function accumulateWeighted(src: Mat, dst: Mat, alpha: number, mask?: Mat): void;
29+
export function accumulateWeightedAsync(src: Mat, dst: Mat, alpha: number, mask?: Mat): Promise<void>;
3030
export function addWeighted(mat: Mat, alpha: number, mat2: Mat, beta: number, gamma: number, dtype?: number): Mat;
3131
export function addWeightedAsync(mat: Mat, alpha: number, mat2: Mat, beta: number, gamma: number, dtype?: number): Promise<Mat>;
3232
export function applyColorMap(src: Mat, colormap: number | Mat): Mat;

test/tests/imgproc/imgprocTests.js

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -415,26 +415,30 @@ module.exports = ({ cv, utils, getTestImg }) => {
415415
[[1, 1, 1], [11, 12, 13]]
416416
]
417417
const src = new cv.Mat(srcData, cv.CV_8UC3)
418+
const dstDepth8 = new cv.Mat(dstData, cv.CV_8UC3)
418419
let dst
419420
const mask = new cv.Mat(maskData, cv.CV_8UC1)
421+
422+
it('should throw if dst has not a depth of CV_32F or CV_64F', () => {
423+
expect(() => cv.accumulate(src, dstDepth8)).to.throw('Imgproc::Accumulate - dst must has a depth of CV_32F or CV_64F');
424+
});
420425

421426
generateAPITests({
422427
getDut: () => cv,
423428
methodName: 'accumulate',
424-
classNameSpace: 'Mat',
425429
methodNameSpace: 'Imgproc',
426430
beforeHook: () => dst = new cv.Mat(dstData, cv.CV_32FC3),
427431
getRequiredArgs: () => ([
428-
dst,
429432
src,
433+
dst,
430434
mask
431435
]),
432-
expectOutput: res => {
436+
expectOutput: () => {
433437
channelIndices = ['x', 'y', 'z']
434-
for (let row = 0; row < res.rows; row++) {
435-
for (let col = 0; col < res.cols; col++) {
436-
for (let channel = 0; channel < res.channels; channel++) {
437-
expect(res.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
438+
for (let row = 0; row < dst.rows; row++) {
439+
for (let col = 0; col < dst.cols; col++) {
440+
for (let channel = 0; channel < dst.channels; channel++) {
441+
expect(dst.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
438442
}
439443
}
440444
}
@@ -467,26 +471,30 @@ module.exports = ({ cv, utils, getTestImg }) => {
467471
const src1 = new cv.Mat(srcData1, cv.CV_8UC3)
468472
const src2 = new cv.Mat(srcData2, cv.CV_8UC3)
469473
let dst
474+
const dstDepth8 = new cv.Mat(dstData, cv.CV_8UC3)
470475
const mask = new cv.Mat(maskData, cv.CV_8UC1)
471476

477+
it('should throw if dst has not a depth of CV_32F or CV_64F', () => {
478+
expect(() => cv.accumulateProduct(src1, src2, dstDepth8)).to.throw('Imgproc::AccumulateProduct - dst must has a depth of CV_32F or CV_64F');
479+
});
480+
472481
generateAPITests({
473482
getDut: () => cv,
474483
methodName: 'accumulateProduct',
475-
classNameSpace: 'Mat',
476484
methodNameSpace: 'Imgproc',
477485
beforeHook: () => dst = new cv.Mat(dstData, cv.CV_32FC3),
478486
getRequiredArgs: () => ([
479-
dst,
480487
src1,
481488
src2,
489+
dst,
482490
mask
483491
]),
484-
expectOutput: res => {
492+
expectOutput: () => {
485493
channelIndices = ['x', 'y', 'z']
486-
for (let row = 0; row < res.rows; row++) {
487-
for (let col = 0; col < res.cols; col++) {
488-
for (let channel = 0; channel < res.channels; channel++) {
489-
expect(res.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
494+
for (let row = 0; row < dst.rows; row++) {
495+
for (let col = 0; col < dst.cols; col++) {
496+
for (let channel = 0; channel < dst.channels; channel++) {
497+
expect(dst.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
490498
}
491499
}
492500
}
@@ -514,25 +522,29 @@ module.exports = ({ cv, utils, getTestImg }) => {
514522

515523
const src = new cv.Mat(srcData, cv.CV_8UC3)
516524
let dst
525+
const dstDepth8 = new cv.Mat(dstData, cv.CV_8UC3)
517526
const mask = new cv.Mat(maskData, cv.CV_8UC1)
518527

528+
it('should throw if dst has not a depth of CV_32F or CV_64F', () => {
529+
expect(() => cv.accumulateSquare(src, dstDepth8)).to.throw('Imgproc::AccumulateSquare - dst must has a depth of CV_32F or CV_64F');
530+
});
531+
519532
generateAPITests({
520533
getDut: () => cv,
521534
methodName: 'accumulateSquare',
522-
classNameSpace: 'Mat',
523535
methodNameSpace: 'Imgproc',
524536
beforeHook: () => dst = new cv.Mat(dstData, cv.CV_32FC3),
525537
getRequiredArgs: () => ([
526-
dst,
527538
src,
539+
dst,
528540
mask
529541
]),
530-
expectOutput: res => {
542+
expectOutput: () => {
531543
channelIndices = ['x', 'y', 'z']
532-
for (let row = 0; row < res.rows; row++) {
533-
for (let col = 0; col < res.cols; col++) {
534-
for (let channel = 0; channel < res.channels; channel++) {
535-
expect(res.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
544+
for (let row = 0; row < dst.rows; row++) {
545+
for (let col = 0; col < dst.cols; col++) {
546+
for (let channel = 0; channel < dst.channels; channel++) {
547+
expect(dst.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
536548
}
537549
}
538550
}
@@ -561,26 +573,30 @@ module.exports = ({ cv, utils, getTestImg }) => {
561573

562574
const src = new cv.Mat(srcData, cv.CV_8UC3)
563575
let dst
576+
const dstDepth8 = new cv.Mat(dstData, cv.CV_8UC3)
564577
const mask = new cv.Mat(maskData, cv.CV_8UC1)
565578

579+
it('should throw if dst has not a depth of CV_32F or CV_64F', () => {
580+
expect(() => cv.accumulateWeighted(src, dstDepth8, alpha)).to.throw('Imgproc::AccumulateWeighted - dst must has a depth of CV_32F or CV_64F');
581+
});
582+
566583
generateAPITests({
567584
getDut: () => cv,
568585
methodName: 'accumulateWeighted',
569-
classNameSpace: 'Mat',
570586
methodNameSpace: 'Imgproc',
571587
beforeHook: () => dst = new cv.Mat(dstData, cv.CV_32FC3),
572588
getRequiredArgs: () => ([
573-
dst,
574589
src,
590+
dst,
575591
alpha,
576592
mask
577593
]),
578-
expectOutput: res => {
594+
expectOutput: () => {
579595
channelIndices = ['x', 'y', 'z']
580-
for (let row = 0; row < res.rows; row++) {
581-
for (let col = 0; col < res.cols; col++) {
582-
for (let channel = 0; channel < res.channels; channel++) {
583-
expect(res.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
596+
for (let row = 0; row < dst.rows; row++) {
597+
for (let col = 0; col < dst.cols; col++) {
598+
for (let channel = 0; channel < dst.channels; channel++) {
599+
expect(dst.at(row, col)[channelIndices[channel]]).to.be.closeTo(expectedData[row][col][channel], 1e-5);
584600
}
585601
}
586602
}

0 commit comments

Comments
 (0)