1 | -- load image to recognize |
2 | loadSnippet("#100")() |
3 | |
4 | minWidth=10 |
5 | minHeight=5 |
6 | bgcolor=0 |
7 | |
8 | function isSet(pixel) |
9 | return pixel ~= nil and pixel ~= bgcolor |
10 | end |
11 | |
12 | function on(x, y) |
13 | return x >= 0 and y >= 0 and x < width and y < height |
14 | and isSet(pixels[y*width+x]) |
15 | end |
16 | |
17 | function check(x, y, pivot) |
18 | if pivot == 2 then return true end |
19 | local is = on(x, y) == true |
20 | local should = pivot == 1 |
21 | --print("check", x, y, is, should) |
22 | return is == should |
23 | end |
24 | |
25 | function detectNode(x, y, a, b, c, d, e, f, g, h, i) |
26 | --print("xy", x, y, "abcdef", a, b, c, d, e, f) |
27 | return check(x-1, y-1, a) |
28 | and check(x, y-1, b) |
29 | and check(x+1, y-1, c) |
30 | and check(x-1, y, d) |
31 | and check(x, y, e) |
32 | and check(x+1, y, f) |
33 | and check(x-1, y+1, g) |
34 | and check(x, y+1, h) |
35 | and check(x+1, y+1, i) |
36 | end |
37 | |
38 | function detect(x1, y1) |
39 | if not detectNode(x1, y1, 0, 0, 0, 0, 1, 1, 0, 1, 0) then return end |
40 | --print("Corner found! ", x1, y1) |
41 | local x=x1+1 |
42 | while on(x, y1) and not on(x, y1-1) and not on(x, y1+1) |
43 | and x < width do x=x+1 end |
44 | if x<x1+minWidth then return end |
45 | local y=y1+1 |
46 | while on(x, y) and not on(x-1, y) and not on(x+1, y1) |
47 | and y < height do y=y+1 end |
48 | if y<y1+minHeight then return end |
49 | |
50 | -- TODO: check bottom and left |
51 | |
52 | return {x1, y1, x-x1+1, y-y1+1} |
53 | end |
54 | |
55 | function clearRectangle(p) |
56 | local x1, y1, w, h = unpack(p) |
57 | for x=0, w-1 do |
58 | pixels[(x1+x)+y1*width] = bgcolor |
59 | pixels[(x1+x)+(y1+h-1)*width] = bgcolor |
60 | end |
61 | for y=1, h-2 do |
62 | pixels[x1+(y1+y)*width] = bgcolor |
63 | pixels[x1+w-1+(y1+y)*width] = bgcolor |
64 | end |
65 | end |
66 | |
67 | positions = {} |
68 | |
69 | for y1=0,height-1 do |
70 | for x1=0,width-1 do |
71 | local p = detect(x1,y1) |
72 | if p then |
73 | print("Rectangle found: "..tostring(p[1]).." "..tostring(p[2]).." "..tostring(p[3]).." "..tostring(p[4])) |
74 | clearRectangle(p) |
75 | table.insert(positions, p) |
76 | end |
77 | end |
78 | end |
test run test run with input download show line numbers
Travelled to 12 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt
No comments. add comment
Recognizer | Recognition Result | Visualize | Recalc |
---|---|---|---|
#308 | 1916 | [visualize] |
Snippet ID: | #101 |
Snippet name: | Rectangle finder |
Eternal ID of this version: | #101/1 |
Text MD5: | d1c0bfa8f9b1de883bff904af4ea7947 |
Author: | stefan |
Category: | image recognition |
Type: | Lua code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2014-01-07 23:12:18 |
Source code size: | 1916 bytes / 78 lines |
Pitched / IR pitched: | Yes / Yes |
Views / Downloads: | 1020 / 233 |
Referenced in: | [show references] |