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

104
LINES

< > BotCompany Repo | #433 // iconDiff

Lua code

1  
--[[
2  
-- returns a value between 0 and 1
3  
function iconDiff_arrays(i1, i2)
4  
  local w, h = #i1[1], #i1
5  
  if w ~= #i2[1] or h ~= #i2 then
6  
    error("iconDiff: Icon size mismatch")
7  
  end
8  
  
9  
  local count, sum = w*h, 0
10  
  for y = 1, h do
11  
    local l1, l2 = i1[y], i2[y]
12  
    for x = 1, w do
13  
      local diff = math.abs(string.byte(l1, x)-string.byte(l2, x))
14  
      sum = sum+diff
15  
    end
16  
  end
17  
  
18  
  return sum/count
19  
end
20  
]]
21  
22  
function iconDiff(i1, i2)
23  
  local l = #i1
24  
  if l ~= #i2 then
25  
    error("iconDiff: Icon size mismatch ("..#i1.."/"..#i2..")")
26  
  end
27  
  
28  
  local count, sum = 0, 0
29  
  for i = 1, l do
30  
    local c1, c2 = string.byte(i1, i), string.byte(i2, i)
31  
    if (c1 == 46) ~= (c2 == 46) then
32  
      error("iconDiff: Icon size mismatch")
33  
    end
34  
    if c1 ~= 46 then
35  
      sum, count = sum+math.abs(c1-c2), count+1
36  
    end
37  
  end
38  
  
39  
  return sum/25/count
40  
end
41  
42  
-- limit = the maximum diff we are interested in
43  
-- (nil is returned when diff is equal or greater than the limit)
44  
-- w = width and height of the icons
45  
function iconDiffWithLimit_verbose(i1, i2, limit, w, h, verbose)
46  
  assert(type(limit) == 'number')
47  
  assert(type(w) == 'number')
48  
  assert(type(h) == 'number')
49  
  
50  
  if limit == 0 then return nil end
51  
  local l = #i1
52  
  if l ~= #i2 then
53  
    error("iconDiff: Icon size mismatch")
54  
  end
55  
  
56  
  local count, sum = w*h, 0
57  
  limit = limit*count*25
58  
  for i = 1, l do
59  
    local c1, c2 = string.sub(i1, i, i), string.sub(i2, i, i)
60  
    if verbose then
61  
      print("c1", c1, "c2", c2, "sum", sum)
62  
    end
63  
    if (c1 == '.') ~= (c2 == '.') then
64  
      error("iconDiff: Icon size mismatch")
65  
    end
66  
    if c1 ~= '.' then
67  
      local diff = math.abs(string.byte(c1)-string.byte(c2))
68  
      sum = sum+diff
69  
      if sum >= limit then return nil end
70  
    end
71  
  end
72  
  
73  
  return sum/25/count
74  
end
75  
76  
-- limit = the maximum diff we are interested in
77  
-- (nil is returned when diff is equal or greater than the limit)
78  
-- w = width and height of the icons
79  
function iconDiffWithLimit(i1, i2, limit, w, h)
80  
  assert(type(limit) == 'number')
81  
  assert(type(w) == 'number')
82  
  assert(type(h) == 'number')
83  
  
84  
  if limit == 0 then return nil end
85  
  local l = #i1
86  
  if l ~= #i2 then
87  
    error("iconDiff: Icon size mismatch")
88  
  end
89  
  
90  
  local count, sum = w*h, 0
91  
  limit = limit*count*25
92  
  for i = 1, l do
93  
    local c1, c2 = string.byte(i1, i), string.byte(i2, i)
94  
    if (c1 == 46) ~= (c2 == 46) then -- 46 is '.'
95  
      error("iconDiff: Icon size mismatch")
96  
    end
97  
    if c1 ~= 46 then
98  
      sum = sum+math.abs(c1-c2)
99  
      if sum >= limit then return nil end
100  
    end
101  
  end
102  
  
103  
  return sum/25/count
104  
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: 772 / 245
Referenced in: [show references]