-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
420 lines (392 loc) · 9.7 KB
/
index.html
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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
<!DOCTYPE html>
<html lang="en">
<head>
<title>Image Segmentation</title>
<link rel="icon" type="image/x-icon" href="favicon.png" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width" />
<script type="text/javascript">
const utf8_decoder = new TextDecoder();
function c_strlen(mem, ptr) {
let len = 0;
while (mem[ptr++]) len++;
return len;
}
function c_string(buffer, ptr) {
const mem = new Uint8Array(buffer);
const len = c_strlen(mem, ptr);
const bytes = new Uint8Array(buffer, ptr, len);
return utf8_decoder.decode(bytes);
}
function puts(ptr) {
console.log(c_string(wasm.memory.buffer, ptr));
}
function random_int() {
return parseInt(Math.random() * (1 << 31));
}
if (!WebAssembly.instantiateStreaming) {
WebAssembly.instantiateStreaming = (promise, env) =>
promise
.then((r) => r.arrayBuffer())
.then((bytes) => WebAssembly.instantiate(bytes, env));
}
const wasmFetch = (file) =>
WebAssembly.instantiateStreaming(fetch(file), {
env: {
puts,
random_int,
},
});
const wasmStatus = new Promise((resolve, reject) => {
wasmFetch("main.wasm")
.then((w) => {
window.wasm_ref = w;
window.wasm = w.instance.exports;
window.isSlow = false;
resolve();
})
.catch(() => {
wasmFetch("main-slow.wasm")
.then((w) => {
window.wasm_ref = w;
window.wasm = w.instance.exports;
window.isSlow = true;
resolve();
})
.catch((err) => {
console.error(err);
reject();
});
});
});
</script>
<style>
body {
background-color: beige;
}
input {
font-family: inherit;
}
.alg {
display: flex;
flex-direction: column;
align-items: center;
}
.alg h2 {
margin-bottom: 5px;
}
.alg div {
margin: 5px;
}
.alg canvas,
.alg img {
border: 5px solid black;
border-radius: 10px;
margin: 10px;
}
.row {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
}
.alert {
padding: 20px;
background-color: #ff9800;
color: white;
margin-bottom: 15px;
display: none;
justify-content: center;
font-size: 2em;
border: 2px solid #965a00;
text-align: center;
position: sticky;
top: 0px;
}
@media (max-width: 786px) {
.alert {
font-size: 1em;
padding: 10px;
}
}
.view-code {
padding: 10px;
background-color: beige;
color: white;
margin-bottom: 5px;
font-size: 1em;
border: 2px solid #0d0582;
position: fixed;
bottom: 0;
display: flex;
}
img {
width: min(400px, 80vw);
height: auto;
}
</style>
</head>
<body>
<div id="warn" class="alert">
Because this browser does not support the optimized build,
computation time may be slower than expected.
</div>
<h1 style="text-align: center; margin-top: 20px">
Image Segmentation Methods Comparison
</h1>
<div class="row" style="justify-content: space-evenly">
<div style="display: flex; flex-direction: column">
<div class="alg" id="source">
<h2>Source</h2>
<div>
<label> Source: <input type="file" /> </label>
</div>
<img
src="source.webp"
width="400"
height="268"
alt="source"
/>
</div>
<div style="width: min(400px, 80vw)">
<p>
<em>
Collection of image segmentation techniques written
in C and compiled to WebAssembly. Computation time
in parentheses.
</em>
</p>
<p>
Digital images use gamma encodings to store luminance.
Gamma correction can make image luminance appear more
realistic.
<label>
<input
type="checkbox"
id="gamma"
name="gamma"
checked="true"
/>
use gamma correction
</label>
</p>
<p>
<b>K-Means:</b> A randomized clustering algorithm to
group pixels into K groups based on Euclidean distance
between RGB values. A higher K leads to more detailed
results.
</p>
<p>
<b>Mean Shift:</b> A hill climbing algorithm to
iteratively find local maxima. The kernel size controls
the sensitivity to smaller modes. The least two
significant bits are dropped from each color component.
</p>
<p>
<b>Split & Merge:</b> A two stage algorithm to split an
image into homogenous regions (by subdividing
nonhomogeneous blocks into quadrants) and then merge
similar neighboring blocks into groups. The tolerance
controls the threshold at which blocks are deemed
homogenous.
</p>
<p>
<b>Balanced Histogram Thresholding:</b> An automatic
thresholding method that divides the color space by
finding the midpoint of an RGB histogram.
</p>
</div>
</div>
<div
style="
display: flex;
flex-direction: column;
justify-content: space-evenly;
align-items: center;
"
>
<div class="row">
<div class="alg" id="kmeans">
<h2>K-Means</h2>
<div>
<label>
K:
<input
type="number"
value="3"
min="2"
max="25"
/>
</label>
<input
type="number"
value="200"
style="display: none"
/>
</div>
<canvas></canvas>
</div>
<div class="alg" id="meanshift">
<h2>Mean Shift</h2>
<div>
<label>
Kernel Size:
<input
type="number"
value="4"
min="1"
max="25"
/>
</label>
<input
type="number"
value="100"
style="display: none"
/>
</div>
<canvas></canvas>
</div>
</div>
<div class="row">
<div class="alg" id="split_and_merge">
<h2>Split & Merge</h2>
<div>
<label>
Tolerance:
<input
type="number"
value="200"
min="50"
max="350"
step="10"
/>
</label>
</div>
<canvas></canvas>
</div>
<div class="alg" id="histogram">
<h2>Balanced Histogram Thresholding</h2>
<div>
<label>no parameters</label>
</div>
<canvas style="margin-top: 13px"></canvas>
</div>
</div>
</div>
</div>
<div class="view-code">
<a
href="https://github.com/inventshah/image-segmentation"
target="_blank"
>
view code
</a>
</div>
<script type="text/javascript">
function load(source) {
for (const elm of document.getElementsByTagName("canvas")) {
if (
elm.width === source.width &&
elm.height === source.height
)
continue;
elm.width = source.width;
elm.height = source.height;
run_alg(elm.parentElement.id);
}
}
const source = document.querySelector("img");
source.onload = () => {
window.width = parseInt(source.width);
window.height = parseInt(source.height);
load(source);
};
window.addEventListener("resize", () => {
window.width = parseInt(source.width);
window.height = parseInt(source.height);
load(source);
});
function run_alg(alg) {
if (window.wasm == undefined || window.width == undefined)
return;
const container = document.getElementById(alg);
const canvas = container.getElementsByTagName("canvas")[0];
const ctx = canvas.getContext("2d");
ctx.drawImage(source, 0, 0, width, height);
const args = [...container.getElementsByTagName("input")].map(
(elm) => +elm.value
);
const img_ptr = wasm.malloc(width * height * 4);
const data = ctx.getImageData(0, 0, width, height).data;
const bytes = new Uint8ClampedArray(
wasm.memory.buffer,
img_ptr,
width * height * 4
);
bytes.set(data);
const title = container.getElementsByTagName("h2")[0].innerText;
const name = title.replace(/ \(.*\)$/, "");
try {
const start = performance.now();
wasm[alg](img_ptr, width, height, ...args);
wasm.memory_reset();
const end = performance.now();
container.getElementsByTagName(
"h2"
)[0].innerHTML = `${name} <span style="font-size: .8em"> <em> (${Math.round(
end - start
)} ms)</em> </span>`;
} catch (err) {
container.getElementsByTagName(
"h2"
)[0].innerText = `${name} (error)`;
console.error(err);
}
const imageData = ctx.getImageData(0, 0, width, height);
imageData.data.set(
new Uint8ClampedArray(
wasm.memory.buffer,
img_ptr,
width * height * 4
)
);
ctx.putImageData(imageData, 0, 0);
}
function* alg_iter() {
for (const elm of document.getElementsByClassName("alg")) {
if (elm.id == "source") continue;
yield elm;
}
}
document.getElementById("gamma").onchange = (e) => {
wasm.change_gamma_status(e.target.checked ? 1 : 0);
for (const elm of alg_iter()) run_alg(elm.id);
};
document.querySelector('input[type="file"]').onchange = (e) => {
console.log(e.target.files);
if (!e.target.files) return;
source.src = URL.createObjectURL(e.target.files[0]);
};
for (const elm of alg_iter()) {
for (const input of elm.getElementsByTagName("input")) {
input.onchange = () => {
run_alg(elm.id);
};
}
const canvas = elm.getElementsByTagName("canvas")[0];
canvas.onclick = () => run_alg(elm.id);
}
window.onload = () =>
wasmStatus.then(() => {
if (window.isSlow == true) {
document.getElementById("warn").style.display = "flex";
setTimeout(() => {
for (const elm of alg_iter()) run_alg(elm.id);
}, 50);
} else {
for (const elm of alg_iter()) run_alg(elm.id);
}
});
</script>
</body>
</html>