Skip to content

Commit 3bd18fc

Browse files
committed
fix: background and noise
1 parent 6a0623e commit 3bd18fc

File tree

6 files changed

+16
-12
lines changed

6 files changed

+16
-12
lines changed

lib/backend/opencv-backend.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,16 @@ class OpenCVBackend {
154154
}
155155

156156
if (channels === 4) {
157+
157158
const channels = foreground.splitChannels();
158159
const a = channels[3].cvtColor(this._cv.COLOR_GRAY2BGR).convertTo(this._cv.CV_32FC3, 1 / 255);
160+
const white = new this._cv.Mat(height, width, this._cv.CV_8UC3).convertTo(this._cv.CV_32FC3, 1 / 255, 1);
161+
const inverseA = white.sub(a);
159162
const b = foreground.cvtColor(this._cv.COLOR_BGRA2BGR).convertTo(this._cv.CV_32FC3);
160163
const foreW = b.hMul(a);
161-
const inverseA = new this._cv.Mat(height, width, this._cv.CV_32FC3, new this._cv.Vec(1, 1, 1)).sub(a);
162-
const backW = foreground.cvtColor(this._cv.COLOR_BGRA2BGR).convertTo(this._cv.CV_32FC3).hMul(inverseA);
163-
return foreW.add(backW).convertTo(this._cv.CV_8UC3);
164+
const backW = background.convertTo(this._cv.CV_8UC3).cvtColor(this._cv.COLOR_BGRA2BGR).convertTo(this._cv.CV_32FC3).hMul(inverseA);
165+
const res = foreW.add(backW).convertTo(this._cv.CV_8UC3);
166+
return res
164167
} else {
165168
throw(new Error(`invalid channels (${channels})`))
166169
}

lib/generators/gaussian-noise.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class GaussianNoiseGenerator extends AbstractNoise {
4949
return h.round(h.number({
5050
type: 'normal',
5151
mean: this.mean,
52-
sigma: this.sigma
52+
std: this.sigma
5353
}));
5454
}
5555
}

test/generators/gaussian-noise.js

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,17 @@ const test = require('ava');
33
const GaussianNoise = require('../../lib/generators/gaussian-noise');
44
const macroGenerator = require('../macros/generator');
55

6-
const width = 55;
7-
const height = 32;
6+
const width = 59;
7+
const height = 47;
88
const channels = 3;
9-
const mean = 2;
9+
const mean = 128;
10+
const sigma = 50;
1011

1112
test('perspective-transform sigma 4 and transparent', macroGenerator, GaussianNoise, {
1213
debugOutput: path.join(__dirname, '../..', 'tmp/gaussian-noise.png'),
1314
expectImg: (t, img) => {
14-
t.is(img.cols, 55);
15-
t.is(img.rows, 32);
15+
t.is(img.cols, width);
16+
t.is(img.rows, height);
1617
const sum = img.getDataAsArray()
1718
.reduce((a, b) => a.concat(b))
1819
.reduce((a, b) => a.concat(b))
@@ -27,6 +28,7 @@ test('perspective-transform sigma 4 and transparent', macroGenerator, GaussianNo
2728
channels,
2829
options: {
2930
mean,
30-
scale: 0.2
31+
scale: 1,
32+
sigma
3133
}
3234
});

test/macros/generator.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ module.exports = function (t, Cstr, {
1818
if (!debugOutput) {
1919
return Promise.resolve();
2020
}
21-
22-
backend.writeImage(debugOutput, res);
21+
backend.writeImage(debugOutput, res.resize(200,200));
2322
})
2423
.then(() => {
2524
if (!expectImg) {

tmp/gaussian-noise.png

52.2 KB
Loading
620 KB
Loading

0 commit comments

Comments
 (0)