-
Notifications
You must be signed in to change notification settings - Fork 12
/
dirty.html
337 lines (281 loc) · 10.2 KB
/
dirty.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
<canvas id="canvas"></canvas>
<script src="lib/jsfeat-custom.js"></script>
<script src="lib/lodash.js"></script>
<!-- <script src="calipers2.js"></script> -->
<script src="src/helpers.js"></script>
<script src="src/lstm.js"></script>
<script src="models/zeus.min.js"></script>
<script src="experiments/test.js"></script>
<script src="src/swtcore.js"></script>
<script src="src/chull.js"></script>
<script>
onload = function(){}
var img = new Image();
img.src = 'images/rotation.png';
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d')
img.onload = function(){
canvas.width = img.naturalWidth;
canvas.height = img.naturalHeight;
ctx.drawImage(img, 0, 0);
var data = ctx.getImageData(0, 0, canvas.width, canvas.height)
textdetect(data)
}
var params = {
// the kernel size for the gaussian blur before canny
kernel_size: 3,
// low and high thresh are parameters for the canny edge detector
low_thresh: 124,
high_thresh: 204,
// maximum stroke width, this is the number of iterations
// the core stroke width transform loop will go through
// before giving up and saying that there is no stroke here
max_stroke: 35,
// the maximum ratio between adjacent strokes for the
// connected components algorithm to consider part of the
// same actual letter
stroke_ratio: 2,
// this is the pixel connectivity required for stuff to happen
min_connectivity: 4,
// the minimum number of pixels in a connected component to
// be considered a candidate for an actual letter
min_area: 30, //default: 38
// maximum stroke width variation allowed within a letter
std_ratio: 0.83,
// maximum aspect ratio to still be considered a letter
// for instance, a really long line wouldn't be considered
// a letter (obviously if this number is too low, it'll start
// excluding l's 1's and i's which would be bad)
aspect_ratio: 10, // default: 8
// maximum ratio between the median thicknesses of adjacent
// letters to be considered part of the same line
thickness_ratio: 3,
// maximum ratio between adjacent letter heights to be considered
// part of the same line
height_ratio: 2.5, // original: 1.7
}
function Dot(a, b){ return a[0] * b[0] + a[1] * b[1] }
function Mag(a){ return Math.sqrt(a[0] * a[0] + a[1] * a[1]) }
function Mul(a, n){ return [a[0] * n, a[1] * n] }
function Div(a, n){ return Mul(a, 1 / n)}
function Unit(a) { return Div(a, Mag(a)) }
function Sub(a, b){ return [a[0] - b[0], a[1] - b[1]]}
function Add(a, b){ return [a[0] + b[0], a[1] + b[1]]}
function Rot90(a) { return [-a[1], a[0]] }
function Sum(x) { for(var i = 0, s = 0; i < x.length; i++) s += x[i]; return s }
function Mean(x) { return Sum(x) / x.length }
function Diff(x) { for(var i = 1, k = []; i < x.length; i++) k[i - 1] = x[i] - x[i - 1]; return k}
function Mod(x, n) { return x - Math.round(x / n) * n }
function moveTo(c, a) { c.moveTo(a[0], a[1]) }
function lineTo(c, a) { c.lineTo(a[0], a[1]) }
function adiff(a, b){
var d = Math.abs(a - b) % (Math.PI * 2);
var r = d > Math.PI ? (Math.PI * 2) - d : d;
return Math.abs(r)
}
function textdetect(src){
width = src.width
height = src.height;
var img_u8 = new jsfeat.matrix_t(width, height, jsfeat.U8C1_t)
var img_dxdy = new jsfeat.matrix_t(width, height, jsfeat.S32C2_t);
console.time("image processing")
jsfeat.imgproc.grayscale(src.data, img_u8.data)
// visualize_matrix(img_u8)
jsfeat.imgproc.sobel_derivatives(img_u8, img_dxdy)
jsfeat.imgproc.gaussian_blur(img_u8, img_u8, params.kernel_size, 0)
jsfeat.imgproc.canny(img_u8, img_u8, params.low_thresh, params.high_thresh)
console.timeEnd("image processing")
params.direction = -1
swt = raw_swt(img_u8, img_dxdy, params).swt;
console.time('connected components')
contours = connected_swt(swt, params).map(wrap_contours);
console.timeEnd('connected components')
c = visualize_matrix(swt, contours)
console.time('morphological dilation')
var dilation = document.createElement('canvas'),
dtx = dilation.getContext('2d');
dilation.width = width;
dilation.height = height;
// document.body.appendChild(dilation)
dtx.fillStyle = 'black'
dtx.fillRect(0, 0, width, height)
dtx.fillStyle = 'white'
contours.forEach(function(s){
dtx.fillRect(s.x0 - s.width, s.y0 - s.height, s.width * 3, s.height * 3)
})
// we can short-circuit all of this by sticking points into the convex hull list
// wait no we need connected components
console.timeEnd('morphological dilation')
console.time('region finding')
var dilmat = new jsfeat.matrix_t(width, height, jsfeat.U8C1_t)
jsfeat.imgproc.grayscale(dtx.getImageData(0, 0, width, height).data, dilmat.data)
regions = connected_swt(dilmat, params).map(wrap_contours);
console.timeEnd('region finding')
dd = visualize_matrix(dilmat, regions)
console.time('rotating calipers')
var lines = regions.map(function(region){
var points = region.contours;
// caliper = new RotatingCalipers(points.map(function(p){
// var x = p % width, y = Math.floor(p / width);
// return [x, y]
// }))
// var hull = caliper.convexHull();
var nicelyFormatedPoints = points.map(function(p){
return {x:p % width, y:Math.floor(p / width)}
})
var hull = convexHull(nicelyFormatedPoints)
dd.beginPath()
dd.moveTo(hull[0].x, hull[0].y)
for(var i = 1; i < hull.length; i++){
dd.lineTo(hull[i].x, hull[i].y)
}
dd.closePath()
dd.strokeStyle = 'yellow'
dd.lineWidth = 4
dd.stroke()
// var rect = caliper.minAreaEnclosingRectangle();
// console.log(rect.angle)
// maer = rect.vertices;
var maer = min_enclosing_rect(hull)
dd.beginPath()
dd.moveTo(maer[0].x, maer[0].y)
for(var i = 1; i < maer.length; i++){
dd.lineTo(maer[i].x, maer[i].y)
}
dd.closePath()
dd.strokeStyle = 'green'
dd.lineWidth = 4
dd.stroke()
var kevinMaer = maer.map(function(p){
return [p.x,p.y]
})
var roof = _.sortBy(_.sortBy(kevinMaer, 1).slice(0, 2), 0);
var dx = roof[1][0] - roof[0][0],
dy = roof[1][1] - roof[0][1];
angle = Math.atan2(dy, dx)
console.log('angle', angle)
var cx = Mean(_.pluck(kevinMaer, 0)), cy = Mean(_.pluck(kevinMaer, 1));
// dd.beginPath()
// dd.moveTo(cx - 5, cy - 5)
// dd.lineTo(cx + 5, cy + 5)
// dd.moveTo(cx + 5, cy - 5)
// dd.lineTo(cx - 5, cy + 5)
// dd.stroke()
var dir = Rot90(Unit([dx, dy]))
dd.fillStyle = 'rgba(0, 144, 0, 0.1)'
var dim = Math.ceil(Math.max(region.width, region.height) / 2)
var hist = new Uint32Array(dim * 2);
var range_min = Infinity, range_max = 0;
var in_region = contours.filter(function(s){
return s.cx > region.x0 && s.cx < region.x1 && s.cy > region.y0 && s.cy < region.y1
})
in_region.forEach(function(s){
var proj = Dot(Sub([s.cx, s.cy], [cx, cy]), dir);
dd.fillRect(cx, cy + proj, 5, 5)
// var r = Math.max(s.width, s.height) / 2;
var r = Math.sqrt(s.width * s.height) / 2;
for(var i = Math.floor(proj - r); i < proj + r; i++){
hist[i + dim]++;
range_min = Math.min(range_min, i + dim);
range_max = Math.max(range_max, i + dim);
}
})
dd.fillStyle = '#007fff'
// var metahist = new Uint32Array(range_max - range_min + 1)
for(var i = range_min; i < range_max; i++){
// metahist[hist[i]]++;
dd.fillRect(cx, cy + i - dim, hist[i], 1)
}
// wumbo = _.toArray(hist.subarray(range_min, range_max))
wumbo = _.toArray(hist)
function t(x){return x >= 3}; // minimum number of letters on a line
var peaks = array_split(_.range(wumbo.length), function(x,y){return t(wumbo[x]) == t(wumbo[y])})
.filter(function(x){return t(wumbo[x[0]])})
.map(function(x){return _.max(x, function(k){return wumbo[k]})});
peaks.forEach(function(p){
var inliers = [];
in_region.forEach(function(s){
var proj = Dot(Sub([s.cx, s.cy], [cx, cy]), dir);
if(Math.abs((p - dim) - proj) < 5){
inliers.push(s)
}
});
inliers.forEach(function(s){
c.fillStyle = 'green'
c.fillRect(s.cx, s.cy, 5, 5)
})
})
peaks.forEach(function(p){
c.beginPath()
var cl = Add([cx, cy], Mul(dir, p - dim));
moveTo(c, Add(cl, Mul(Rot90(dir), dim)))
lineTo(c, Add(cl, Mul(Rot90(dir), -dim)))
c.lineWidth = 2;
c.strokeStyle = 'green'
c.stroke()
var line = document.createElement('canvas')
var s = 1.3;
line.width = dim * 2 * s
line.height = 28
var lx = line.getContext('2d')
lx.rotate(-angle)
// lx.drawImage(img, cl[0] - dim, cl[1] - 50, dim * 2, 100, 0, 0, dim * 2, 100);
lx.drawImage(img, (-cl[0] + dim) * s, (-cl[1] + 9) * s, s * img.naturalWidth, s * img.naturalHeight);
document.body.appendChild(line)
var imdata = lx.getImageData(0, 0, line.width, line.height);
var rows = [];
for(var i = 0; i < imdata.width; i++){
var col = []
for(var j = 0; j < imdata.height; j++){
col.push(1 - imdata.data[4 * (i + j * imdata.width)] / 255);
}
rows.push(col)
}
console.log(rows)
//
document.body.appendChild(document.createElement('br'))
displayImage(rows);
var text = net.predictString(rows);
console.log(text)
document.body.appendChild(document.createTextNode(text))
document.body.appendChild(document.createElement('br'))
visualizeOutput(net.output);
document.body.appendChild(document.createElement('br'))
sparkline(net)
document.body.appendChild(document.createElement('br'))
})
// dd.fillStyle = 'green'
// for(var i = 0; i < metahist.length; i++){
// dd.fillRect(cx, cy + i, -metahist[i], 1)
// }
})
console.timeEnd('rotating calipers')
}
// hough transform, try to solve for a set of parallel lines at a particular orientation and line height (2 parameters)
// alternatively, use rotating calipers to solve for the orientation of a region
// or use rotating calipers as an initial estimate and do gradient-descent with hough transform (or ransac)
function wrap_contours(points){
var size = points.length;
var x0 = Infinity, y0 = Infinity, x1 = 0, y1 = 0;
for(var i = 0; i < size; i++){
var p = points[i];
var x = p % width, y = Math.floor(p / width);
x0 = Math.min(x0, x); y0 = Math.min(y0, y);
x1 = Math.max(x1, x); y1 = Math.max(y1, y);
}
var cy = y0 + (y1 - y0) / 2,
cx = x0 + (x1 - x0) / 2;
return {
x0: x0,
y0: y0,
y1: y1,
x1: x1,
cx: cx,
cy: cy,
width: x1 - x0 + 1,
height: y1 - y0 + 1,
size: size,
contours: points
}
}
</script>