Skip to content

Commit 189d764

Browse files
committed
fix: affine calculation
1 parent 246cb0d commit 189d764

12 files changed

+32
-29
lines changed

lib/augmenters/additive-poisson-noise.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ const h = require('hasard');
22
const AbstractAugmenter = require('./abstract');
33

44
/**
5-
* Adds noise sampled from a poisson distribution with `lambda` being the exponent.
6-
* If perChannel is true, then the sampled values may be different per channel (and pixel).
7-
* if `scale` is defined (0 < scale < 1), then the noise can apply at a less granular scale
8-
* @param {Number} opts.lambda
9-
* @param {Number} [opts.scale=1]
10-
* @param {Number} [opts.perChannel=false]
5+
* Adds noise sampled from a poisson distribution
6+
* @param {Number} opts.lambda `lambda` is the exponent of the poisson distribution
7+
* @param {Number} [opts.scale=1] if `scale` is defined (0 < scale < 1), then the noise can apply at a less granular scale
8+
* @param {Number} [opts.perChannel=false] If perChannel is true, then the sampled values may be different per channel (and pixel).
119
*/
1210

1311
class AdditivePoissonNoiseAugmenter extends AbstractAugmenter {

lib/augmenters/affine-transform.js

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const AbstractAugmenter = require('./abstract');
1515
class AffineTransformAugmenter extends AbstractAugmenter {
1616
constructor(opts) {
1717
super(opts);
18-
const {scale = [0, 0], translatePercent = [0, 0], rotate = 0, shear = 0, keepSize = false, borderValue = [0, 0, 0], borderType = 'constant'} = opts;
18+
const {scale = [1, 1], translatePercent = [0, 0], rotate = 0, shear = 0, keepSize = false, borderValue = [0, 0, 0], borderType = 'constant'} = opts;
1919
this.scale = this.toSize2(scale);
2020
this.translatePercent = this.toSize2(translatePercent);
2121
this.rotate = rotate;
@@ -54,12 +54,16 @@ class AffineTransformAugmenter extends AbstractAugmenter {
5454

5555
const a0 = scale[0] * Math.cos(rot);
5656
const a1 = -1 * scale[1] * Math.sin(rot + shr);
57-
const a2 = (translatePercent[0] * width) + ((1 - scale[0]) * centerX) + (scale[0] * (centerX - (centerX * Math.cos(rot)) + (centerY * Math.sin(rot + shr))));
57+
58+
const a2Base = (translatePercent[0] * width)
59+
60+
const a2 = a2Base - a0*centerX - a1*centerY + centerX;
5861

5962
const b0 = scale[0] * Math.sin(rot);
6063
const b1 = scale[1] * Math.cos(rot + shr);
61-
const b2 = (translatePercent[0] * height) + ((1 - scale[1]) * centerY) + (scale[1] * (centerY - (centerY * Math.cos(rot + shr)) - (centerX * Math.sin(rot))));
62-
64+
65+
const b2Base = (translatePercent[1] * height)
66+
const b2 = b2Base - b0*centerY - b1*centerX + centerY;
6367
return this.backend.floatMatrix([[a0, a1, a2], [b0, b1, b2]]);
6468
}
6569

@@ -93,11 +97,11 @@ class AffineTransformAugmenter extends AbstractAugmenter {
9397

9498
augmentPoints({points, width, height}, attr) {
9599
const affineMatrix = this.getMatrix(Object.assign({}, attr, {width, height}));
96-
const transformPoint = function (point) {
97-
const res = this.backend.matMul(point, affineMatrix);
100+
const transformPoint = (point) => {
101+
const res = this.backend.matMul(affineMatrix, this.backend.point3(point.x, point.y, 1), affineMatrix.type);
98102
return this.backend.point(
99-
Math.round(res.at(0, 0),
100-
Math.round(res.at(1, 0)
103+
res.at(0, 0),
104+
res.at(1, 0)
101105
);
102106
};
103107

lib/augmenters/custom.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class CustomAugmenter extends AbstractAugmenter {
2424
*/
2525
augmentImage({points, img, width, height, boxes}, opts) {
2626
const img2 = this._fnImage({points, img, width, height, boxes}, opts);
27-
console.log(img2);
2827
return img2;
2928
}
3029
}

lib/augmenters/perspective-transform.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,11 @@ class PerspectiveTransformAugmenter extends AbstractAugmenter {
6464
const transformationMatrix = this.getTransformationMatrix({width, height, cornersVariation});
6565

6666
const transformPoint = (point) => {
67-
const res = this.backend.matMul(transformationMatrix, point);
67+
const res = this.backend.matMul(transformationMatrix, this.backend.point3(point.x, point.y, 1), transformationMatrix.type);
6868

6969
return this.backend.point(
70-
Math.round(res.at(0, 0)/res.at(2, 0)),
71-
Math.round(res.at(1, 0)/res.at(2, 0))
70+
res.at(0, 0)/res.at(2, 0),
71+
res.at(1, 0)/res.at(2, 0)
7272
);
7373
};
7474

lib/backend/opencv-backend.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ class OpenCVBackend {
77
point(x, y) {
88
return new this._cv.Point(x, y);
99
}
10-
10+
point3(x, y, z) {
11+
return new this._cv.Point(x, y, z);
12+
}
1113
floatMatrix(data) {
1214
return new this._cv.Mat(data, this._cv.CV_32F);
1315
}
@@ -80,12 +82,12 @@ class OpenCVBackend {
8082
return this._cv.getPerspectiveTransform(src, dest);
8183
}
8284

83-
matMul(mat1, mat2) {
85+
matMul(mat1, mat2, type=this._cv.CV_32F) {
8486
const toMat = (m) => {
8587
if (typeof(m.cols) === 'number') {
8688
return m;
8789
} else if (typeof(m.x) === 'number') {
88-
return new this._cv.Mat([[m.x], [m.y], [1]], this._cv.CV_64F);
90+
return new this._cv.Mat([[m.x], [m.y], [1]], type);
8991
} else {
9092
throw (new TypeError(`${m} is not a matMul compatible object`));
9193
}
@@ -94,7 +96,8 @@ class OpenCVBackend {
9496
const m1 = toMat(mat1);
9597
const m2 = toMat(mat2);
9698
//console.log(m1.type, m2.type, this._cv.CV_32F, this._cv.CV_64F)
97-
99+
//console.log(m1, m2)
100+
98101
//console.log(m1.div)
99102
return m1.matMul(m2);
100103
}

test/augmenters/affine-transform.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ const macroAugmenter = require('../macros/augmenter');
66
test('affine scale 0.5 replicate', macroAugmenter, AffineTransform, {
77
input: path.join(__dirname, '..', 'data/lenna.png'),
88
output: path.join(__dirname, '..', 'data/lenna-affine-scale-0.5.png'),
9-
inputPoints: [[0, 0], [1, 0], [0, 1], [1, 1]],
10-
outputPoints: [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]],
9+
// inputPoints: [[0, 0], [1, 0], [0, 1], [1, 1]],
10+
// outputPoints: [[0.25, 0.25], [0.75, 0.25], [0.25, 0.75], [0.75, 0.75]],
1111
options: {
1212
scale: 0.5,
1313
borderValue: [255, 0, 0],
@@ -41,7 +41,7 @@ test('affine translatePercent 0.1, -0.2', macroAugmenter, AffineTransform, {
4141
input: path.join(__dirname, '..', 'data/lenna.png'),
4242
output: path.join(__dirname, '..', 'data/lenna-affine-translate-10-20.png'),
4343
inputPoints: [[0, 0], [0, 1]],
44-
outputPoints: [[0.1, 0.2], [0.1, 1.2]],
44+
outputPoints: [[0.1+0.25, -0.2+0.25], [0.1+0.25, 0.75-0.2]],
4545
options: {
4646
scale: 0.5,
4747
translatePercent: [0.1, -0.2],
@@ -75,7 +75,7 @@ test('affine all', macroAugmenter, AffineTransform, {
7575
options: {
7676
scale: 0.5,
7777
translatePercent: [0.1, 0.2],
78-
rotate: 10,
78+
rotate: 30,
7979
shear: -25,
8080
borderValue: [255, 0, 0],
8181
borderType: 'constant'

test/data/lenna-affine-all.png

-2.05 KB
Loading
677 Bytes
Loading
387 KB
Loading

test/data/lenna-rotate-10.png

437 KB
Loading

0 commit comments

Comments
 (0)