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

77
LINES

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

Lua code

get("#388") -- rect functions
get("#137") -- split
get("#390") -- msinsert
get("#348") -- rgb functions

_debug = false

function findSymbol(img, icon, minSimilarity)
  -- split icon into lines
  if type(icon) == 'string' then icon = split(icon, "\n") end
  local w, h = #icon[1], #icon
  if _debug then
    print("w/h, img="..w.."/"..h..", "..img.width.."/"..img.height)
  end

  -- count non-question marks
  local count = 0
  for yy=1, h do
    line = icon[yy]
    for xx=1, w do
      c = line:sub(xx, xx)
      if c ~= '?' then
        count=count+1
      end
    end
  end

  local limit = count*25/100
  
  if _debug then
    print("count/limit: "..count.."/"..limit)
  end

  for y=0, img.height-h do
    for x=0, img.width-w do
      if _debug then
        print("Testing "..x.."/"..y)
      end
      local total, innerPixels = 0, {}
      local sim
    
      for yy=y, y+h-1 do
        local line = icon[yy-y+1]
        for xx=x, x+w-1 do
          local pix = img.getInt(xx, yy)
          local c = line:sub(xx-x+1, xx-x+1)
          if c == '?' then
            msinsert(innerPixels, pix)
          else
            local b = math.floor(bright(rgb(pix))*25+0.5)
            local lb = string.byte(c)-string.byte("A")
            local diff = math.abs(b-lb)
            total = total+diff
            if total > limit then
              if _debug then print("limit reached") end
              goto out
            end
          end
        end
      end
    
      sim = math.floor(100*(1-total/(count*25))+0.5)
      if _debug then
        print("Similarity at "..x.."/"..y..": "..sim.."%")
      end
    
      if sim >= minSimilarity then
        return newRectangle(x, y, w, h)
      end
    
      ::out::
      if _debug then
        print("Skipping "..x.."/"..y)
      end
    end
  end
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: 755 / 146
Referenced in: [show references]