Skip to content

Commit

Permalink
Merge pull request #272 from justadudewhohacks/#267
Browse files Browse the repository at this point in the history
fixed mat mean to return a Vec4 with mean for each channel
  • Loading branch information
justadudewhohacks authored Apr 26, 2018
2 parents 1cc4ac8 + b53b99d commit ff3c65c
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 6 deletions.
18 changes: 18 additions & 0 deletions cc/core/Mat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -771,6 +773,22 @@ NAN_METHOD(Mat::GoodFeaturesToTrackAsync) {
);
}

NAN_METHOD(Mat::Mean) {
FF::SyncBinding(
std::make_shared<MatBindings::MeanWorker>(Mat::Converter::unwrap(info.This())),
"Mat::Mean",
info
);
}

NAN_METHOD(Mat::MeanAsync) {
FF::AsyncBinding(
std::make_shared<MatBindings::MeanWorker>(Mat::Converter::unwrap(info.This())),
"Mat::MeanAsync",
info
);
}

NAN_METHOD(Mat::MeanStdDev) {
FF::SyncBinding(
std::make_shared<MatBindings::MeanStdDevWorker>(Mat::Converter::unwrap(info.This())),
Expand Down
2 changes: 2 additions & 0 deletions cc/core/Mat.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 27 additions & 0 deletions cc/core/MatBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -700,6 +700,33 @@ namespace MatBindings {
return ObjectArrayConverter<Point2, cv::Point2f>::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<v8::Value> 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:
Expand Down
6 changes: 1 addition & 5 deletions cc/core/coreUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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); \
} \
Expand Down
3 changes: 2 additions & 1 deletion lib/typings/Mat.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Mat>;
mean(): Mat;
mean(): Vec4;
meanAsync(): Promise<Vec4>;
meanStdDev(mask?: Mat): { mean: Mat, stddev: Mat };
meanStdDevAsync(mask?: Mat): Promise<{ mean: Mat, stddev: Mat }>;
medianBlur(kSize: number): Mat;
Expand Down
81 changes: 81 additions & 0 deletions test/tests/core/Mat/Mat.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down

0 comments on commit ff3c65c

Please sign in to comment.