Skip to content
This repository was archived by the owner on Oct 18, 2023. It is now read-only.

Commit ff3c65c

Browse files
Merge pull request justadudewhohacks#272 from justadudewhohacks/justadudewhohacks#267
fixed mat mean to return a Vec4 with mean for each channel
2 parents 1cc4ac8 + b53b99d commit ff3c65c

File tree

6 files changed

+131
-6
lines changed

6 files changed

+131
-6
lines changed

cc/core/Mat.cc

+18
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ NAN_MODULE_INIT(Mat::Init) {
8585
Nan::SetPrototypeMethod(ctor, "sumAsync", SumAsync);
8686
Nan::SetPrototypeMethod(ctor, "goodFeaturesToTrack", GoodFeaturesToTrack);
8787
Nan::SetPrototypeMethod(ctor, "goodFeaturesToTrackAsync", GoodFeaturesToTrackAsync);
88+
Nan::SetPrototypeMethod(ctor, "mean", Mean);
89+
Nan::SetPrototypeMethod(ctor, "meanAsync", MeanAsync);
8890
Nan::SetPrototypeMethod(ctor, "meanStdDev", MeanStdDev);
8991
Nan::SetPrototypeMethod(ctor, "meanStdDevAsync", MeanStdDevAsync);
9092
#if CV_VERSION_MINOR > 1
@@ -771,6 +773,22 @@ NAN_METHOD(Mat::GoodFeaturesToTrackAsync) {
771773
);
772774
}
773775

776+
NAN_METHOD(Mat::Mean) {
777+
FF::SyncBinding(
778+
std::make_shared<MatBindings::MeanWorker>(Mat::Converter::unwrap(info.This())),
779+
"Mat::Mean",
780+
info
781+
);
782+
}
783+
784+
NAN_METHOD(Mat::MeanAsync) {
785+
FF::AsyncBinding(
786+
std::make_shared<MatBindings::MeanWorker>(Mat::Converter::unwrap(info.This())),
787+
"Mat::MeanAsync",
788+
info
789+
);
790+
}
791+
774792
NAN_METHOD(Mat::MeanStdDev) {
775793
FF::SyncBinding(
776794
std::make_shared<MatBindings::MeanStdDevWorker>(Mat::Converter::unwrap(info.This())),

cc/core/Mat.h

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,8 @@ class Mat : public Nan::ObjectWrap {
116116
static NAN_METHOD(ConvertScaleAbsAsync);
117117
static NAN_METHOD(GoodFeaturesToTrack);
118118
static NAN_METHOD(GoodFeaturesToTrackAsync);
119+
static NAN_METHOD(Mean);
120+
static NAN_METHOD(MeanAsync);
119121
static NAN_METHOD(MeanStdDev);
120122
static NAN_METHOD(MeanStdDevAsync);
121123
#if CV_VERSION_MINOR > 1

cc/core/MatBindings.h

+27
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,33 @@ namespace MatBindings {
700700
return ObjectArrayConverter<Point2, cv::Point2f>::wrap(corners);
701701
}
702702
};
703+
704+
struct MeanWorker : public CatchCvExceptionWorker {
705+
public:
706+
cv::Mat self;
707+
MeanWorker(cv::Mat self) {
708+
this->self = self;
709+
}
710+
711+
cv::Mat mask = cv::noArray().getMat();
712+
713+
cv::Scalar mean;
714+
715+
std::string executeCatchCvExceptionWorker() {
716+
mean = cv::mean(self, mask);
717+
return "";
718+
}
719+
720+
v8::Local<v8::Value> getReturnValue() {
721+
return Vec4::Converter::wrap(cv::Vec4d(mean));
722+
}
723+
724+
bool unwrapOptionalArgs(Nan::NAN_METHOD_ARGS_TYPE info) {
725+
return (
726+
Mat::Converter::optArg(0, &mask, info)
727+
);
728+
}
729+
};
703730

704731
struct MeanStdDevWorker : public CatchCvExceptionWorker {
705732
public:

cc/core/coreUtils.h

+1-5
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@
7979
Nan::SetPrototypeMethod(ctor, "absdiff", Absdiff); \
8080
Nan::SetPrototypeMethod(ctor, "exp", Exp); \
8181
Nan::SetPrototypeMethod(ctor, "log", Log); \
82-
Nan::SetPrototypeMethod(ctor, "mean", Mean); \
8382
Nan::SetPrototypeMethod(ctor, "sqrt", Sqrt); \
8483
Nan::SetPrototypeMethod(ctor, "dot", Dot);
8584

@@ -125,10 +124,7 @@
125124
} \
126125
static NAN_METHOD(Log) { \
127126
FF_SELF_OPERATOR(cv::log, unwrapper); \
128-
} \
129-
static NAN_METHOD(Mean) { \
130-
FF_SELF_OPERATOR(cv::mean, unwrapper); \
131-
} \
127+
} \
132128
static NAN_METHOD(Sqrt) { \
133129
FF_SELF_OPERATOR(cv::sqrt, unwrapper); \
134130
} \

lib/typings/Mat.d.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ export class Mat {
191191
matMulDerivAsync(B: Mat): Promise<{ dABdA: Mat, dABdB: Mat }>;
192192
matchTemplate(template: Mat, method: number, mask?: Mat): Mat;
193193
matchTemplateAsync(template: Mat, method: number, mask?: Mat): Promise<Mat>;
194-
mean(): Mat;
194+
mean(): Vec4;
195+
meanAsync(): Promise<Vec4>;
195196
meanStdDev(mask?: Mat): { mean: Mat, stddev: Mat };
196197
meanStdDevAsync(mask?: Mat): Promise<{ mean: Mat, stddev: Mat }>;
197198
medianBlur(kSize: number): Mat;

test/tests/core/Mat/Mat.test.js

+81
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,87 @@ describe('Mat', () => {
812812
});
813813
});
814814

