Skip to content

Commit

Permalink
BackgroundSubtractors refactored + added testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
justadudewhohacks committed Sep 23, 2017
1 parent c2ac476 commit 9f8dfcb
Show file tree
Hide file tree
Showing 11 changed files with 262 additions and 95 deletions.
15 changes: 6 additions & 9 deletions cc/modules/video/BackgroundSubtractor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,14 @@ void BackgroundSubtractor::Init(v8::Local<v8::FunctionTemplate> ctor) {
};

NAN_METHOD(BackgroundSubtractor::Apply) {
double learningRate = -1;
FF_REQUIRE_INSTANCE(Mat::constructor, info[0], "expected arg0 to be instance of Mat");
cv::Mat frame = FF_UNWRAP_MAT_AND_GET(info[0]->ToObject());
if (info[1]->IsNumber()) {
learningRate = info[1]->NumberValue();
}
FF_METHOD_CONTEXT("BackgroundSubtractorKNN::Apply");

FF_ARG_INSTANCE(0, cv::Mat frame, Mat::constructor, FF_UNWRAP_MAT_AND_GET);
FF_ARG_NUMBER_IFDEF(1, double learningRate, -1);

BackgroundSubtractor* self = FF_UNWRAP(info.This(), BackgroundSubtractor);
self->getSubtractor()->apply(frame, self->fgMask, learningRate);

v8::Local<v8::Object> jsMat = FF_NEW_INSTANCE(Mat::constructor);
FF_OBJ jsMat = FF_NEW_INSTANCE(Mat::constructor);
FF_UNWRAP_MAT_AND_GET(jsMat) = self->fgMask;
info.GetReturnValue().Set(jsMat);
FF_RETURN(jsMat);
}
28 changes: 17 additions & 11 deletions cc/modules/video/BackgroundSubtractorKNN.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ NAN_MODULE_INIT(BackgroundSubtractorKNN::Init) {
};

