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

104
LINES

< > BotCompany Repo | #433 // iconDiff

Lua code

--[[
-- returns a value between 0 and 1
function iconDiff_arrays(i1, i2)
  local w, h = #i1[1], #i1
  if w ~= #i2[1] or h ~= #i2 then
    error("iconDiff: Icon size mismatch")
  end
  
  local count, sum = w*h, 0
  for y = 1, h do
    local l1, l2 = i1[y], i2[y]
    for x = 1, w do
      local diff = math.abs(string.byte(l1, x)-string.byte(l2, x))
      sum = sum+diff
    end
  end
  
  return sum/count
end
]]

function iconDiff(i1, i2)
  local l = #i1
  if l ~= #i2 then
    error("iconDiff: Icon size mismatch ("..#i1.."/"..#i2..")")
  end
  
  local count, sum = 0, 0
  for i = 1, l do
    local c1, c2 = string.byte(i1, i), string.byte(i2, i)
    if (c1 == 46) ~= (c2 == 46) then
      error("iconDiff: Icon size mismatch")
    end
    if c1 ~= 46 then
      sum, count = sum+math.abs(c1-c2), count+1
    end
  end
  
  return sum/25/count
end

-- limit = the maximum diff we are interested in
-- (nil is returned when diff is equal or greater than the limit)
-- w = width and height of the icons
function iconDiffWithLimit_verbose(i1, i2, limit, w, h, verbose)
  assert(type(limit) == 'number')
  assert(type(w) == 'number')
  assert(type(h) == 'number')
  
  if limit == 0 then return nil end
  local l = #i1
  if l ~= #i2 then
    error("iconDiff: Icon size mismatch")
  end
  
  local count, sum = w*h, 0
  limit = limit*count*25
  for i = 1, l do
    local c1, c2 = string.sub(i1, i, i), string.sub(i2, i, i)
    if verbose then
      print("c1", c1, "c2", c2, "sum", sum)
    end
    if (c1 == '.') ~= (c2 == '.') then
      error("iconDiff: Icon size mismatch")
    end
    if c1 ~= '.' then
      local diff = math.abs(string.byte(c1)-string.byte(c2))
      sum = sum+diff
      if sum >= limit then return nil end
    end
  end
  
  return sum/25/count
end

-- limit = the maximum diff we are interested in
-- (nil is returned when diff is equal or greater than the limit)
-- w = width and height of the icons
function iconDiffWithLimit(i1, i2, limit, w, h)
  assert(type(limit) == 'number')
  assert(type(w) == 'number')
  assert(type(h) == 'number')
  
  if limit == 0 then return nil end
  local l = #i1
  if l ~= #i2 then
    error("iconDiff: Icon size mismatch")
  end
  
  local count, sum = w*h, 0
  limit = limit*count*25
  for i = 1, l do
    local c1, c2 = string.byte(i1, i), string.byte(i2, i)
    if (c1 == 46) ~= (c2 == 46) then -- 46 is '.'
      error("iconDiff: Icon size mismatch")
    end
    if c1 ~= 46 then
      sum = sum+math.abs(c1-c2)
      if sum >= limit then return nil end
    end
  end
  
  return sum/25/count
end

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: #433
Snippet name: iconDiff
Eternal ID of this version: #433/1
Text MD5: 4ef7f1b4e5ce02d7e844fc8db8842acf
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-09 15:28:34
Source code size: 2665 bytes / 104 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 770 / 244
Referenced in: [show references]