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

77
LINES

< > BotCompany Repo | #413 // Symbol Finder (include)

Lua code

1  
get("#388") -- rect functions
2  
get("#137") -- split
3  
get("#390") -- msinsert
4  
get("#348") -- rgb functions
5  
6  
_debug = false
7  
8  
function findSymbol(img, icon, minSimilarity)
9  
  -- split icon into lines
10  
  if type(icon) == 'string' then icon = split(icon, "\n") end
11  
  local w, h = #icon[1], #icon
12  
  if _debug then
13  
    print("w/h, img="..w.."/"..h..", "..img.width.."/"..img.height)
14  
  end
15  
16  
  -- count non-question marks
17  
  local count = 0
18  
  for yy=1, h do
19  
    line = icon[yy]
20  
    for xx=1, w do
21  
      c = line:sub(xx, xx)
22  
      if c ~= '?' then
23  
        count=count+1
24  
      end
25  
    end
26  
  end
27  
28  
  local limit = count*25/100
29  
  
30  
  if _debug then
31  
    print("count/limit: "..count.."/"..limit)
32  
  end
33  
34  
  for y=0, img.height-h do
35  
    for x=0, img.width-w do
36  
      if _debug then
37  
        print("Testing "..x.."/"..y)
38  
      end
39  
      local total, innerPixels = 0, {}
40  
      local sim
41  
    
42  
      for yy=y, y+h-1 do
43  
        local line = icon[yy-y+1]
44  
        for xx=x, x+w-1 do
45  
          local pix = img.getInt(xx, yy)
46  
          local c = line:sub(xx-x+1, xx-x+1)
47  
          if c == '?' then
48  
            msinsert(innerPixels, pix)
49  
          else
50  
            local b = math.floor(bright(rgb(pix))*25+0.5)
51  
            local lb = string.byte(c)-string.byte("A")
52  
            local diff = math.abs(b-lb)
53  
            total = total+diff
54  
            if total > limit then
55  
              if _debug then print("limit reached") end
56  
              goto out
57  
            end
58  
          end
59  
        end
60  
      end
61  
    
62  
      sim = math.floor(100*(1-total/(count*25))+0.5)
63  
      if _debug then
64  
        print("Similarity at "..x.."/"..y..": "..sim.."%")
65  
      end
66  
    
67  
      if sim >= minSimilarity then
68  
        return newRectangle(x, y, w, h)
69  
      end
70  
    
71  
      ::out::
72  
      if _debug then
73  
        print("Skipping "..x.."/"..y)
74  
      end
75  
    end
76  
  end
77  
end

Author comment

Began life as a copy of #394

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: #413
Snippet name: Symbol Finder (include)
Eternal ID of this version: #413/1
Text MD5: 611aae6e3a63f444cf32a79858d87464
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-06 02:14:10
Source code size: 1888 bytes / 77 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 806 / 166
Referenced in: [show references]