Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

55
LINES

< > BotCompany Repo | #479 // Embeddable border finder (getcrop2)

Lua code

1  
get("#348") -- rgb functions
2  
3  
brightnessThreshold = 0.9
4  
5  
function getcrop2(img)
6  
  local w, h, g = img.width, img.height, img.getInt
7  
  
8  
  local x1, y1, x2, y2 = 0, 0, w, h
9  
  
10  
  -- left side
11  
  
12  
  while x1 < x2 do
13  
    for y=y1, y2-1 do
14  
      if bright(rgb(g(x1, y))) <= brightnessThreshold then goto out0 end
15  
    end
16  
    x1=x1+1
17  
  end
18  
  ::out0::
19  
    
20  
  -- top side
21  
  
22  
  while y1 < y2 do
23  
    for x=x1, x2-1 do
24  
      if bright(rgb(g(x, y1))) <= brightnessThreshold then goto out1 end
25  
    end
26  
    y1=y1+1
27  
  end
28  
  ::out1::
29  
    
30  
  -- right side
31  
  
32  
  while x2 > x1 do
33  
    for y=y1, y2-1 do
34  
      if bright(rgb(g(x2-1, y))) <= brightnessThreshold then goto out2 end
35  
    end
36  
    x2=x2-1
37  
  end
38  
  ::out2::
39  
    
40  
  -- bottom side
41  
  
42  
  while y2 > y1 do
43  
    for x=x1, x2-1 do
44  
      if bright(rgb(g(x, y2-1))) <= brightnessThreshold then goto out3 end
45  
    end
46  
    y2=y2-1
47  
  end
48  
  ::out3::
49  
  
50  
  if x2 <= x1 or y2 <= y1 then -- weird result? return full image
51  
    x1, y1, x2, y2 = 0, 0, w, h
52  
  end
53  
  
54  
  return x1, y1, x2, y2
55  
end

Author comment

Began life as a copy of #346

Assumes light background

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

Snippet ID: #479
Snippet name: Embeddable border finder (getcrop2)
Eternal ID of this version: #479/1
Text MD5: 3e44e068229f1d1ff9a50cbfe2f82e8c
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-03-08 17:45:03
Source code size: 1062 bytes / 55 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 647 / 155
Referenced in: [show references]