-
Notifications
You must be signed in to change notification settings - Fork 1
/
surfaceAugmenterBeta.html
980 lines (790 loc) · 27.8 KB
/
surfaceAugmenterBeta.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
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
<html>
<head>
<title>SurfaceAugmenter</title>
<mta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="expires" content="0">
<meta name="author" content="wizards23+surfaces@gmail.com">
<meta name="description" content="SurfaceAugmenter allows you to get images of flat surfaces that will fit exactly onto the flat surface it was taken from.">
<meta name="keywords" content="javascript, canvas, perspective correction, metalab">
<script type="text/javascript">
/* <![CDATA[ */
(function() {
var s = document.createElement('script'), t = document.getElementsByTagName('script')[0];
s.type = 'text/javascript';
s.async = true;
s.src = 'http://api.flattr.com/js/0.6/load.js?mode=auto';
t.parentNode.insertBefore(s, t);
})();
/* ]]> */
</script>
<style> </style>
<script type="text/javascript" src="libs/NumJS/NumJS.js"></script>
<script type="text/javascript">NumJS.loader_html("libs/NumJS/")</script>
<script type="text/javascript" src="src/image.js"></script>
<script type="text/javascript" src="src/helpers.js"></script>
<script type="text/javascript" src="src/lines.js"></script>
<script type="text/javascript">
var img;
var imgWidth, imgHeight;
var fileName = "test2.jpg";
var imgScale = 1.0;
var points = [];
var pixels, bwPixels;
var maskInitBlack = 50;
var maskLine = 200;
var maskOutBlack = 100;
function setup()
{
changeMode('lab');
document.getElementById('imageFile').addEventListener('change', handleImageSelect, false);
document.getElementById('canvas').addEventListener('click', handleCanvasClick, false);
// enable dnd on canvas to load images
var holder = document.getElementById('canvas');
holder.ondragover = function () { this.className = 'hover'; return false; };
holder.ondragend = function () { this.className = ''; return false; };
holder.ondrop = function (e) {
this.className = ''; // what for?
e.preventDefault();
var file = e.dataTransfer.files[0];
myLoadHandler(file);
};
asyncLoadImageFromURL(fileName);
}
function myLoadHandler(file) {
fileName = file.name;
asyncLoadImageFromFile(file);
}
function handleImageSelect(evt) {
clearCaches();
var files = evt.target.files; // FileList object
var file = files[0];
myLoadHandler(file);
}
function clearCaches() {
pixels = null;
bwPixels = null;
}
function ensureCaches(cacheRest) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
if (!pixels) {
pixels = context.getImageData(0,0, canvas.width, canvas.height);
}
if (cacheRest && !bwPixels) {
bwPixels = RGBA2A(pixels, context);
}
}
function handleCanvasClick(evt) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
clearDump();
ensureCaches(false);
var coords = canvas.relMouseCoords(evt);
var x = coords.x/imgScale;
var y = coords.y/imgScale;
if (currentMode == "lab") {
x = Math.round(x);
y = Math.round(y);
handleLabClick(x, y, true);
return;
}
if (document.getElementById("radioSearchTypeManual").checked)
{
if (x >= 0 && x < imgWidth && y >= 0 && y < imgHeight)
points.push({x:x, y:y});
context.beginPath();
context.arc(x, y, 3, 0, Math.PI*2, true);
context.closePath();
context.fill();
if (points.length == 4)
{
doCorrection(pixels, points, context, null);
points = [];
}
}
else {
ensureCaches(true);
x = Math.round(x);
y = Math.round(y);
/*
var whiteSum = 255;
var whiteVarsum = 0;
var whiteN = 1;
var blackSum = 0;
var blackVarsum = 0;
var blackN = 1;
var br = 0.0;
fill4(x, y, function(x, y, state) {
if (x >= 0 && x < imgWidth && y >= 0 && y < imgHeight) {
var whiteAvg = whiteSum/whiteN;
var blackAvg = blackSum/blackN;
var whiteVar = whiteVarsum/whiteN;
var blackVar = blackVarsum/blackN;
var idx = x+imgWidth*y;
var idx4 = 4*idx;
var mask = maskPixels.data[idx];
if (mask == maskInitBlack || mask == maskLine || mask == maskOutBlack) {
return false;
}
var val = bwPixelsSobolev.data[idx];
br += 0.03;
//pixels.data[idx4] = br;
//pixels.data[idx4+1] = br-255;
//pixels.data[idx4+2] = br-512;
if (state == maskInitBlack)
{
if (val > (0.5*blackAvg+0.5*whiteAvg)) {
//blackSum += val; blackVarsum += Math.abs(val-blackAvg); blackN++;
whiteSum += val; whiteVarsum += Math.abs(val-whiteAvg); whiteN++;
maskPixels.data[idx] = maskLine;
return maskLine;
}
blackSum += val; blackVarsum += Math.abs(val-blackAvg); blackN++;
//whiteSum += val; whiteVarsum += Math.abs(val-whiteAvg); whiteN++;
maskPixels.data[idx] = maskInitBlack;
return maskInitBlack;
}
else if (state == maskLine)
{
if (val < (0.7*blackAvg+0.3*whiteAvg)) {
maskPixels.data[idx] = maskOutBlack;
return maskOutBlack;
}
//blackSum += val; blackVarsum += Math.abs(val-blackAvg); blackN++;
whiteSum += val; whiteVarsum += Math.abs(val-whiteAvg); whiteN++;
maskPixels.data[idx] = maskLine;
return maskLine;
}
}
return false;
}, maskInitBlack);
applyMaskAlphaOnPixels(bwPixelsSobolev, maskPixels, bwPixelsSobolevClipped)
*/
var res = handleLabClick(x, y);
maskPixels = res[0];
bwPixelsSobolevClipped = res[1];
var bestGroup = findLineCandidates(pixels, maskPixels, bwPixelsSobolevClipped);
for (var i = 0; i < bestGroup.length; i++) {
improveLine(bestGroup[i], bwPixelsSobolevClipped, 4, 0.05);
}
var pointsIn = [];
for (var i = 0; i < bestGroup.length; i++) {
pointsIn.push(intersectLines(bestGroup[i], bestGroup[(i+1)%bestGroup.length]));
}
doCorrection(pixels, pointsIn, context, bestGroup);
}
}
function doCorrection(pixels, pointsIn, context, bestGroup) {
if (bestGroup == null) {
var bestGroup = [];
for (var i = 0; i < pointsIn.length; i++) {
bestGroup.push({p1:pointsIn[i], p2:pointsIn[(i+1)%4]});
}
}
// draw outher lines in pixels
for (var i = 0; i < bestGroup.length; i++) {
drawLine2(bestGroup[i].p1, bestGroup[i].p2, pixels, 0, 255, 0);
}
drawLine2(pointsIn[0], pointsIn[2], pixels, 0, 0, 255);
drawLine2(pointsIn[1], pointsIn[3], pixels, 0, 0, 255);
var pixelsIn = pixels;
var minD = -1, minIdx = -1;
for (var i = 0; i < 4; i++) {
var d = distancePoints({x:0,y:0}, pointsIn[i]);
if (minIdx < 0 || d < minD) {
minD = d;
minIdx = i;
}
}
//minIdx;
var newPoints = [];
for (var i = 0; i < 4; i++) {
newPoints.push(pointsIn[(i+minIdx)%4]);
}
pointsIn = newPoints;
var dpi = getDpi();
var scale = dpi*0.0393700788; // 1mm = 0.0393700787 inch
var markerWidth = parseFloat(document.getElementById("markerWidth").value);
var markerHeight = parseFloat(document.getElementById("markerHeight").value);
var outW = markerWidth*scale, outH = markerHeight*scale; // size of my testpattern 170x250mm
var inputW = distancePoints(pointsIn[0], pointsIn[1]);
var inputH = distancePoints(pointsIn[1], pointsIn[2])
if ((inputW > inputH) != (outW > outH)) {
var t = outW;
outW = outH;
outH = t;
}
//var pointsOut = [{x:0, y:0}, {x:outW-1, y:0}, {x:outW-1, y:outH-1}, {x:0, y:outH-1}];
var pointsOut = [{x:0, y:0}, {x:outW, y:0}, {x:outW, y:outH}, {x:0, y:outH}];
var params = calcParameters(pointsIn, pointsOut);
//var params = calcParameters(pointsOut, pointsIn);
//var corners = [{x:0,y:0}, {x:pixelsIn.width-1, y:0}, {x:pixelsIn.width-1, y:pixelsIn.height-1}, {x:0, y:pixelsIn.height-1}];
var corners = [{x:0,y:0}, {x:pixelsIn.width, y:0}, {x:pixelsIn.width, y:pixelsIn.height}, {x:0, y:pixelsIn.height}];
var minX=0, minY=0, maxX=0, maxY=0;
for (var i = 0; i < 4; i++) {
var cornerT = fwdMapXY(params, corners[i]);
//var cornerT = invMapXY(params, corners[i]);
if (cornerT.x < minX || i==0) minX = cornerT.x;
if (cornerT.x > maxX || i==0) maxX = cornerT.x;
if (cornerT.y < minY || i==0) minY = cornerT.y;
if (cornerT.y > maxY || i==0) maxY = cornerT.y;
}
if (document.getElementById("optionOutputNearest").checked) {
var pixelsOut = context.createImageData(Math.ceil(maxX-minX), Math.ceil(maxY-minY));
var idxOut = 0;
for (var yOut = 0; yOut < pixelsOut.height; yOut++) {
for (var xOut = 0; xOut < pixelsOut.width; xOut++) {
//var pointIn = fwdMapXY(params, {x:xOut+minX, y:yOut+minY});
var pointIn = invMapXY(params, {x:xOut+minX, y:yOut+minY});
var xIn = Math.round(pointIn.x);
var yIn = Math.round(pointIn.y);
if (xIn >= 0 && yIn >= 0 && xIn < pixelsIn.width && yIn < pixelsIn.height) {
var idxIn = 4*(yIn*pixelsIn.width+xIn);
pixelsOut.data[idxOut] = pixelsIn.data[idxIn+0];
pixelsOut.data[idxOut+1] = pixelsIn.data[idxIn+1];
pixelsOut.data[idxOut+2] = pixelsIn.data[idxIn+2];
pixelsOut.data[idxOut+3] = pixelsIn.data[idxIn+3];
}
idxOut += 4;
}
}
dumpImageData(pixelsOut, "<br />Perspective corrected image (Filter: None, Nearest Neighbour)<br/>");
}
if (document.getElementById("optionOutputBilinear").checked) {
var pixelsOut = context.createImageData(Math.ceil(maxX-minX), Math.ceil(maxY-minY));
var idxOut = 0;
for (var yOut = 0; yOut < pixelsOut.height; yOut++) {
for (var xOut = 0; xOut < pixelsOut.width; xOut++) {
var pointIn = invMapXY(params, {x:xOut+minX, y:yOut+minY})
var xNextIdx, xNextScale, yNextIdx, yNextScale, xyNextIdx, xyNextScale;
var xIn = Math.round(pointIn.x);
var yIn = Math.round(pointIn.y);
var idxIn = 4*(yIn*pixelsIn.width+xIn);
if (xIn > pointIn.x) {
xNextIdx = idxIn-4;
xNextScale = xIn - pointIn.x;
}
else {
xNextIdx = idxIn+4;
xNextScale = pointIn.x - xIn;
}
if (yIn > pointIn.y) {
yNextIdx = idxIn-4*pixelsIn.width;
yNextScale = yIn - pointIn.y;
xyNextIdx = xNextIdx-4*pixelsIn.width;
}
else {
yNextIdx = idxIn+4*pixelsIn.width;
yNextScale = pointIn.y - yIn;
xyNextIdx = xNextIdx+4*pixelsIn.width;
}
if (xIn >= 0 && yIn >= 0 && xIn < pixelsIn.width && yIn < pixelsIn.height) {
var idxOut = 4*(yOut*pixelsOut.width+xOut);
for (var i = 0; i < 3; i++) {
pixelsOut.data[idxOut+i] = (1-yNextScale) * ((1 - xNextScale) * pixelsIn.data[idxIn+i] +
(xNextScale) * pixelsIn.data[xNextIdx+i]) +
(yNextScale) * ((1 - xNextScale) * pixelsIn.data[yNextIdx+i] +
(xNextScale) * pixelsIn.data[xyNextIdx+i]);
}
pixelsOut.data[idxOut+3] = 255;
}
idxOut += 4;
}
}
dumpImageData(pixelsOut, "<br />Perspective corrected image (Filter: Bilinear)<br/>");
}
if (document.getElementById("optionOutputImageMagick").checked)
{
var pointsText = "";
for (i = 0; i < pointsIn.length; i++)
{
p = pointsIn[i];
r = {x:pointsOut[i].x, y:pointsOut[i].y};
pointsText += "" + p.x + "," + p.y + "," + r.x + "," + r.y + " ";
}
var sizeText = (maxX-minX)+"x"+(maxY-minY);
sizeText = "2000x2000"
var cmdLine = "convert " + fileName + " -virtual-pixel transparent +distort Perspective \"" +
pointsText + "\" +repage " + fileName + "_corrected.jpg";
dumpHTML("<br/>ImageMagick Command Line: <div style='border-width:2px; border-style:solid; border-color:black; padding:10px;'>" + cmdLine + "</div>");
}
context.putImageData(pixels, 0,0);
if (document.getElementById("optionShowSobel").checked)
{
ensureCaches(true);
var grayPixels = A2RGBA(bwPixelsSobolev, context);
dumpImageData(grayPixels, "<br />magnitude of the sobel gradient<br/>");
}
if (document.getElementById("optionShowMask").checked)
{
ensureCaches(true);
var grayPixels = A2RGBA(maskPixels, context);
dumpImageData(grayPixels, "<br />the mask used for clipping the gradient for the line search<br/>");
}
if (document.getElementById("optionShowClippedSobel").checked)
{
ensureCaches(true);
var grayPixels = A2RGBA(bwPixelsSobolevClipped, context);
dumpImageData(grayPixels, "<br />clipped magnitude of the sobel gradient<br/>");
}
if (document.getElementById("optionShowClippedSobelLines").checked)
{
ensureCaches(true);
var grayPixels = A2RGBA(bwPixelsSobolevClipped, context);
if (bestGroup != null) {
for (var i = 0; i < bestGroup.length; i++) {
drawLine2(bestGroup[i].p1, bestGroup[i].p2, grayPixels, 255, 0, 0);
}
}
dumpImageData(grayPixels, "<br />clipped magnitude of the gradient overlayed with the vectorized lines (this is used for the search so the vectorized lines should be very well aligned with the pixels<br/>");
}
}
function drawImage(img) {
var canvas = document.getElementById('canvas');
canvas.width = img.width;
canvas.height = img.height;
imgWidth = img.width;
imgHeight = img.height;
scaleImage(1);
var context = canvas.getContext('2d');
context.drawImage(img, 0, 0, img.width, img.height);
}
function scaleImage(scale)
{
imgScale *= scale;
var canvas = document.getElementById('canvas');
canvas.style.width = "" + (imgWidth*imgScale) +"px";
canvas.style.height = "" + (imgHeight*imgScale) +"px";
//
//canvas.height = imgHeight;
}
var allowedModes = ['advanced', 'lab', 'about'];
var currentMode;
function changeMode(mode) {
for (var i = 0; i < allowedModes.length; i++) {
var m = allowedModes[i];
var id = m + "Controlls";
if (m == mode) {
//document.getElementById(id).style.visibility = "visible";
document.getElementById(id).style.display = "block";
}
else {
//document.getElementById(id).style.visibility = "hidden";
document.getElementById(id).style.display = "none";
}
}
currentMode = mode;
}
function clearDump() {
removeAllChildrenFromNode(document.getElementById("dump"));
}
function dumpHTML(text) {
var div = document.createElement('div');
div.innerHTML = text;
document.getElementById("dump").appendChild(div);
}
function dumpImageData(pixels, text) {
var c = document.createElement('canvas');
c.width = pixels.width;
c.height = pixels.height;
var div = document.createElement('div');
div.innerHTML = text;
div.appendChild(c);
document.getElementById("dump").appendChild(div);
var context = c.getContext('2d');
context.putImageData(pixels, 0,0);
}
function getDpi() {
return parseFloat(document.getElementById('outputDpiAdvanced').value);
return 5; // defualt value for other modes (something small is good)
}
function handleTestClick()
{
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var pixels = context.getImageData(0,0, canvas.width, canvas.height);
var bwPixels = RGBA2A(pixels, context);
var bwPixelsSobolev = RGBA2A(pixels, context);
applyKernelAlphaOnPixels(bwPixels, dxx, dyy, bwPixelsSobolev);
var grayPixels = A2RGBA(bwPixelsSobolev, context);
context.putImageData(grayPixels, 0,0);
}
/* alternating circle bad
var circle = {data:[
0,-1, 1,-1, 0,
1, 0, 0, 0, 1,
-1, 0, 0, 0,-1,
1, 0, 0, 0, 1,
0,-1, 1,-1, 0,
], width:5, height:5, scale:1};
var circleInv = {data:[
0,-1, 1,-1, 0,
1, 0, 0, 0, 1,
-1, 0, 0, 0,-1,
1, 0, 0, 0, 1,
0,-1, 1,-1, 0,
], width:5, height:5, scale:1/16};
//*/
/* big circle
var circle = {data:[
0, 0,-1,-1,-1, 0, 0,
0, 1, 0, 0, 0,-1, 0,
1, 0, 0, 0, 0, 0,-1,
1, 0, 0, 0, 0, 0,-1,
1, 0, 0, 0, 0, 0,-1,
0, 1, 0, 0, 0,-1, 0,
0, 0, 1, 1, 1, 0, 0,
], width:7, height:7, scale:1};
var circleInv = {data:[
0, 0,-1,-1,-1, 0, 0,
0, 1, 0, 0, 0,-1, 0,
1, 0, 0, 0, 0, 0,-1,
1, 0, 0, 0, 0, 0,-1,
1, 0, 0, 0, 0, 0,-1,
0, 1, 0, 0, 0,-1, 0,
0, 0, 1, 1, 1, 0, 0,
], width:7, height:7, scale:1/16};
//*/
/* big circle
var circle = {data:[
0, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 1, 1, 0, 0,
], width:7, height:7, scale:1/16};
var circleInv = {data:[
0, 0, 1, 1, 1, 0, 0,
0, 1, 0, 0, 0, 1, 0,
1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1,
1, 0, 0, 0, 0, 0, 1,
0, 1, 0, 0, 0, 1, 0,
0, 0, 1, 1, 1, 0, 0,
], width:7, height:7, scale:1/12};
//*/
/*
var circle = {data:[
0, 1, 1, 1, 0,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
0, 1, 1, 1, 0,
], width:5, height:5, scale:1/12};
var circleInv = {data:[
0, 1, 1, 1, 0,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
1, 0, 0, 0, 1,
0, 1, 1, 1, 0,
], width:5, height:5, scale:1/12};
/*
var circle = {data:[
0, 1, 0,
1, 0, 1,
0, 1, 0,
], width:3, height:3, scale:1/4};
var circleInv = {data:[
0, 1, 0,
1, 0, 1,
0, 1, 0,
], width:3, height:3, scale:1/4};
//*/
///*
var circle = {data:[
0, 1/4, 0,
1/4, 0, 1/4,
0, 1/4, 0,
], width:3, height:3, scale:1};
var circleInv = {data:[
0, 1/4, 0,
1/4, 0, 1/4,
0, 1/4, 0,
// 0, -1/4, 0,
// -1/4, -1, -1/4,
// 0, -1/4, 0,
], width:3, height:3, scale:1};
//*/
function handleTestClick2()
{
clearDump();
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var pixels = context.getImageData(0,0, canvas.width, canvas.height);
var bwPixels = RGBA2A(pixels, context);
var bwPixelsOut = RGBA2A(pixels, context);
var bwPixelsOut2 = RGBA2A(pixels, context);
APixelsFill(bwPixelsOut, 0);
applyBaseA(bwPixels, circle, bwPixelsOut);
applyKernelA(bwPixelsOut, circleInv, bwPixelsOut2);
var grayPixels = A2RGBA(bwPixelsOut, context);
dumpImageData(grayPixels, "pixels<br/>");
var grayPixels = A2RGBA(bwPixelsOut2, context);
dumpImageData(grayPixels, "pixels<br/>");
}
var STATE_INIT = 0;
var STATE_WHITE = 1;
function getA(pixels, x, y) {
var idx = y * pixels.width + x;
return pixels.data[idx];
}
function setA(pixels, x, y, val) {
var idx = y * pixels.width + x;
pixels.data[idx] = val;
}
function mindOutline(x, y, seekFn, testFn, outlineFn, fillFn)
{
while (!seekFn(x, y)) {
x++;
}
// assert(testFn(x,y)==false)
if (!testFn(x,y))
alert("I dont want to live like this! One part of you is saying true while the other is saying false!");
var posLeft = [], posRight = [];
var minY = y, maxY = y;
var dir = 2;
var startX = x, startY = y;
var done = false;
var dirCounter = 0;
while (!done) {
if (posLeft[y] == undefined || x < posLeft[y]) posLeft[y] = x;
if (posRight[y] == undefined || x > posRight[y]) posRight[y] = x;
if (y < minY) minY = y;
if (y > maxY) maxY = y;
if (dir < 0) dir = 3;
if (dir == 0) {
dx = 1, dy = 0;
}
else if (dir == 1) {
dx = 0, dy = 1;
}
else if (dir == 2) {
dx = -1, dy =0;
}
else {
dx = 0, dy = -1;
}
var newX = x + dx;
var newY = y + dy;
if (testFn(newX, newY)) {
x = newX;
y = newY;
outlineFn(x, y, dx, dy);
dirCounter=0;
dir--;
if (dir < 0) dir = 3;
if (x == startX && y == startY) done = true;
}
else {
dir = (dir + 1) % 4;
dirCounter++;
if (dirCounter == 4) done = true;
}
}
alert(minY + " " + maxY);
for (var fY = minY; fY <= maxY; fY++) {
//alert(minY + " " + maxY);
for (var fX = posLeft[fY]; fX <= posRight[fY]; fX++) {
fillFn(fX,fY);
}
}
}
function handleLabClick(x, y, show)
{
clearDump();
ensureCaches(true);
alert(x + " " + y);
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
var mask = createImageDataA(bwPixels.width, bwPixels.height); //RGBA2A(pixels, context);
var bwPixelsSobolev = createImageDataA(bwPixels.width, bwPixels.height);
var bwPixelsSobolevClipped = createImageDataA(bwPixels.width, bwPixels.height);
applyKernelAlphaOnPixels(bwPixels, dxx, dyy, bwPixelsSobolev);
alert("chaches done");
var seekState = STATE_INIT;
var blackFn = function(x, y) {
var val = getA(bwPixelsSobolev, x, y);
return val < 100;
}
var veryBlackFn = function(x, y) {
var val = getA(bwPixelsSobolev, x, y);
return val < 50;
}
var seekFn = function(x,y) {
if (seekState == STATE_INIT) {
if (!blackFn(x, y)) seekState = STATE_WHITE;
return false;
}
else if (seekState == STATE_WHITE) {
return veryBlackFn(x, y);
}
else {
alert("I dont want to live in inconsistent states!");
}
}
var fillFn = function(x, y) {
val = blackFn(x, y) ? maskInitBlack : maskLine;
setA(mask, x, y, val);
}
var outlineFn = function(x, y, dx, dy) {
var dumdumduboidoooooIdoNothing = 1;
}
mindOutline(x, y, seekFn, veryBlackFn, outlineFn, fillFn);
alert("outline done");
applyMaskAlphaOnPixels(bwPixelsSobolev, mask, bwPixelsSobolevClipped)
alert("masking done");
if (show) {
var grayPixels = A2RGBA(bwPixelsSobolevClipped, context);
dumpImageData(grayPixels, "clipped<br/>");
var grayPixels = A2RGBA(bwPixelsSobolev, context);
dumpImageData(grayPixels, "pixels<br/>");
}
alert("DOne");
//var grayPixels = A2RGBA(maskNice, context);
//dumpImageData(grayPixels, "pixels<br/>");
return [mask, bwPixelsSobolevClipped];
}
/*
function applyKernelAlphaOnPixel(pixels, kernel, x, y)
{
var xx, yy, kernelIndex, pixelIndex;
var sum = 0;
var startx = (kernel.width-1)/2;
var starty = (kernel.height-1)/2;
kernelIndex = 0;
for (yy = -starty; yy <= starty; yy++) {
for (xx = -startx; xx <= startx; xx++) {
var xxx = x + xx;
var yyy = y + yy;
if (xxx > 0 && yyy > 0 && xxx < pixels.width && yyy < pixels.height) {
pixelIndex = xxx + yyy*pixels.width;
sum += kernel.data[kernelIndex]*pixels.data[pixelIndex];
}
kernelIndex++;
}
}
return sum*kernel.scale;
}
*/
function applyKernelA(pixels, kernel, pixelsOut)
{
var outIndex = 0;
for (y = 0; y < pixels.height; y++) {
for (x = 0; x < pixels.width; x++) {
var val = applyKernelAlphaOnPixel(pixels, kernel, x, y);
pixelsOut.data[outIndex] = val;
outIndex++;
}
}
}
function applyBaseA(pixels, base, pixelsOut)
{
var startx = (base.width-1)/2;
var starty = (base.height-1)/2;
var outIndex = 0;
for (y = 0; y < pixels.height; y++) {
for (x = 0; x < pixels.width; x++) {
var s = pixels.data[outIndex];
var kernelIndex = 0;
for (yy = -starty; yy <= starty; yy++) {
for (xx = -startx; xx <= startx; xx++) {
var xxx = Math.round(x + xx);
var yyy = Math.round(y + yy);
if (xxx > 0 && yyy > 0 && xxx < pixels.width && yyy < pixels.height) {
pixelIndex = xxx + yyy*pixels.width;
pixelsOut.data[pixelIndex] += base.scale*base.data[kernelIndex]*s;
}
kernelIndex++;
}
}
outIndex++;
}
}
}
</script>
</head>
<body onload="setup()">
<span style="font-size:x-large;">
<a href="javascript:changeMode('advanced');">Perspective Correction</a> |
<a href="javascript:changeMode('lab');">Image Lab</a> |
<a href="javascript:changeMode('about');">About</a>
</span>
<span style="">
<a class="FlattrButton" style="display:none;" rev="flattr;button:compact;" href="http://www.wizards23.net/projects2011/surfaceaugmenter/surfaceAugmenterBeta.html"></a>
<noscript><a href="http://flattr.com/thing/394074/SurfaceAugmenter" target="_blank">
<img src="http://api.flattr.com/button/flattr-badge-large.png" alt="Flattr this" title="Flattr this" border="0" /></a></noscript>
</span>
<hr/>
<form name="controls" action="">
<div id="advancedControlls">
<fieldset>
<legend>How should the <a href="markers/250mmx170mm.svg">marker</a> be found?</legend>
<input id="radioSearchTypeManual" type="radio" name="searchType" value="manual">Manually click onto the 4 corners in clockwise order to start the perspective correction<br>
<input id="radioSearchTypeClickCenter" type="radio" name="searchType" value="click" checked="true">Click inside the inner white area of the marker to start the perspective correction<br>
<input id="radioSearchTypeAuto" type="radio" name="searchType" value="auto" disabled="true">not implemented yet: Automatic marker detection: Click anywhere in the image to start the perspective correction<br>
</fieldset>
<fieldset>
<legend>Dimensions</legend>
DPI of output image: <input type="text" id="outputDpiAdvanced" value="20" /><br/>
Inner Width of marker in mm: <input type="text" id="markerWidth" value="170" /><br/>
Inner Height of marker in mm: <input type="text" id="markerHeight" value="250" /><br/>
</fieldset>
<fieldset>
<legend>How should the perspective correction be done? (Multiple output paths can be selected for comparison)</legend>
<input id="optionOutputNearest" type="checkbox">Javascript/Canvas using No Filter/Nearest Neighbour (slow for large images)<br/>
<input id="optionOutputBilinear" type="checkbox" checked="true">Javascript/Canvas using Bilinear Interpolation (slow for large images)<br/>
<input id="optionOutputImageMagick" type="checkbox" checked="true"><a href="http://www.imagemagick.org/">ImageMagick</a> command line (recommended for larger DPI)<br>
</fieldset>
<fieldset>
<legend>How does it work? Show me the guts!</legend>
<input id="optionShowSobel" type="checkbox" checked="true"> Show Sobel Gradient Image<br/>
<input id="optionShowMask" type="checkbox" checked="true"> Show Mask Image used for clipping<br/>
<input id="optionShowClippedSobel" type="checkbox" checked="true"> Show Clipped Gradient Image<br/>
<input id="optionShowClippedSobelLines" type="checkbox" checked="true"> Show Clipped Sobel Gradient Image with vectorized lines<br/>
</fieldset>
</div>
<div id="labControlls" style="display:none;">
This is just a testing ground for some ideas I want to try out...<br/>
<input type="button" value="Apply Sobel Operator" onclick="handleTestClick();">
<input type="button" value="apply base" onclick="handleTestClick2();">
<input type="button" value="convolve" onclick="handleTestClick3();">
</div>
<div id="aboutControlls" style="display:none;">
The basic idea is this: You attach a printed marker (a black rectangle, I use <a href="markers/250mmx170mm.svg">this svg</a> which has an inner size of 170x250mm) next to the flat surface you are interested in (in this example a star wars poster that you want to augment somehow) and take a picture. Because we have the marker and we know its size in the real world we can automagically do a size and perspective correction of the flat surface. You can then modify the corrected image in gimp or photoshop and when you print it out it will fit right into the place where the image came from: the real world flat surface :)
The whole thing runs in javascript in the webbrowser thanks to html5 :)
<br/><br/>
The perspective and scaling is derived from the vectorized inner contour of the black frame of the marker.
The lines are vectorized with an antialiased search in the radon transformed gradient image (I always wanted to write this sentence:). Adjust the DPI of the output image to get more detail
but be aware that the resulting image might be huge.
<br/><br/>
Clifford's <a href="https://github.com/cliffordwolf/NumJS">NumJS</a> is used for solving the 8-dimensional linear equation that appears in the
perspective matrix calculations...and thanks to clifford also for writing
<a href="http://svn.clifford.at/handicraft/2011/cliprect/cliprect.cc">cliprect.cc</a>...my perspective correction code is just
a 1:1 translation of clifford's code to js (so this makes this webpage GPL I guess:). Thanks to bernhard for showing me the radon transform in the context of line vectorization :)
<br/><br/>
The latest version of the full source code can be found here:
<a href="https://bitbucket.org/wizard23/surfaceaugmenter">https://bitbucket.org/wizard23/surfaceaugmenter</a>
<br/><br/>
questions, ideas, comments to: <a href="mailto:wizards23+surfaces@gmail.com">wizards23+surfaces@gmail.com</a>
<br/><br/>
I collect pictures of applications of the SurfaceAugmenter in <a href="http://www.flickr.com/photos/wizard23/sets/72157627506093695/">this flickr set.</a>
<br/><br/>
(GPL) by <a href="http://www.wizards23.net">wizard23</a>
</div>
<hr/>
Change the Input Image: <input type="file" value="imageFile" id="imageFile" />
<input type="button" value="scale x 1/1.2" onclick="scaleImage(1/1.2);">
<input type="button" value="scale x 1.2" onclick="scaleImage(1.2);">
</form>
<canvas id="canvas"></canvas>
<hr/>
<div id="dump">
Result Images will be created here.
</div>
<hr/>
(GPL) by <a href="http://www.wizards23.net/">wizard23</a>
</body>
</html>