From 2a2ea7d74acfd66729cf612f3f4b09b4ab232a06 Mon Sep 17 00:00:00 2001 From: Anshul Date: Thu, 21 Apr 2016 22:18:19 -0700 Subject: [PATCH] Add matrix mean method --- src/Matrix.cc | 17 ++++++++++++++++- src/Matrix.h | 1 + test/unit.js | 13 +++++++++++++ 3 files changed, 30 insertions(+), 1 deletion(-) diff --git a/src/Matrix.cc b/src/Matrix.cc index b1a1b39..77eb43d 100755 --- a/src/Matrix.cc +++ b/src/Matrix.cc @@ -106,6 +106,7 @@ void Matrix::Init(Local target) { Nan::SetPrototypeMethod(ctor, "copyWithMask", CopyWithMask); Nan::SetPrototypeMethod(ctor, "setWithMask", SetWithMask); Nan::SetPrototypeMethod(ctor, "meanWithMask", MeanWithMask); + Nan::SetPrototypeMethod(ctor, "mean", Mean); Nan::SetPrototypeMethod(ctor, "shift", Shift); Nan::SetPrototypeMethod(ctor, "reshape", Reshape); Nan::SetPrototypeMethod(ctor, "release", Release); @@ -2484,10 +2485,24 @@ NAN_METHOD(Matrix::MeanWithMask) { Matrix *mask = Nan::ObjectWrap::Unwrap(info[0]->ToObject()); cv::Scalar means = cv::mean(self->mat, mask->mat); - v8::Local < v8::Array > arr = Nan::New(3); + v8::Local < v8::Array > arr = Nan::New(4); arr->Set(0, Nan::New(means[0])); arr->Set(1, Nan::New(means[1])); arr->Set(2, Nan::New(means[2])); + arr->Set(3, Nan::New(means[3])); + + info.GetReturnValue().Set(arr); +} + +NAN_METHOD(Matrix::Mean) { + SETUP_FUNCTION(Matrix) + + cv::Scalar means = cv::mean(self->mat); + v8::Local arr = Nan::New(4); + arr->Set(0, Nan::New(means[0])); + arr->Set(1, Nan::New(means[1])); + arr->Set(2, Nan::New(means[2])); + arr->Set(3, Nan::New(means[3])); info.GetReturnValue().Set(arr); } diff --git a/src/Matrix.h b/src/Matrix.h index e000737..74cd100 100755 --- a/src/Matrix.h +++ b/src/Matrix.h @@ -121,6 +121,7 @@ public: JSFUNC(CopyWithMask) JSFUNC(SetWithMask) JSFUNC(MeanWithMask) + JSFUNC(Mean) JSFUNC(Shift) JSFUNC(Reshape) diff --git a/test/unit.js b/test/unit.js index a6312b2..309cf62 100755 --- a/test/unit.js +++ b/test/unit.js @@ -351,6 +351,19 @@ test('Subtract', function(assert) { assert.end(); }); +test('Mean', function(assert) { + var a = new cv.Matrix.Zeros(2, 2, cv.Constants.CV_8UC3); + + // Set [0, 0] element to 1 for all three channels + a.set(0, 0, 1, 0); + a.set(0, 0, 1, 1); + a.set(0, 0, 1, 2); + + var means = a.mean(); + assert.deepEqual(means, [0.25, 0.25, 0.25, 0]); + assert.end(); +}); + // Test the examples folder. require('./examples')()