/
erode.js
212 lines (198 loc) · 5.6 KB
/
erode.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import Image from '../Image';
/**
* Erosion is one of two fundamental operations (with dilatation) in morphological
* image processing from which all other morphological operations are based (from Wikipedia).
* Replaces each value with it's local minimum among the pixels with a kernel value of 1.
* http://docs.opencv.org/2.4/doc/tutorials/imgproc/erosion_dilatation/erosion_dilatation.html
* https://en.wikipedia.org/wiki/Erosion_(morphology)
* @memberof Image
* @instance
* @param {object} [options]
* @param {Array<Array<number>>} [options.kernel] - The kernel can only have ones and zeros. Default: [[1, 1, 1], [1, 1, 1], [1, 1, 1]]
* @param {number} [options.iterations=1] - The number of successive erosions
* @return {Image}
*/
export default function erode(options = {}) {
let {
kernel = [
[1, 1, 1],
[1, 1, 1],
[1, 1, 1],
],
iterations = 1,
} = options;
this.checkProcessable('erode', {
bitDepth: [1, 8, 16],
components: 1,
alpha: 0,
});
if (kernel.columns % 2 === 0 || kernel.rows % 2 === 0) {
throw new TypeError(
'erode: The number of rows and columns of the kernel must be odd',
);
}
let onlyOnes = true;
outer: for (const row of kernel) {
for (const value of row) {
if (value !== 1) {
onlyOnes = false;
break outer;
}
}
}
let result = this;
for (let i = 0; i < iterations; i++) {
if (this.bitDepth === 1) {
if (onlyOnes) {
const newImage = result.clone();
result = erodeOnceBinaryOnlyOnes(
result,
newImage,
kernel.length,
kernel[0].length,
);
} else {
const newImage = Image.createFrom(result);
result = erodeOnceBinary(result, newImage, kernel);
}
} else if (onlyOnes) {
const newImage = Image.createFrom(result);
result = erodeOnceGreyOnlyOnes(
result,
newImage,
kernel.length,
kernel[0].length,
);
} else {
const newImage = Image.createFrom(result);
result = erodeOnceGrey(result, newImage, kernel);
}
}
return result;
}
function erodeOnceGrey(img, newImage, kernel) {
const kernelWidth = kernel.length;
const kernelHeight = kernel[0].length;
let radiusX = (kernelWidth - 1) / 2;
let radiusY = (kernelHeight - 1) / 2;
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
let min = img.maxValue;
for (let jj = 0; jj < kernelHeight; jj++) {
for (let ii = 0; ii < kernelWidth; ii++) {
if (kernel[ii][jj] !== 1) continue;
let i = ii - radiusX + x;
let j = jj - radiusY + y;
if (i < 0 || j < 0 || i >= img.width || j >= img.height) continue;
const value = img.getValueXY(i, j, 0);
if (value < min) min = value;
}
}
newImage.setValueXY(x, y, 0, min);
}
}
return newImage;
}
function erodeOnceGreyOnlyOnes(img, newImage, kernelWidth, kernelHeight) {
const radiusX = (kernelWidth - 1) / 2;
const radiusY = (kernelHeight - 1) / 2;
const minList = [];
for (let x = 0; x < img.width; x++) {
minList.push(0);
}
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
let min = img.maxValue;
for (
let h = Math.max(0, y - radiusY);
h < Math.min(img.height, y + radiusY + 1);
h++
) {
const value = img.getValueXY(x, h, 0);
if (value < min) {
min = value;
}
}
minList[x] = min;
}
for (let x = 0; x < img.width; x++) {
let min = img.maxValue;
for (
let i = Math.max(0, x - radiusX);
i < Math.min(img.width, x + radiusX + 1);
i++
) {
if (minList[i] < min) {
min = minList[i];
}
}
newImage.setValueXY(x, y, 0, min);
}
}
return newImage;
}
function erodeOnceBinary(img, newImage, kernel) {
const kernelWidth = kernel.length;
const kernelHeight = kernel[0].length;
let radiusX = (kernelWidth - 1) / 2;
let radiusY = (kernelHeight - 1) / 2;
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
let min = 1;
intLoop: for (let jj = 0; jj < kernelHeight; jj++) {
for (let ii = 0; ii < kernelWidth; ii++) {
if (kernel[ii][jj] !== 1) continue;
let i = ii - radiusX + x;
let j = jj - radiusY + y;
if (j < 0 || i < 0 || i >= img.width || j >= img.height) continue;
const value = img.getBitXY(i, j);
if (value === 0) {
min = 0;
break intLoop;
}
}
}
if (min === 1) {
newImage.setBitXY(x, y);
}
}
}
return newImage;
}
function erodeOnceBinaryOnlyOnes(img, newImage, kernelWidth, kernelHeight) {
const radiusX = (kernelWidth - 1) / 2;
const radiusY = (kernelHeight - 1) / 2;
const minList = [];
for (let x = 0; x < img.width; x++) {
minList.push(0);
}
for (let y = 0; y < img.height; y++) {
for (let x = 0; x < img.width; x++) {
minList[x] = 1;
for (
let h = Math.max(0, y - radiusY);
h < Math.min(img.height, y + radiusY + 1);
h++
) {
if (img.getBitXY(x, h) === 0) {
minList[x] = 0;
break;
}
}
}
for (let x = 0; x < img.width; x++) {
if (newImage.getBitXY(x, y) === 0) continue;
for (
let i = Math.max(0, x - radiusX);
i < Math.min(img.width, x + radiusX + 1);
i++
) {
if (minList[i] === 0) {
newImage.clearBitXY(x, y);
break;
}
}
}
}
return newImage;
}