-
Notifications
You must be signed in to change notification settings - Fork 1
/
markerFinder.html
439 lines (347 loc) · 13.4 KB
/
markerFinder.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
<html>
<head>
<title>SurfaceAugmenter</title>
<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" src="libs/pixastic/pixastic.core.js"></script>
<script type="text/javascript" src="libs/pixastic/actions/histogram.js"></script>
<script type="text/javascript">
var img;
var imgWidth, imgHeight;
var imgScale = 1.0;
var points = [];
var pixels, bwPixels, maskPixels;
var maskInitBlack = 50;
var maskLine = 200;
var maskOutBlack = 100;
function setup()
{
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];
asyncLoadImageFromFile(file);
};
asyncLoadImageFromURL("test2.jpg");
}
function handleImageSelect(evt) {
pixels = undefined;
var files = evt.target.files; // FileList object
var file = files[0];
asyncLoadImageFromFile(file);
}
function handleCanvasClick(evt) {
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
if (!pixels) {
pixels = context.getImageData(0,0, canvas.width, canvas.height);
//pixels2 = context.getImageData(0,0, canvas.width, canvas.height);
bwPixels = RGBA2A(pixels, context);
bwPixelsSobolev = createImageDataA(canvas.width, canvas.height);
applyKernelAlphaOnPixels(bwPixels, dxx, dyy, bwPixelsSobolev);
maskPixels = createImageDataA(canvas.width, canvas.height);
APixelsFill(maskPixels, 0);
}
var coords = canvas.relMouseCoords(evt);
var x = coords.x/imgScale;
var y = coords.y/imgScale;
if (true || evt.ctrlKey || evt.button == 1)
{
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;
//pixels.data[idx4] = 255;
//pixels.data[idx4+1] = 0;
//pixels.data[idx4+2] = 0;
return maskLine;
}
blackSum += val; blackVarsum += Math.abs(val-blackAvg); blackN++;
//whiteSum += val; whiteVarsum += Math.abs(val-whiteAvg); whiteN++;
maskPixels.data[idx] = maskInitBlack;
//pixels.data[idx4] = 0;
//pixels.data[idx4+1] = 255;
//pixels.data[idx4+2] = 0;
return maskInitBlack;
}
else if (state == maskLine)
{
if (val < (0.7*blackAvg+0.3*whiteAvg)) {
//pixels.data[idx4] = 255;
//pixels.data[idx4+1] = 255;
//pixels.data[idx4+2] = 0;
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;
//pixels.data[idx4] = 0;
//pixels.data[idx4+1] = 0;
//pixels.data[idx4+2] = 255;
return maskLine;
}
}
return false;
}, maskInitBlack);
applyMaskAlphaOnPixels(bwPixelsSobolev, maskPixels, bwPixelsSobolev)
var bestGroup = findLineCandidates(pixels, maskPixels, bwPixelsSobolev);
for (var i = 0; i < bestGroup.length; i++) {
improveLine(bestGroup[i], bwPixelsSobolev, 4, 0.05);
}
for (var i = 0; i < bestGroup.length-2; i++) {
var x1 = intersectLines(bestGroup[i], bestGroup[i+1]);
var x2 = intersectLines(bestGroup[i+2], bestGroup[(i+3)%bestGroup.length]);
drawLine2(x1, x2, pixels, 0, 0, 255);
}
var pixelsIn = pixels;
var pointsIn = [];
for (var i = 0; i < bestGroup.length; i++) {
pointsIn.push(intersectLines(bestGroup[i], bestGroup[(i+1)%bestGroup.length]));
}
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 = parseFloat(document.getElementById('outputDpi').value);
var scale = dpi*0.0393700788; // 1mm = 0.0393700787 inch
var outW = 170*scale, outH = 250*scale; // size of my testpattern 170x250mm
if (distancePoints(pointsIn[0], pointsIn[1]) > distancePoints(pointsIn[1], pointsIn[2])) {
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 = 100000, minY = 100000, maxX =-100000, maxY = -100000;
for (var i = 0; i < 4; i++) {
var cornerT = fwdMapXY(params, corners[i]);
//var cornerT = invMapXY(params, corners[i]);
//alert(cornerT.x + " " + cornerT.y);
if (cornerT.x < minX) minX = cornerT.x;
if (cornerT.x > maxX) maxX = cornerT.x;
if (cornerT.y < minY) minY = cornerT.y;
if (cornerT.y > maxY) maxY = cornerT.y;
}
var pixelsOut = context.createImageData(Math.ceil(maxX-minX), Math.ceil(maxY-minY));
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})
//pointIn.x -= minX;
//pointIn.y -= 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);
var idxOut = 4*(yOut*pixelsOut.width+xOut);
pixelsOut.data[idxOut+0] = 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];
}
//else alert(xIn + " " + xOut);
}
}
for (var i = 0; i < bestGroup.length; i++) {
drawLine2(bestGroup[i].p1, bestGroup[i].p2, pixels, 0, 255, 0);
drawLine2A(bestGroup[i].p1, bestGroup[i].p2, bwPixelsSobolev, 0);
}
context.putImageData(pixels, 0,0);
var canvasResult = document.getElementById('canvasResult');
canvasResult.width = pixelsOut.width;
canvasResult.height = pixelsOut.height;
var contextResult = canvasResult.getContext('2d');
contextResult.putImageData(pixelsOut, 0,0);
var canvas2 = document.getElementById('canvas2');
canvas2.width = maskPixels.width;
canvas2.height = maskPixels.height;
var context2 = canvas2.getContext('2d');
var grayPixels = A2RGBA(maskPixels, context2);
context2.putImageData(grayPixels, 0,0);
var canvas3 = document.getElementById('canvas3');
canvas3.width = maskPixels.width;
canvas3.height = maskPixels.height;
var context3 = canvas3.getContext('2d');
var grayPixels = A2RGBA(bwPixelsSobolev, context3);
for (var i = 0; i < bestGroup.length; i++) {
drawLine2(bestGroup[i].p1, bestGroup[i].p2, grayPixels, 255, 0, 0);
}
context3.putImageData(grayPixels, 0,0);
}
else {
if (x >= 0 && x < imgWidth && y >= 0 && y < imgHeight)
points.push([x, y]);
context.beginPath();
context.arc(x, y, 3, 0, Math.PI*2, true);
context.closePath();
context.fill();
if (points.length == 2)
{
var sum = 0;
var n = 0;
var w = bwPixels.width;
xiaolinWuLineIterator(points[0][0],points[0][1], points[1][0], points[1][1], function(x, y, c) {
n += c;
sum += c* bwPixels.data[x+w*y];
pixels.data[4*(x+w*y)] = (1-c) * pixels.data[4*(x+w*y)] + c * 0;
pixels.data[4*(x+w*y)+1] = (1-c) * pixels.data[4*(x+w*y)+1] + c * 0;
pixels.data[4*(x+w*y)+2] = (1-c) * pixels.data[4*(x+w*y)+2] + c * 0;
//pixels.data[4*(x+w*y)+3] = 255;
});
alert("s: " + sum + " n: " + n + " avg: " + (sum/n));
/*
lineIterator(points[0][0],points[0][1], points[1][0], points[1][1], function(x, y) {
n++;
sum += bwPixels[x+w*y];
pixels.data[4*(x+w*y)] = 255 - pixels.data[4*(x+w*y)];
pixels.data[4*(x+w*y)+1] = 255 - pixels.data[4*(x+w*y)+1];canvas.width = img.width;
canvas.height = img.height;
pixels.data[4*(x+w*y)+2] = 255 - pixels.data[4*(x+w*y)+2];
});
//*/
//alert("s: " + sum + " n: " + n + " " + (sum/n));
context.putImageData(pixels, 0, 0);
//document.getElementById('outputText').value = "convert ..\\..\\web\\" + fileName + " -virtual-pixel transparent -distort Perspective \"" + s + "\" " + fileName + "out.png";
//handleLoad(img);
points = [];
}
}
}
function drawImage(img) {
var canvas = document.getElementById('canvas');
canvas.width = img.width;
canvas.height = img.height;
imgWidth = img.width;
imgHeight = img.height;
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 hist = {};
function handleTestClick()
{
var canvas = document.getElementById('canvas');
var context = canvas.getContext('2d');
//var pixels = context.getImageData(110,110,120,120);
var pixels = context.getImageData(0,0, canvas.width, canvas.height);
var bwPixels = RGBA2A(pixels, context);
var bwPixelsSobolev = RGBA2A(pixels, context);
var x, y;
applyKernelAlphaOnPixels(bwPixels, dxx, dyy, bwPixelsSobolev)
var grayPixels = A2RGBA(bwPixelsSobolev, context);
context.putImageData(grayPixels, 0,0);
return;
Pixastic.process(document.getElementById("canvas"), "histogram", {
average : false, paint:true,color:"rgba(255,255,255,0.5)",returnValue:hist
});
document.getElementById('debugText').value = hist.values;
}
function xffdlj() {
//pixels = context.getImageData(0,0, canvas.width, canvas.height);
//bwPixels = Filters.RGBA2A(pixels, context);
// applyKernelAlpha(pixels, kernel, x, y)
}
</script>
</head>
<body onload="setup()">
<b>Click inside the white paper with the black frame to do a perspective and size correction of the image!</b>
<br/>
<br/>
The perspective and scaling is derived from the vectorized inner contour of the black frame. 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 NumJS 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 :)
<form name="controls" action="">
<input type="file" value="imageFile" id="imageFile" />
<input type="button" value="scale/1.2" onclick="scaleImage(1/1.2);"><input type="button" value="scale*1.2" onclick="scaleImage(1.2);">
<input type="button" value="Apply Sobel Operator" onclick="handleTestClick();">
DPI: <input type="text" id="outputDpi" value="25" />
</form>
<canvas id="canvas"></canvas>
<br>
<br>
the perspective and size corrected image
<br>
<canvas id="canvasResult"></canvas>
<br>
<br>
the mask used for clipping the gradient for the line search
<br>
<canvas id="canvas2"></canvas>
<br>
<br>
the 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>
<canvas id="canvas3"></canvas>
<br/><br/>
Next steps: Antialiased corrected image and saving the output as png or jpg, whitebalance, handling of raw files (mad laughter!!!11elf)...js is so cool :)
<br/><br/>
thanks for scrolling down this far!
</body>
</html>