Skip to content

Commit a09d8bb

Browse files
committed
fix: rm custom, add doc
1 parent 70a3f30 commit a09d8bb

File tree

6 files changed

+99
-61
lines changed

6 files changed

+99
-61
lines changed

lib/augmenters/crop.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,31 @@ const AbstractAugmenter = require('./abstract');
99
*/
1010

1111
/**
12-
* Add blur to the image
12+
* Crop the image
1313
* @param {Object} opts options
1414
* @param {ArraylRTB} opts.percent percent of cropping to do
15+
* @example
16+
// Simple usage, crop by 10%, on every side
17+
new ia.Crop(0.1);
18+
* @example
19+
//Simple usage with random variable crop by 10% to 30%
20+
new ia.Crop(h.number(0.1, 0.3));
21+
* @example
22+
//Crop by 10% on left, 20% on top, to 30% on the right, 10% on the bottom
23+
new ia.Crop([0.1, 0.2, 0.3, 0.1]);
1524
*/
1625

1726
class CropAugmenter extends AbstractAugmenter {
1827
constructor(opts) {
19-
super(opts);
20-
const {percent = 0} = opts;
28+
let o;
29+
if (typeof (opts) === 'number' || Array.isArray(opts) || hasard.isHasard(opts)) {
30+
o = {percent: opts};
31+
} else {
32+
o = opts;
33+
}
34+
35+
super(o);
36+
const {percent} = o;
2137
this.percent = this.toSize4(percent);
2238
this.percent.forEach(p => {
2339
if (p < 0) {

lib/augmenters/custom.js

Lines changed: 0 additions & 25 deletions
This file was deleted.

lib/augmenters/pad.js

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,35 @@ const AbstractAugmenter = require('./abstract');
77
* @param {ArraylRTB} opts.percent size of padding in percent
88
* @param {ColorArgument} [opts.borderValue=[0,0,0]] if borderType is "constant" this is used as border pixel values
99
* @param {BorderTypeArgument} [opts.borderType="constant"] "constant", "replicate", "transparent"
10+
* @example
11+
// Simple usage, pad by 10%, on every side
12+
new ia.Pad(0.1);
13+
* @example
14+
// pad by 10%, with transparent background
15+
new ia.Pad({
16+
percent: 0.1,
17+
borderType: "transparent"
18+
});
19+
* @example
20+
// pad by 10% (left, right), 20% (top, bottom), with constant blue background
21+
new ia.Pad({
22+
percent: [0.1, 0.2],
23+
borderType: "transparent",
24+
borderValue: [0, 0, 255]
25+
});
1026
*/
1127

1228
class PadAugmenter extends AbstractAugmenter {
1329
constructor(opts) {
14-
super(opts);
15-
const {borderValue = [0, 0, 0], borderType = 'replicate', percent = 0} = opts;
30+
let o;
31+
if (typeof (opts) === 'number' || Array.isArray(opts) || hasard.isHasard(opts)) {
32+
o = {percent: opts};
33+
} else {
34+
o = opts;
35+
}
36+
37+
super(o);
38+
const {borderValue = [0, 0, 0], borderType = 'replicate', percent = 0} = o;
1639
this.percent = this.toSize4(percent);
1740
this.percent.forEach(p => {
1841
if (p < 0) {

lib/augmenters/perspective-transform.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,34 @@ const AbstractAugmenter = require('./abstract');
33
/**
44
* Applies a random four-point perspective transform to the image (kinda like an advanced form of cropping).
55
* Each point has a random distance from the image corner, derived from a normal distribution with sigma `sigma`.
6-
* @param {Object} opts options
7-
* @param {NumberArgument} opts.sigma the sigma of the normal distribution
8-
* @param {BooleanArgument} opts.keepSize If `keepSize` is set to True (default), each image will be resized back to its original size.
9-
* @param {ColorArgument} [opts.borderValue=[0,0,0]] if borderType is "constant" this is used as border pixel values
10-
* @param {BorderTypeArgument} [opts.borderType="constant"] can be "constant", "replicate", "transparent"
11-
* @param {Array.<Array.<Number>>} [opts.cornersVariation=null] if set, sigma is not used. For more deterministic behavior, use this to set manually the percent (x,y) variation of each corner
6+
* @param {Object} sigma options
7+
* @param {NumberArgument} sigma.sigma the sigma of the normal distribution
8+
* @param {BooleanArgument} sigma.keepSize If `keepSize` is set to True (default), each image will be resized back to its original size.
9+
* @param {ColorArgument} [sigma.borderValue=[0,0,0]] if borderType is "constant" this is used as border pixel values
10+
* @param {BorderTypeArgument} [sigma.borderType="constant"] can be "constant", "replicate", "transparent"
11+
* @param {Array.<Array.<Number>>} [sigma.cornersVariation=null] if set, sigma is not used. For more deterministic behavior, use this to set manually the percent (x,y) variation of each corner
12+
* @example
13+
// Simple usage, perspective transform on a sigma=10% random perspective tarnsform
14+
new ia.PerspectiveTransform(0.1);
15+
* @example
16+
// Now replicate the borders
17+
new ia.PerspectiveTransform({
18+
sigma: 0.1,
19+
borderType: "replicate"
20+
});
1221
*/
1322

1423
class PerspectiveTransformAugmenter extends AbstractAugmenter {
1524
constructor(opts) {
16-
super(opts);
17-
const {sigma, keepSize, borderValue = [0, 0, 0], borderType = 'constant', cornersVariation} = opts;
25+
let o;
26+
if (typeof (opts) === 'number' || hasard.isHasard(opts)) {
27+
o = {sigma: opts};
28+
} else {
29+
o = opts;
30+
}
31+
32+
super(o);
33+
const {sigma, keepSize, borderValue = [0, 0, 0], borderType = 'constant', cornersVariation} = o;
1834
this.sigma = this.toSize2(sigma) || [0, 0];
1935
this.keepSize = keepSize;
2036
this.borderValue = borderValue;

lib/augmenters/resize.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,28 @@ const AbstractAugmenter = require('./abstract');
55
* Resize the image
66
* @param {Object} opts options
77
* @param {ArrayXY} opts.size size of the result image
8+
* @example
9+
// Simple usage, resize to 100x100 square
10+
new ia.Resize(100);
11+
* @example
12+
// Simple usage, resize to 100x50 square
13+
new ia.Resize([100, 50]);
14+
* @example
15+
// Explicit form
16+
new ia.Resize({size: [100, 50]});
817
*/
918

1019
class ResizeAugmenter extends AbstractAugmenter {
1120
constructor(opts) {
12-
super(opts);
13-
const {size} = opts;
21+
let o;
22+
if (typeof (opts) === 'number' || Array.isArray(opts) || hasard.isHasard(opts)) {
23+
o = {size: opts};
24+
} else {
25+
o = opts;
26+
}
27+
28+
super(o);
29+
const {size} = o;
1430
this.size = this.toSize2(size);
1531
}
1632

lib/augmenters/sequential.js

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ const AbstractAugmenter = require('./abstract');
55
* Build a sequence of actions
66
* @param {Object} opts options
77
* @param {Array.<ImageAugment> | Hasard.<Array.<ImageAugment>>} opts.steps steps to be run in sequence
8+
* @example
9+
* // Simple usage
10+
* const img = cv.imread('lenna.jpg');
11+
* const augmenter = new ia.Sequential([
12+
* new ia.Blur(2),
13+
* new ia.PerspectiveTransform(0.2)
14+
* ]);
15+
* const {image} = augmenter.runOnce({image: img});
816
*/
917

1018
class Sequential extends AbstractAugmenter {
@@ -17,28 +25,12 @@ class Sequential extends AbstractAugmenter {
1725
}
1826
}
1927

20-
runOnce({img, points = [], boxes = []}) {
21-
let steps;
22-
if (hasard.isHasard(this.steps)) {
23-
steps = this.steps.runOnce();
24-
} else {
25-
steps = this.steps;
26-
}
27-
28-
if (!Array.isArray(steps)) {
29-
throw (new TypeError(`${steps} should be an array`));
30-
}
31-
32-
const width = img.cols;
33-
const height = img.rows;
34-
35-
let res = {img, points, width, height, boxes};
36-
37-
steps.forEach(step => {
38-
res = step.runOnce(res);
28+
runOnce(o, runOpts) {
29+
let current = o;
30+
this.steps.forEach(s => {
31+
current = s.runOnce(current, runOpts);
3932
});
40-
41-
return res;
33+
return current;
4234
}
4335
}
4436
module.exports = Sequential;

0 commit comments

Comments
 (0)