815+
describe('mean', () => {
816+
const mask = new cv.Mat(1, 2, cv.CV_8U, 255);
817+
describe('C1', () => {
818+
const matData = [
819+
[0.5, 1]
820+
];
821+
822+
generateAPITests({
823+
getDut: () => new cv.Mat(matData, cv.CV_32FC1),
824+
methodName: 'mean',
825+
methodNameSpace: 'Mat',
826+
getOptionalArgs: () => ([
827+
mask
828+
]),
829+
expectOutput: (res) => {
830+
expect(res.at(0)).to.eq(0.75);
831+
}
832+
});
833+
});
834+
835+
describe('C2', () => {
836+
const matData = [
837+
[[0.5, 0.5], [1, 1.5]]
838+
];
839+
840+
generateAPITests({
841+
getDut: () => new cv.Mat(matData, cv.CV_32FC2),
842+
methodName: 'mean',
843+
methodNameSpace: 'Mat',
844+
getOptionalArgs: () => ([
845+
mask
846+
]),
847+
expectOutput: (res) => {
848+
expect(res.at(0)).to.eq(0.75);
849+
expect(res.at(1)).to.eq(1);
850+
}
851+
});
852+
});
853+
854+
describe('C3', () => {
855+
const matData = [
856+
[[0.5, 0.5, 0.5], [1, 1.5, 2.5]]
857+
];
858+
859+
generateAPITests({
860+
getDut: () => new cv.Mat(matData, cv.CV_32FC3),
861+
methodName: 'mean',
862+
methodNameSpace: 'Mat',
863+
getOptionalArgs: () => ([
864+
mask
865+
]),
866+
expectOutput: (res) => {
867+
expect(res.at(0)).to.eq(0.75);
868+
expect(res.at(1)).to.eq(1);
869+
expect(res.at(2)).to.eq(1.5);
870+
}
871+
});
872+
});
873+
874+
describe('C4', () => {
875+
const matData = [
876+
[[0.5, 0.5, 0.5, 0.5], [1, 1.5, 2.5, 3.5]]
877+
];
878+
879+
generateAPITests({
880+
getDut: () => new cv.Mat(matData, cv.CV_32FC4),
881+
methodName: 'mean',
882+
methodNameSpace: 'Mat',
883+
getOptionalArgs: () => ([
884+
mask
885+
]),
886+
expectOutput: (res) => {
887+
expect(res.at(0)).to.eq(0.75);
888+
expect(res.at(1)).to.eq(1);
889+
expect(res.at(2)).to.eq(1.5);
890+
expect(res.at(3)).to.eq(2);
891+
}
892+
});
893+
});
894+
});
895+
815896
describe('meanStdDev', () => {
816897
const mask = new cv.Mat(20, 20, cv.CV_8U, 255);
817898
generateAPITests({

0 commit comments

Comments
 (0)