forked from kyamagu/js-segment-annotator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pf-segmentation.js
401 lines (387 loc) · 12.6 KB
/
pf-segmentation.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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/**
* Javascript implementation of an image segmentation algorithm of
*
* Efficient Graph-Based Image Segmentation
* Pedro F. Felzenszwalb and Daniel P. Huttenlocher
* International Journal of Computer Vision, 59(2) September 2004.
*
* API
* ---
*
* PFSegmentation(imageURL, options)
*
* The function takes the following options.
* * `sigma` - Parameter for Gaussian pre-smoothing. Default 0.5.
* * `threshold` - Threshold value of the algorithm. Default 500.
* * `minSize` - Minimum segment size in pixels. Default 20.
* * `toDataURL` - callback function to receive the result as a data URL.
* * `callback` - function to be called on finish. The function takes a single
* argument of result object that contains following fields.
* * `width` - Width of the image in pixels.
* * `height` - Height of the image in pixels.
* * `size` - Number of segments.
* * `indexMap` - Int32Array of `width * height` elements containing
* segment index for each pixel location. The segment index
* at pixel `(i, j)` is `indexMap(i * width + j)`, where
* `i` is the y coordinate of the pixel and `j` is the x
* coordinate.
*
* Example
* -------
*
* Drawing the result to a canvas.
*
* function colorRandomRGB(size, indexMap, imageData) {
* var width = imageData.width;
* var height = imageData.height;
* var rgbData = imageData.data;
* var colormap = new Uint8Array(size * 3);
* for (var i = 0; i < colormap.length; ++i)
* colormap[i] = Math.round(255 * Math.random());
* for (var i = 0; i < height; ++i) {
* for (var j = 0; j < width; ++j) {
* var index = indexMap[i * width + j];
* rgbData[4 * (i * width + j) + 0] = colormap[3 * index + 0];
* rgbData[4 * (i * width + j) + 1] = colormap[3 * index + 1];
* rgbData[4 * (i * width + j) + 2] = colormap[3 * index + 2];
* rgbData[4 * (i * width + j) + 3] = 255;
* }
* }
* }
*
* PFSegmentation('/path/to/image.jpg', {
* sigma: 1.0,
* threshold: 500,
* minSize: 100,
* callback: function(result) {
* var canvas = document.createElement('canvas');
* canvas.width = result.width;
* canvas.height = result.height;
* var context = canvas.getContext('2d');
* var imageData = context.getImageData(0,
* 0,
* canvas.width,
* canvas.height);
* colorRandomRGB(result.size, result.indexMap, imageData);
* context.putImageData(imageData, 0, 0);
* document.body.appendChild(canvas);
* }
* });
*
* Kota Yamaguchi 2013.
*/
(function() {
// Create a normalized Gaussian filter.
function createGaussian(sigma) {
sigma = Math.max(sigma, 0.01);
var length = Math.ceil(sigma * 4) + 1,
mask = new Float32Array(length),
sumValues = 0,
i;
for (i = 0; i < length; ++i) {
var value = Math.exp(-0.5 * Math.pow(i / sigma, 2));
sumValues += Math.abs(value);
mask[i] = value;
}
sumValues = 2 * sumValues - Math.abs(mask[0]); // 2x except center.
for (i = 0; i < length; ++i)
mask[i] /= sumValues;
return mask;
}
// Convolve even.
function convolveEven(imageData, filter) {
var width = imageData.width,
height = imageData.height,
source = imageData.data,
temporary = new Float32Array(source),
i,
j,
k,
l,
sum;
// Horizontal filter.
for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
for (k = 0; k < 3; ++k) {
sum = filter[0] * source[4 * (i * width + j) + k];
for (l = 1; l < filter.length; ++l) {
sum += filter[l] * (
source[4 * (i * width + Math.max(j - l, 0)) + k] +
source[4 * (i * width + Math.min(j + l, width - 1)) + k]
);
}
temporary[4 * (i * width + j) + k] = sum;
}
}
}
// Vertical filter.
for (i = 0; i < height; ++i) {
for (j = 0; j < width; ++j) {
for (k = 0; k < Math.min(4, 3); ++k) {
sum = filter[0] * temporary[4 * (i * width + j) + k];
for (l = 1; l < filter.length; ++l) {
sum += filter[l] * (
temporary[4 * (Math.max(i - l, 0) * width + j) + k] +
temporary[4 * (Math.min(i + l, height - 1) * width + j) + k]
);
}
source[4 * (i * width + j) + k] = sum;
}
}
}
}
// Smooth an image.
function smoothImage(imageData, sigma) {
var gaussian = createGaussian(sigma);
convolveEven(imageData, gaussian);
}
// Create an edge structure.
function createEdges(imageData, options) {
var width = imageData.width,
height = imageData.height,
rgbData = imageData.data,
edgeSize = 4 * width * height - 3 * width - 3 * height + 2,
index = 0,
edges = {
a: new Int32Array(edgeSize),
b: new Int32Array(edgeSize),
w: new Float32Array(edgeSize)
},
x1,
x2;
for (var i = 0; i < height; ++i) {
for (var j = 0; j < width; ++j) {
if (j < width - 1) {
x1 = i * width + j;
x2 = i * width + j + 1;
edges.a[index] = x1;
edges.b[index] = x2;
x1 = 4 * x1;
x2 = 4 * x2;
edges.w[index] = Math.sqrt(
Math.pow(rgbData[x1 + 0] - rgbData[x2 + 0], 2) +
Math.pow(rgbData[x1 + 1] - rgbData[x2 + 1], 2) +
Math.pow(rgbData[x1 + 2] - rgbData[x2 + 2], 2)
);
++index;
}
if (i < height - 1) {
x1 = i * width + j;
x2 = (i + 1) * width + j;
edges.a[index] = x1;
edges.b[index] = x2;
x1 = 4 * x1;
x2 = 4 * x2;
edges.w[index] = Math.sqrt(
Math.pow(rgbData[x1 + 0] - rgbData[x2 + 0], 2) +
Math.pow(rgbData[x1 + 1] - rgbData[x2 + 1], 2) +
Math.pow(rgbData[x1 + 2] - rgbData[x2 + 2], 2)
);
++index;
}
if ((j < width - 1) && (i < height - 1)) {
x1 = i * width + j;
x2 = (i + 1) * width + j + 1;
edges.a[index] = x1;
edges.b[index] = x2;
x1 = 4 * x1;
x2 = 4 * x2;
edges.w[index] = Math.sqrt(
Math.pow(rgbData[x1 + 0] - rgbData[x2 + 0], 2) +
Math.pow(rgbData[x1 + 1] - rgbData[x2 + 1], 2) +
Math.pow(rgbData[x1 + 2] - rgbData[x2 + 2], 2)
);
++index;
}
if ((j < width - 1) && (i > 0)) {
x1 = i * width + j;
x2 = (i - 1) * width + j + 1;
edges.a[index] = x1;
edges.b[index] = x2;
x1 = 4 * x1;
x2 = 4 * x2;
edges.w[index] = Math.sqrt(
Math.pow(rgbData[x1 + 0] - rgbData[x2 + 0], 2) +
Math.pow(rgbData[x1 + 1] - rgbData[x2 + 1], 2) +
Math.pow(rgbData[x1 + 2] - rgbData[x2 + 2], 2)
);
++index;
}
}
}
return edges;
}
// Sort edges.
function sortEdgesByWeights(edges) {
var order = new Array(edges.w.length),
i;
for (i = 0; i < order.length; ++i)
order[i] = i;
var a = edges.a,
b = edges.b,
w = edges.w;
order.sort(function(i, j) { return w[i] - w[j]; });
var temporaryA = new Uint32Array(a),
temporaryB = new Uint32Array(b),
temporaryW = new Float32Array(w);
for (i = 0; i < order.length; ++i) {
temporaryA[i] = a[order[i]];
temporaryB[i] = b[order[i]];
temporaryW[i] = w[order[i]];
}
edges.a = temporaryA;
edges.b = temporaryB;
edges.w = temporaryW;
}
// Create a universe struct.
function createUniverse(nodes, c) {
var universe = {
nodes: nodes,
rank: new Int32Array(nodes),
p: new Int32Array(nodes),
size: new Int32Array(nodes),
threshold: new Float32Array(nodes)
};
for (var i = 0; i < nodes; ++i) {
universe.size[i] = 1;
universe.p[i] = i;
universe.threshold[i] = c;
}
return universe;
}
// Find a vertex pointing self.
function findNode(universe, index) {
var i = index;
while (i !== universe.p[i])
i = universe.p[i];
universe.p[index] = i;
return i;
}
// Join a node.
function joinNode(universe, a, b) {
if (universe.rank[a] > universe.rank[b]) {
universe.p[b] = a;
universe.size[a] += universe.size[b];
}
else {
universe.p[a] = b;
universe.size[b] += universe.size[a];
if (universe.rank[a] == universe.rank[b])
universe.rank[b]++;
}
universe.nodes--;
}
// Segment a graph.
function segmentGraph(imageData, options) {
var c = options.threshold,
minSize = options.minSize,
edges = createEdges(imageData, options),
a,
b,
i;
sortEdgesByWeights(edges);
var universe = createUniverse(imageData.width * imageData.height, c);
// Bottom-up merge.
for (i = 0; i < edges.a.length; ++i) {
a = findNode(universe, edges.a[i]);
b = findNode(universe, edges.b[i]);
if (a != b &&
edges.w[i] <= universe.threshold[a] &&
edges.w[i] <= universe.threshold[b]) {
joinNode(universe, a, b);
a = findNode(universe, a);
universe.threshold[a] = edges.w[i] + (c / universe.size[a]);
}
}
// Merge small components.
for (i = 0; i < edges.a.length; ++i) {
a = findNode(universe, edges.a[i]);
b = findNode(universe, edges.b[i]);
if (a != b &&
(universe.size[a] < minSize || universe.size[b] < minSize))
joinNode(universe, a, b);
}
return universe;
}
// Create an index map.
function createIndexMap(universe, imageData, options) {
var width = imageData.width,
height = imageData.height,
indexMap = new Int32Array(width * height),
nodeIds = [],
lastId = 0;
for (var i = 0; i < height; ++i) {
for (var j = 0; j < width; ++j) {
var component = findNode(universe, i * width + j),
index = nodeIds[component];
if (index === undefined) {
index = lastId;
nodeIds[component] = lastId++;
}
indexMap[i * width + j] = index;
}
}
return indexMap;
}
// Compute segmentation.
function computeSegmentation(imageData, options) {
smoothImage(imageData, options.sigma);
var universe = segmentGraph(imageData, options),
indexMap = createIndexMap(universe, imageData, options);
if (options.callback) {
var rgbData = new Uint8Array(imageData.data);
options.callback({
width: imageData.width,
height: imageData.height,
size: universe.nodes,
indexMap: indexMap,
rgbData: rgbData
});
}
if (options.toDataURL)
getDataURL(imageData.width, imageData.height, indexMap, options);
}
// Convert to Data URL.
function getDataURL(width, height, indexMap, options) {
var canvas = document.createElement('canvas');
canvas.width = width;
canvas.height = height;
var context = canvas.getContext('2d'),
imageData = context.createImageData(width, height),
data = imageData.data;
for (var i = 0; i < indexMap.length; ++i) {
var value = indexMap[i];
data[4 * i + 0] = value & 255;
data[4 * i + 1] = (value >>> 8) & 255;
data[4 * i + 2] = (value >>> 16) & 255;
}
context.putImageData(imageData, 0, 0);
options.toDataURL(canvas.toDataURL());
}
// When image is loaded.
function onSuccessImageLoad(image, options) {
var canvas = document.createElement('canvas');
canvas.width = image.width;
canvas.height = image.height;
var context = canvas.getContext('2d');
context.drawImage(image, 0, 0);
var imageData = context.getImageData(0, 0, image.width, image.height),
segmentation = computeSegmentation(imageData, options);
}
// When image is invalid.
function onErrorImageLoad() {
alert('Failed to load an image: ' + image.src);
}
// Public API.
window.PFSegmentation = function(imageURL, options) {
if (typeof options === 'undefined') options = {};
if (options.sigma === undefined) options.sigma = 0.5;
if (options.threshold === undefined) options.threshold = 500;
if (options.minSize === undefined) options.minSize = 20;
var image = new Image();
image.src = imageURL;
image.crossOrigin = null;
image.onerror = function() { onErrorImageLoad(image); };
image.onload = function() { onSuccessImageLoad(image, options); };
};
}).call(this);