-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathepicycles.html
464 lines (396 loc) · 13.7 KB
/
epicycles.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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>Epicycles</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.8.0/addons/p5.dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/mathjs/6.0.2/math.min.js"></script>
<script src="https://docs.opencv.org/master/opencv.js" type="text/javascript"></script>
<script src="https://docs.opencv.org/master/utils.js" type="text/javascript"></script>
<p>YOU CAN DO WHAT EVERY YOU WANT WITH IT!</p>
<script>
function initializeMap() {
let utils = new Utils('errorMessage');
utils.addFileInputHandler('imageInput', 'canvas1');
}
function waitAndStart()
{
document.getElementById("epicycles").hidden = true;
document.getElementById("imageProccessing").hidden = true;
var timer = setInterval(function() {
clearInterval(timer);
clahe();
document.getElementById("epicycles").hidden = false;
document.getElementById("imageProccessing").hidden = false;
}, 1000);
}
function dft(samples) {
const N = samples.length;
const arr = Array(N);
for (let i = 0; i < N; i++) {
arr[i] = math.complex(0, 0);
for (let n = 0; n < N; n++) {
const theta = (TWO_PI * i * n) / N;
const c = math.complex(cos(theta), -sin(theta));
const m = math.multiply(samples[n], c);
arr[i] = math.add(arr[i], m);
}
arr[i].re /= N;
arr[i].im /= N;
let amp = sqrt(arr[i].re * arr[i].re + arr[i].im * arr[i].im);
let phase = atan2(arr[i].im, arr[i].re);
arr[i] = {
re: arr[i].re,
im: arr[i].im,
freq: i,
amp: amp,
phase: phase
};
}
return arr;
}
function clahe() {
let clahe1 = parseInt(document.getElementById("clahe1").value);
let clahe2 = parseInt(document.getElementById("clahe2").value);
let src = cv.imread('canvas1');
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0);
if (clahe1 != 0 && clahe2 != 0) { //cv.equalizeHist(src, src);
let tileGridSize = new cv.Size(clahe1, clahe1);
let clahe = new cv.CLAHE(clahe2, tileGridSize);
clahe.apply(src, src);
}
cv.imshow('canvas2', src);
src.delete();
medorgaus();
}
function medorgaus() {
let src = cv.imread('canvas2');
let kernelSize = parseInt(document.getElementById("kernelSize").value) * 2 - 1;
if (kernelSize != -1) {
if (document.getElementById("median").checked == true) {
cv.medianBlur(src, src, kernelSize);
} else {
let ksize = new cv.Size(kernelSize, kernelSize);
cv.GaussianBlur(src, src, ksize, 0, 0, cv.BORDER_DEFAULT);
}
}
cv.imshow('canvas3', src);
src.delete();
threshold();
}
function threshold() {
let thr1 = parseInt(document.getElementById("thr1").value);
let thr2 = parseInt(document.getElementById("thr2").value);
if (thr1 > thr2) {
thr2 = thr1;
document.getElementById("thr2").value = thr2.toString();
} else {
let src = cv.imread('canvas3');
if (thr1 != 0) {
cv.threshold(src, src, thr1, thr2, cv.THRESH_BINARY);
}
cv.imshow('canvas4', src);
src.delete();
laplacian();
}
}
function laplacian() {
let src = cv.imread('canvas4');
let kernelSize = parseInt(document.getElementById("laplacian").value) * 2 - 1;
if (kernelSize != -1) {
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0);
cv.Laplacian(src, src, cv.CV_8U, kernelSize, 1, 0, cv.BORDER_DEFAULT); //cv.Canny(src, src, 100, 255, 3, false);
}
cv.imshow('canvas5', src);
src.delete();
erodedilate();
}
function erodedilate()
{
let src = cv.imread('canvas5');
let anchor = new cv.Point(-1, -1);
let mker = cv.Mat.ones(3, 3, cv.CV_8U);
let erode = parseInt(document.getElementById("erode").value);
let dilate = parseInt(document.getElementById("dilate").value);
if (erode != 0) {
cv.erode(src, src, mker, anchor, erode, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
}
if (dilate != 0) {
cv.dilate(src, src, mker, anchor, dilate, cv.BORDER_CONSTANT, cv.morphologyDefaultBorderValue());
}
cv.imshow('canvas6', src);
src.delete();
}
let time = 0;
let path = [];
let sizeCanvas;
let complexFourier;
let complex_points;
let sortContoursArray;
let infoContoursRelations;
function calcComplexFourierWithPointSkip()
{
complex_points = [];
document.getElementById("DivPointSkip").hidden = false;
for (let i = 0; i < sortContoursArray.length; i++) {
/*
* Küçük değerli contourları saymaya biliriz.
*/
/*if (contoursArray[i].length < 100)
continue;*/
/*
* sortContoursArray infoContoursRelations kullanılarak efektif geçişler yapılır.
*/
for (let j = 0; j < sortContoursArray[i].contour.length; j+=parseInt(document.getElementById("pointSkip").value)) {
complex_points.push(math.complex(sortContoursArray[i].contour[j][0] - sizeCanvas[0] / 2, sortContoursArray[i].contour[j][1] - sizeCanvas[1] / 2));
}
}
time = 0;
path = [];
complexFourier = undefined;
complexFourier = dft(complex_points);
complexFourier.sort((a, b) => b.amp - a.amp);
}
function createPathAndDrawEpicycles() {
let stack = [];
let contoursArray = [];
let hierarchy = new cv.Mat();
let src = cv.imread('canvas6'); //let dst = cv.Mat.zeros(src.rows, src.cols, cv.CV_8UC3);
let contours = new cv.MatVector();
sortContoursArray = [];
infoContoursRelations = [];
sizeCanvas = [src.cols, src.rows];
cv.cvtColor(src, src, cv.COLOR_RGB2GRAY, 0);
cv.findContours(src, contours, hierarchy, cv.RETR_LIST, cv.CHAIN_APPROX_SIMPLE); // RETR_CCOMP RETR_EXTERNAL RETR_TREE
/*
* Resimde bulunan contourların x-y değerleri diziye aktarılıyor.
* İlk başta contours.get(i) kullanarak gerekli yerlere erişiyordum.
* Ama bunun performans kaybı olduğunu gördüm. Ek olarak size büyük olduğunda
* algoritmanın ortasında erişim problemi yaşanıyordu. Hataya düşüyordu.
*/
for (let i = 0; i < contours.size(); i++) {
let pArr = [];
/*
* Laplacian uygulanmadığı zamanda bazen karşıma çıkıyor. contour noktalarından başlangıcı 0-0 oluyor.
* Başlangıç piksel x-y si 0-0 olan contouru es geçiyorum. Genelde sonuncu contour oluyor. Eğer arada olursa bir ara
* contoursArray[i] = pArr; satırında i değeri kullanılmamalı.
*/
if (contours.get(i).data32S[0] == 0 && contours.get(i).data32S[1] == 0)
continue;
for (let j = 0; j < contours.get(i).rows; j++) {
pArr[j] = [contours.get(i).data32S[j * 2], contours.get(i).data32S[j * 2 + 1]];
}
contoursArray[i] = pArr;
}
src.delete(); hierarchy.delete(); contours.delete();
/*
* Her contour'a en yakın contour'u buluyor ve diziye ekliyor.
* Bulurken hangi noktaların yakın olduğunu da diziye ekliyor.
*/
for (let i = 0; i < contoursArray.length; i++) {
let contourSelect;
let absVal = Infinity;
let p1, p2;
let j = 0;
for (let pi = 0; pi < contoursArray[i].length && absVal != 0; pi++) {
let pix_xi = contoursArray[i][pi][0];
let pix_yi = contoursArray[i][pi][1];
for (j = 0; j < contoursArray.length && absVal != 0; j++) {
/*
* i. contour'a en yakın olanı bulmak için kendisine bakmamalı.
*/
if (i == j) {
continue;
}
/*
* j. contour eğer i. contour tarafından en yakın seçilmişse, kendisi aynısını yakın seçmesin
* Seçmesi veya seçmemesinin performansa etkisi bakılmalı.
*/
if (j < i) {
if (infoContoursRelations[j].contourSelect == i) {
//continue;
}
}
for (let pj = 0; pj < contoursArray[j].length; pj++) {
let pix_xj = contoursArray[j][pj][0];
let pix_yj = contoursArray[j][pj][1];
let len = (pix_xi - pix_xj) ** 2 + (pix_yi - pix_yj) ** 2;
if (len < absVal) {
p1 = pi;
p2 = pj;
absVal = len;
contourSelect = j;
}
if (absVal < 3) {
len = 0;
absVal = 0;
break;
}
}
}
}
//console.log(i, contourSelect, contoursArray[i].length);
infoContoursRelations.push({p1:p1, p2:p2, contourSelect:contourSelect, inSort:0});
}
//console.log(infoContoursRelations);
/*
* Contourları yukarıda bulduğu ilişkiye göre sıralıyor.
*/
for(let i = 0; i < infoContoursRelations.length; i++) {
let curr = i;
while (infoContoursRelations[curr].inSort == 0) {
stack.push(curr);
while (stack.length > 0) {
let j = stack.pop();
if (infoContoursRelations[j].inSort == 0) {
for (let k = 0; k < infoContoursRelations.length; k++) {
if (infoContoursRelations[k].contourSelect == j && infoContoursRelations[k].inSort == 0) {
stack.push(j); // değişiklik
stack.push(k);
}
}
}
curr = j;
infoContoursRelations[j].inSort = 1;
// hangi eleman olduğu bilgisi atılacak.
sortContoursArray.push({j:j, contour:contoursArray[j]});
}
curr = infoContoursRelations[curr].contourSelect;
}
}
//console.log(sortContoursArray);
/*
* Kullanmayı düşündüm ama kullanmayacağım. Yorum olarak kalsın :D
* arr1.push.apply(arr1, arr1.splice(0,2));
*/
createCanvas(sizeCanvas[0], sizeCanvas[1]);
calcComplexFourierWithPointSkip();
}
function setup() {
//frameRate(30);
}
function epicycles(x, y, rotation, fourier) {
for (let i = 0; i < fourier.length; i++) {
let prevx = x;
let prevy = y;
let freq = fourier[i].freq;
let radius = fourier[i].amp;
let phase = fourier[i].phase;
x += radius * cos(freq * time + phase + rotation);
y += radius * sin(freq * time + phase + rotation);
stroke(0, 255);
noFill();
ellipse(prevx, prevy, radius * 2);
line(prevx, prevy, x, y);
stroke(255, 0, 0);
}
return createVector(x, y);
}
function draw() {
if (complexFourier != undefined) {
background(255);
let v = epicycles(width / 2, height / 2, 0, complexFourier);
path.unshift(v);
beginShape();
noFill();
for (let i = 0; i < path.length; i++) {
vertex(path[i].x, path[i].y);
}
endShape();
time += TWO_PI / complexFourier.length;
if (time > TWO_PI) {
time = 0;
}
}
}
</script>
</head>
<body onload="initializeMap()">
<div>
<div>
<p>
"Gözat" butonundan resim yükleyin. Basit görüntü düzeltme araçlarını kullanın. "epicycles" için en son gürültüsü az, çizgili resim oluşturmaya çalışın. <br />
"Epicycles Oluştur" butonuna tıklayarak epicycles oluşturabilirsiniz.<br />
</p>
</div>
<div>
<input type="file" id="imageInput" accept="image/*" oninput="waitAndStart()" />
<input type="button" id="epicycles" hidden="true" onClick="createPathAndDrawEpicycles()" value="Epicycles Oluştur" />
</div>
</div>
<br />
<div>
<table id="imageProccessing" hidden="true">
<tr>
<th>
<p>Girdi Resmi</p>
</th>
<th>
<p>Adaptif Hist. Eşitleme (clahe)</p>
<p>Threshold for contrast limit</p>
<input type="range" min="0" max="20" value="0" id="clahe1" onchange="clahe()" />
<p>Size of grid for histogram equalization</p>
<input type="range" min="0" max="40" value="0" id="clahe2" onchange="clahe()" />
</th>
<th>
<p>Gürültü Giderme - Sadece 1'i işlenir</p>
<input type="radio" id="median" name="kernel" checked />
<label for="median">Median Kernel Size</label> <br /> <br />
<input type="radio" id="gaussian" name="kernel" />
<label for="gaussian">Gaussian Kernel Size</label>
<br /> <br />
<input type="range" min="0" max="7" value="7" id="kernelSize" onchange="medorgaus()" />
</th>
</tr>
<tr>
<td>
<canvas id="canvas1" style='width:300px;height:300px'></canvas>
</td>
<td>
<canvas id="canvas2" style='width:300px;height:300px'></canvas>
</td>
<td>
<canvas id="canvas3" style='width:300px;height:300px'></canvas>
</td>
</tr>
<tr>
<th>
<p>Eşik değer</p>
<p>Threshold Min</p>
<input type="range" min="0" max="255" value="238" id="thr1" onchange="threshold()" />
<p>Threshold Max</p>
<input type="range" min="0" max="255" value="255" id="thr2" onchange="threshold()" />
</th>
<th>
<p>Kenar Bulma</p>
<p>Laplacian</p>
<input type="range" min="0" max="7" value="1" id="laplacian" onchange="laplacian()" />
</th>
<th>
<p>Yayma ve Genişletme</p>
<p>Erode</p>
<input type="range" min="0" max="10" value="0" id="erode" onchange="erodedilate()" />
<p>Dilate</p>
<input type="range" min="0" max="10" value="1" id="dilate" onchange="erodedilate()" />
</th>
</tr>
<tr>
<td>
<canvas id="canvas4" style='width:300px;height:300px'></canvas>
</td>
<td>
<canvas id="canvas5" style='width:300px;height:300px'></canvas>
</td>
<td>
<canvas id="canvas6" style='width:300px;height:300px'></canvas>
</td>
</tr>
</table>
</div>
<div id="DivPointSkip" hidden="true">
<p>Nokta Atlama</p>
<input type="range" min="1" max="20" value="5" id="pointSkip" onchange="calcComplexFourierWithPointSkip()"/>
</div>
</body>
</html>