NAN_METHOD(BackgroundSubtractorKNN::New) {
BackgroundSubtractorKNN* self = new BackgroundSubtractorKNN();
int history = 500;
double dist2Threshold = 400;
bool detectShadows = true;
if (info.Length() > 0) {
FF_REQUIRE_ARGS_OBJ("BackgroundSubtractorKNN::New");
FF_DESTRUCTURE_TYPECHECKED_JSPROP_IFDEF(args, history, IsUint32, Uint32Value);
FF_DESTRUCTURE_TYPECHECKED_JSPROP_IFDEF(args, dist2Threshold, IsNumber, NumberValue);
FF_DESTRUCTURE_TYPECHECKED_JSPROP_IFDEF(args, history, IsBoolean, BooleanValue);
FF_METHOD_CONTEXT("BackgroundSubtractorKNN::New");

// optional args
bool hasOptArgsObj = FF_HAS_ARG(0) && info[0]->IsObject();
FF_OBJ optArgs = hasOptArgsObj ? info[0]->ToObject() : FF_NEW_OBJ();

FF_GET_UINT_IFDEF(optArgs, uint history, "history", 500);
FF_GET_NUMBER_IFDEF(optArgs, double dist2Threshold, "dist2Threshold", 400);
FF_GET_BOOL_IFDEF(optArgs, bool detectShadows, "detectShadows", true);
if (!hasOptArgsObj) {
FF_ARG_UINT_IFDEF(0, history, history);
FF_ARG_NUMBER_IFDEF(1, dist2Threshold, dist2Threshold);
FF_ARG_BOOL_IFDEF(2, detectShadows, detectShadows);
}
self->subtractor = cv::createBackgroundSubtractorKNN(history, dist2Threshold, detectShadows);

BackgroundSubtractorKNN* self = new BackgroundSubtractorKNN();
self->subtractor = cv::createBackgroundSubtractorKNN((int)history, dist2Threshold, detectShadows);
self->Wrap(info.Holder());
info.GetReturnValue().Set(info.Holder());
FF_RETURN(info.Holder());
};
28 changes: 17 additions & 11 deletions cc/modules/video/BackgroundSubtractorMOG2.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,23 @@ NAN_MODULE_INIT(BackgroundSubtractorMOG2::Init) {
};

NAN_METHOD(BackgroundSubtractorMOG2::New) {
BackgroundSubtractorMOG2* self = new BackgroundSubtractorMOG2();
int history = 500;
double varThreshold = 16;
bool detectShadows = true;
if (info.Length() > 0) {
FF_REQUIRE_ARGS_OBJ("BackgroundSubtractorMOG2::New");
FF_DESTRUCTURE_TYPECHECKED_JSPROP_IFDEF(args, history, IsUint32, Uint32Value);
FF_DESTRUCTURE_TYPECHECKED_JSPROP_IFDEF(args, varThreshold, IsNumber, NumberValue);
FF_DESTRUCTURE_TYPECHECKED_JSPROP_IFDEF(args, history, IsBoolean, BooleanValue);
FF_METHOD_CONTEXT("BackgroundSubtractorMOG2::New");

// optional args
bool hasOptArgsObj = FF_HAS_ARG(0) && info[0]->IsObject();
FF_OBJ optArgs = hasOptArgsObj ? info[0]->ToObject() : FF_NEW_OBJ();

FF_GET_UINT_IFDEF(optArgs, uint history, "history", 500);
FF_GET_NUMBER_IFDEF(optArgs, double varThreshold, "varThreshold", 16);
FF_GET_BOOL_IFDEF(optArgs, bool detectShadows, "detectShadows", true);
if (!hasOptArgsObj) {
FF_ARG_UINT_IFDEF(0, history, history);
FF_ARG_NUMBER_IFDEF(1, varThreshold, varThreshold);
FF_ARG_BOOL_IFDEF(2, detectShadows, detectShadows);
}
self->subtractor = cv::createBackgroundSubtractorMOG2(history, varThreshold, detectShadows);

BackgroundSubtractorMOG2* self = new BackgroundSubtractorMOG2();
self->subtractor = cv::createBackgroundSubtractorMOG2((int)history, varThreshold, detectShadows);
self->Wrap(info.Holder());
info.GetReturnValue().Set(info.Holder());
FF_RETURN(info.Holder());
};
12 changes: 5 additions & 7 deletions doc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,20 +121,18 @@ TODO doc Rect, RotatedRect, Size
* <a href="./io/VideoCapture.md#read">read</a>
* <a href="./io/VideoCapture.md#reset">reset</a>

TODO API refactoring
* <a href="./video.md"><b>video</b></a>
* <a href="./video.md#BackgroundSubtractorMOG2"><b>BackgroundSubtractorMOG2</b></a>
* <a href="./video.md#BackgroundSubtractorMOG2.apply">apply</a>
* <a href="./video.md#BackgroundSubtractorKNN"><b>BackgroundSubtractorKNN</b></a>
* <a href="./video.md#BackgroundSubtractorKNN.apply">apply</a>
* <a href="./video/video.md"><b>video</b></a>
* <a href="./video/BackgroundSubtractorMOG2.md"><b>BackgroundSubtractorMOG2</b></a>
* <a href="./video/BackgroundSubtractorMOG2.md#apply">apply</a>
* <a href="./video/BackgroundSubtractorKNN.md"><b>BackgroundSubtractorKNN</b></a>
* <a href="./video/BackgroundSubtractorKNN.md#apply">apply</a>

* <a href="./calib3d.md"><b>calib3d</b></a>
* <a href="./calib3d.md#findHomography">findHomography</a>

* <a href="./photo.md"><b>photo</b></a>
* <a href="./photo.md#fastNlMeansDenoisingColored">fastNlMeansDenoisingColored</a>

TODO API refactoring
* <a href="./objdetect/objdetect.md"><b>objdetect</b></a>
* <a href="./objdetect/CascadeClassifier.md"><b>CascadeClassifier</b></a>
* <a href="./objdetect/CascadeClassifier.md#detectMultiScale">detectMultiScale</a>
Expand Down
57 changes: 0 additions & 57 deletions doc/video.md

This file was deleted.

25 changes: 25 additions & 0 deletions doc/video/BackgroundSubtractorKNN.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# BackgroundSubtractorKNN

## Accessors
``` javascript
BackgroundSubtractorKNN {
history: Uint,
dist2Threshold: Number,
detectShadows: Boolean
}
```

<a name="constructors"></a>

## Constructors
``` javascript
BackgroundSubtractorKNN : new BackgroundSubtractorKNN(Uint history = 500, Number varThreshold = 400, Boolean detectShadows = true)
```
## Methods

<a name="apply"></a>

### apply
``` javascript
Mat : bgsubtractor.apply(Mat frame, Number learningRate = -1)
```
25 changes: 25 additions & 0 deletions doc/video/BackgroundSubtractorMOG2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# BackgroundSubtractorMOG2

## Accessors
``` javascript
BackgroundSubtractorMOG2 {
history: Uint,
varThreshold: Number,
detectShadows: Boolean
}
```

<a name="constructors"></a>

## Constructors
``` javascript
BackgroundSubtractorMOG2 : new BackgroundSubtractorMOG2(Uint history = 500, Number varThreshold = 16, Boolean detectShadows = true)
```
## Methods

<a name="apply"></a>

### apply
``` javascript
Mat : bgsubtractor.apply(Mat frame, Number learningRate = -1)
```
1 change: 1 addition & 0 deletions doc/video/video.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# video
79 changes: 79 additions & 0 deletions test/cc/modules/video/BackgroundSubtractorKNNTests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const cv = global.dut;
const {
assertMetaData,
assertPropsWithValue,
funcShouldRequireArgs,
readTestImage
} = global.utils;
const { expect } = require('chai');

module.exports = () => {
describe('BackgroundSubtractorKNN', () => {
const history = 1000;
const dist2Threshold = 100.5;
const detectShadows = false;

describe('constructor', () => {
it('should be constructable with default args', () => {
expect(() => new cv.BackgroundSubtractorKNN()).to.not.throw();
});

it('should be constructable with optional args', () => {
const bgsubtractor = new cv.BackgroundSubtractorKNN(
history,
dist2Threshold,
detectShadows
);
assertPropsWithValue(bgsubtractor)({
history,
dist2Threshold,
detectShadows
});
});

it('should be constructable with optional args object', () => {
const bgsubtractor = new cv.BackgroundSubtractorKNN({
history,
dist2Threshold,
detectShadows
});
assertPropsWithValue(bgsubtractor)({
history,
dist2Threshold,
detectShadows
});
});

it('should implement apply', () => {
const bgsubtractor = new cv.BackgroundSubtractorKNN();
expect(bgsubtractor).to.have.property('apply').to.be.a('function');
});
});

describe('apply', () => {
let testImg;

before(() => {
testImg = readTestImage();
});

const learningRate = 2.5;

funcShouldRequireArgs(() => new cv.BackgroundSubtractorKNN().apply());

it('can be called if required args passed', () => {
const bgsubtractor = new cv.BackgroundSubtractorKNN();
const fgMask = bgsubtractor.apply(testImg);
expect(fgMask).instanceOf(cv.Mat);
assertMetaData(fgMask)(testImg.rows, testImg.cols, cv.CV_8U);
});

it('can be called with learningRate', () => {
const bgsubtractor = new cv.BackgroundSubtractorKNN();
const fgMask = bgsubtractor.apply(testImg, learningRate);
expect(fgMask).instanceOf(cv.Mat);
assertMetaData(fgMask)(testImg.rows, testImg.cols, cv.CV_8U);
});
});
});
};
79 changes: 79 additions & 0 deletions test/cc/modules/video/BackgroundSubtractorMOG2Tests.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
const cv = global.dut;
const {
assertMetaData,
assertPropsWithValue,
funcShouldRequireArgs,
readTestImage
} = global.utils;
const { expect } = require('chai');

module.exports = () => {
describe('BackgroundSubtractorMOG2', () => {
const history = 1000;
const varThreshold = 8;
const detectShadows = false;

describe('constructor', () => {
it('should be constructable with default args', () => {
expect(() => new cv.BackgroundSubtractorMOG2()).to.not.throw();
});

it('should be constructable with optional args', () => {
const bgsubtractor = new cv.BackgroundSubtractorMOG2(
history,
varThreshold,
detectShadows
);
assertPropsWithValue(bgsubtractor)({
history,
varThreshold,
detectShadows
});
});

it('should be constructable with optional args object', () => {
const bgsubtractor = new cv.BackgroundSubtractorMOG2({
history,
varThreshold,
detectShadows
});
assertPropsWithValue(bgsubtractor)({
history,
varThreshold,
detectShadows
});
});

it('should implement apply', () => {
const bgsubtractor = new cv.BackgroundSubtractorMOG2();
expect(bgsubtractor).to.have.property('apply').to.be.a('function');
});
});

describe('apply', () => {
let testImg;

before(() => {
testImg = readTestImage();
});

const learningRate = 2.5;

funcShouldRequireArgs(() => new cv.BackgroundSubtractorMOG2().apply());

it('can be called if required args passed', () => {
const bgsubtractor = new cv.BackgroundSubtractorMOG2();
const fgMask = bgsubtractor.apply(testImg);
expect(fgMask).instanceOf(cv.Mat);
assertMetaData(fgMask)(testImg.rows, testImg.cols, cv.CV_8U);
});

it('can be called with learningRate', () => {
const bgsubtractor = new cv.BackgroundSubtractorMOG2();
const fgMask = bgsubtractor.apply(testImg, learningRate);
expect(fgMask).instanceOf(cv.Mat);
assertMetaData(fgMask)(testImg.rows, testImg.cols, cv.CV_8U);
});
});
});
};
Loading

0 comments on commit 9f8dfcb

Please sign in to comment.