From b53b99d411f09bafc1ede1ff837ba8c1266c06f5 Mon Sep 17 00:00:00 2001 From: vincent Date: Thu, 26 Apr 2018 09:10:12 +0200 Subject: [PATCH] fixed mat mean to return a Vec4 with mean for each channel --- cc/core/Mat.cc | 18 ++++++++ cc/core/Mat.h | 2 + cc/core/MatBindings.h | 27 +++++++++++ cc/core/coreUtils.h | 6 +-- lib/typings/Mat.d.ts | 3 +- test/tests/core/Mat/Mat.test.js | 81 +++++++++++++++++++++++++++++++++ 6 files changed, 131 insertions(+), 6 deletions(-) diff --git a/cc/core/Mat.cc b/cc/core/Mat.cc index d22e2eac3..3558a9570 100644 --- a/cc/core/Mat.cc +++ b/cc/core/Mat.cc @@ -85,6 +85,8 @@ NAN_MODULE_INIT(Mat::Init) { Nan::SetPrototypeMethod(ctor, "sumAsync", SumAsync); Nan::SetPrototypeMethod(ctor, "goodFeaturesToTrack", GoodFeaturesToTrack); Nan::SetPrototypeMethod(ctor, "goodFeaturesToTrackAsync", GoodFeaturesToTrackAsync); + Nan::SetPrototypeMethod(ctor, "mean", Mean); + Nan::SetPrototypeMethod(ctor, "meanAsync", MeanAsync); Nan::SetPrototypeMethod(ctor, "meanStdDev", MeanStdDev); Nan::SetPrototypeMethod(ctor, "meanStdDevAsync", MeanStdDevAsync); #if CV_VERSION_MINOR > 1 @@ -771,6 +773,22 @@ NAN_METHOD(Mat::GoodFeaturesToTrackAsync) { ); } +NAN_METHOD(Mat::Mean) { + FF::SyncBinding( + std::make_shared(Mat::Converter::unwrap(info.This())), + "Mat::Mean", + info + ); +} + +NAN_METHOD(Mat::MeanAsync) { + FF::AsyncBinding( + std::make_shared(Mat::Converter::unwrap(info.This())), + "Mat::MeanAsync", + info + ); +} + NAN_METHOD(Mat::MeanStdDev) { FF::SyncBinding( std::make_shared(Mat::Converter::unwrap(info.This())), diff --git a/cc/core/Mat.h b/cc/core/Mat.h index 73b04d921..f97192642 100644 --- a/cc/core/Mat.h +++ b/cc/core/Mat.h @@ -116,6 +116,8 @@ class Mat : public Nan::ObjectWrap { static NAN_METHOD(ConvertScaleAbsAsync); static NAN_METHOD(GoodFeaturesToTrack); static NAN_METHOD(GoodFeaturesToTrackAsync); + static NAN_METHOD(Mean); + static NAN_METHOD(MeanAsync); static NAN_METHOD(MeanStdDev); static NAN_METHOD(MeanStdDevAsync); #if CV_VERSION_MINOR > 1 diff --git a/cc/core/MatBindings.h b/cc/core/MatBindings.h index 7f8a31482..aa409d595 100644 --- a/cc/core/MatBindings.h +++ b/cc/core/MatBindings.h @@ -700,6 +700,33 @@ namespace MatBindings { return ObjectArrayConverter::wrap(corners); } }; + + struct MeanWorker : public CatchCvExceptionWorker { + public: + cv::Mat self; + MeanWorker(cv::Mat self) { + this->self = self; + } + + cv::Mat mask = cv::noArray().getMat(); + + cv::Scalar mean; + + std::string executeCatchCvExceptionWorker() { + mean = cv::mean(self, mask); + return ""; + } + + v8::Local getReturnValue() { + return Vec4::Converter::wrap(cv::Vec4d(mean)); + } + + bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) { + return ( + Mat::Converter::optArg(0, &mask, info) + ); + } + }; struct MeanStdDevWorker : public CatchCvExceptionWorker { public: diff --git a/cc/core/coreUtils.h b/cc/core/coreUtils.h index 3bf3cd638..9f994f084 100644 --- a/cc/core/coreUtils.h +++ b/cc/core/coreUtils.h @@ -79,7 +79,6 @@ Nan::SetPrototypeMethod(ctor, "absdiff", Absdiff); \ Nan::SetPrototypeMethod(ctor, "exp", Exp); \ Nan::SetPrototypeMethod(ctor, "log", Log); \ - Nan::SetPrototypeMethod(ctor, "mean", Mean); \ Nan::SetPrototypeMethod(ctor, "sqrt", Sqrt); \ Nan::SetPrototypeMethod(ctor, "dot", Dot); @@ -125,10 +124,7 @@ } \ static NAN_METHOD(Log) { \ FF_SELF_OPERATOR(cv::log, unwrapper); \ - } \ - static NAN_METHOD(Mean) { \ - FF_SELF_OPERATOR(cv::mean, unwrapper); \ - } \ + } \ static NAN_METHOD(Sqrt) { \ FF_SELF_OPERATOR(cv::sqrt, unwrapper); \ } \ diff --git a/lib/typings/Mat.d.ts b/lib/typings/Mat.d.ts index 837fc7743..b13dfc57d 100644 --- a/lib/typings/Mat.d.ts +++ b/lib/typings/Mat.d.ts @@ -191,7 +191,8 @@ export class Mat { matMulDerivAsync(B: Mat): Promise<{ dABdA: Mat, dABdB: Mat }>; matchTemplate(template: Mat, method: number, mask?: Mat): Mat; matchTemplateAsync(template: Mat, method: number, mask?: Mat): Promise; - mean(): Mat; + mean(): Vec4; + meanAsync(): Promise; meanStdDev(mask?: Mat): { mean: Mat, stddev: Mat }; meanStdDevAsync(mask?: Mat): Promise<{ mean: Mat, stddev: Mat }>; medianBlur(kSize: number): Mat; diff --git a/test/tests/core/Mat/Mat.test.js b/test/tests/core/Mat/Mat.test.js index 2a530c5bc..5e76c824c 100644 --- a/test/tests/core/Mat/Mat.test.js +++ b/test/tests/core/Mat/Mat.test.js @@ -812,6 +812,87 @@ describe('Mat', () => { }); }); + describe('mean', () => { + const mask = new cv.Mat(1, 2, cv.CV_8U, 255); + describe('C1', () => { + const matData = [ + [0.5, 1] + ]; + + generateAPITests({ + getDut: () => new cv.Mat(matData, cv.CV_32FC1), + methodName: 'mean', + methodNameSpace: 'Mat', + getOptionalArgs: () => ([ + mask + ]), + expectOutput: (res) => { + expect(res.at(0)).to.eq(0.75); + } + }); + }); + + describe('C2', () => { + const matData = [ + [[0.5, 0.5], [1, 1.5]] + ]; + + generateAPITests({ + getDut: () => new cv.Mat(matData, cv.CV_32FC2), + methodName: 'mean', + methodNameSpace: 'Mat', + getOptionalArgs: () => ([ + mask + ]), + expectOutput: (res) => { + expect(res.at(0)).to.eq(0.75); + expect(res.at(1)).to.eq(1); + } + }); + }); + + describe('C3', () => { + const matData = [ + [[0.5, 0.5, 0.5], [1, 1.5, 2.5]] + ]; + + generateAPITests({ + getDut: () => new cv.Mat(matData, cv.CV_32FC3), + methodName: 'mean', + methodNameSpace: 'Mat', + getOptionalArgs: () => ([ + mask + ]), + expectOutput: (res) => { + expect(res.at(0)).to.eq(0.75); + expect(res.at(1)).to.eq(1); + expect(res.at(2)).to.eq(1.5); + } + }); + }); + + describe('C4', () => { + const matData = [ + [[0.5, 0.5, 0.5, 0.5], [1, 1.5, 2.5, 3.5]] + ]; + + generateAPITests({ + getDut: () => new cv.Mat(matData, cv.CV_32FC4), + methodName: 'mean', + methodNameSpace: 'Mat', + getOptionalArgs: () => ([ + mask + ]), + expectOutput: (res) => { + expect(res.at(0)).to.eq(0.75); + expect(res.at(1)).to.eq(1); + expect(res.at(2)).to.eq(1.5); + expect(res.at(3)).to.eq(2); + } + }); + }); + }); + describe('meanStdDev', () => { const mask = new cv.Mat(20, 20, cv.CV_8U, 255); generateAPITests({