1 | -- needs rgb functions and rectangle functions |
2 | |
3 | function shrinkRectangle(r, img) |
4 | local w, h = img.width, img.height |
5 | |
6 | local function g(x, y) |
7 | return bright(rgb(img.getInt(x, y))) |
8 | end |
9 | |
10 | local x1, y1, x2, y2 = r.x, r.y, r.x+r.width, r.y+r.height |
11 | |
12 | -- left side |
13 | |
14 | while x1 < x2 do |
15 | for y=y1, y2-1 do |
16 | if g(x1, y) <= 0.5 then goto end1 end |
17 | end |
18 | x1=x1+1 |
19 | end |
20 | ::end1:: |
21 | |
22 | -- top side |
23 | |
24 | while y1 < y2 do |
25 | for x=x1, x2-1 do |
26 | if g(x, y1) <= 0.5 then goto end2 end |
27 | end |
28 | y1=y1+1 |
29 | end |
30 | ::end2:: |
31 | |
32 | -- right side |
33 | |
34 | while x2 > x1 do |
35 | for y=y1, y2-1 do |
36 | if g(x2-1, y) <= 0.5 then goto end3 end |
37 | end |
38 | x2=x2-1 |
39 | end |
40 | ::end3:: |
41 | |
42 | -- bottom side |
43 | |
44 | while y2 > y1 do |
45 | for x=x1, x2-1 do |
46 | if g(x, y2-1) <= 0.5 then goto end4 end |
47 | end |
48 | y2=y2-1 |
49 | end |
50 | ::end4:: |
51 | |
52 | if x2 <= x1 or y2 <= y1 then |
53 | return r |
54 | else |
55 | return newRectangle(x1, y1, x2-x1, y2-y1) |
56 | end |
57 | end |
test run test run with input download show line numbers
Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, znullqslnikg
No comments. add comment
Snippet ID: | #410 |
Snippet name: | shrinkRectangle |
Eternal ID of this version: | #410/1 |
Text MD5: | 473dfa909d260ec847a41eacafd5d020 |
Author: | stefan |
Category: | image recognition |
Type: | Lua code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2015-02-06 00:08:54 |
Source code size: | 1006 bytes / 57 lines |
Pitched / IR pitched: | No / Yes |
Views / Downloads: | 790 / 209 |
Referenced in: | [show references] |