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

65
LINES

< > BotCompany Repo | #428 - LXTerminal Contents Finder (include)

Lua code

1  
get("#388") -- rect functions
2  
get("#348") -- rgb functions
3  
get("#356") -- num2hex
4  
5  
function findContents()
6  
  hashes = otherresults["#442"] or error("Need result of #442, please recalc")
7  
  
8  
  j, icons = nil, {}
9  
  while true do
10  
    i, j, r, hash = string.find(hashes, "(%d+, %d+, %d+, %d+) %-> ([a-f0-9]+)", j)
11  
    if i == nil then break end
12  
    
13  
    icons[hash] = r
14  
  end
15  
  
16  
  r = icons["00645074"] or icons["0061777a"]
17  
  if r == nil then return "LXTerminal icon not found" end
18  
  
19  
  -- look for a mostly black line below the icon
20  
  r = stringtorect(r)
21  
  x, y = r.x, r.y
22  
  contents_y1 = nil
23  
  while y < img.height and contents_y1 == nil do
24  
    w = 50
25  
    b = 0
26  
    for xx = x, x+w-1 do
27  
      b = b+bright(rgb(img.getInt(xx, y)))
28  
    end
29  
    b = b/w
30  
    --print("Brightness at", y, ":", b)
31  
    if b < 0.01 then
32  
      contents_y1 = y
33  
    end
34  
    y = y+1
35  
  end
36  
  
37  
  if contents_y1 == nil then return "Terminal contents not found" end
38  
  
39  
  -- find left side of contents
40  
  
41  
  contents_x1 = x
42  
  while contents_x1 > 0 and img.getInt(contents_x1-1, contents_y1) == 0 do
43  
    contents_x1 = contents_x1-1
44  
  end
45  
  
46  
  -- find width of contents
47  
  while x < img.width and img.getInt(x, contents_y1) == 0 do
48  
    x = x+1
49  
  end
50  
  
51  
  contents_x2 = x-1
52  
  
53  
  -- find bottom of contents
54  
  y = contents_y1+1
55  
  
56  
  while y < img.height and
57  
    (img.getInt(contents_x1, y) ~= 0xa4a4a4 or
58  
     img.getInt(contents_x1+1, y) ~= 0xa4a4a4) do
59  
    --print("y", y, num2hex(img.getInt(contents_x1, y)), num2hex(img.getInt(contents_x1+1, y)))
60  
    y = y+1
61  
  end
62  
  
63  
  contents_y2 = y
64  
  contents_y1 = contents_y1+1
65  
end

Author comment

Began life as a copy of #424

test run  test run with input  download  show line numbers   

Travelled to 8 computer(s): aoiabmzegqzx, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #428
Snippet name: LXTerminal Contents Finder (include)
Eternal ID of this version: #428/1
Text MD5: 4ea8daaf613bf90210cb565921e7759b
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-09 20:38:15
Source code size: 1644 bytes / 65 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 634 / 199
Referenced in: [show references]

Formerly at http://tinybrain.de/428 & http://428.tinybrain.de