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

94
LINES

< > BotCompany Repo | #350 // RecogUtil 2

Lua code

1  
get("#348") -- bright and rgb
2  
3  
function getLeftmostPoint(img, y)
4  
  local x1 = 0
5  
  while x1 < img.width and img.getInt(x1, y) >= 0xF00000 do
6  
    x1 = x1+1
7  
  end
8  
  return x1
9  
end
10  
11  
function getRightmostPoint(img, y)
12  
  local x2 = img.width
13  
  while x2 > 0 and img.getInt(x2-1, y) >= 0xF00000 do
14  
    x2 = x2-1
15  
  end
16  
  return x2
17  
end
18  
19  
function getWidth(img, y)
20  
  local x2 = getRightmostPoint(img, y)
21  
  local x1 = getLeftmostPoint(img, y)
22  
  return math.max(0, x2-x1)
23  
end
24  
25  
function getAverageWidth(img, y1, y2)
26  
  local sum = 0
27  
  for y = y1, y2-1 do
28  
    sum = sum+getWidth(img, y)
29  
  end
30  
  return sum/(y2-y1)
31  
end
32  
33  
function getAverageLeft(img, y1, y2)
34  
  y1, y2 = y1 or 0, y2 or img.height -- default arguments
35  
   
36  
  local sum = 0
37  
  for y = y1, y2-1 do
38  
    sum = sum+getLeftmostPoint(img, y)
39  
  end
40  
  return sum/(y2-y1)
41  
end
42  
43  
function getAverageLeft_debug(img, y1, y2)
44  
  y1, y2 = y1 or 0, y2 or img.height -- default arguments
45  
   
46  
  local sum = 0
47  
  for y = y1, y2-1 do
48  
    local x = getLeftmostPoint(img, y)
49  
    sum = sum+x
50  
    print("y", y, "left", x)
51  
  end
52  
  return sum/(y2-y1)
53  
end
54  
55  
function getMaxWidth(img, y1, y2)
56  
  local max = 0
57  
  for y = y1, y2-1 do
58  
    max = math.max(max, getWidth(img, y))
59  
  end
60  
  return max
61  
end
62  
63  
function upperWidth(img)
64  
  return getAverageWidth(img, 0, math.floor(img.height/3))
65  
end
66  
67  
function upperMaxWidth(img)
68  
  return getMaxWidth(img, 0, math.floor(img.height/3))
69  
end
70  
71  
function lowerWidth(img)
72  
  return getAverageWidth(img, math.floor(img.height*2/3), img.height)
73  
end
74  
75  
function lowerMaxWidth(img)
76  
  return getMaxWidth(img, math.floor(img.height*2/3), img.height)
77  
end
78  
79  
function upperToLowerWidth(img)
80  
  return upperWidth(img) / lowerWidth(img)
81  
end
82  
83  
function getCenterOfGravity(img)
84  
  local sumx, sumy, sumdensity = 0, 0, 0
85  
  for y=0, img.height-1 do
86  
    for x=0, img.width-1 do
87  
      local density = 1-bright(rgb(img.getInt(x,y)))
88  
      sumdensity = sumdensity+density
89  
      sumx = sumx+density*x
90  
      sumy = sumy+density*y
91  
    end
92  
  end
93  
  return sumx/sumdensity, sumy/sumdensity
94  
end

Author comment

Similar to #343, but with "img" argument

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: #350
Snippet name: RecogUtil 2
Eternal ID of this version: #350/1
Text MD5: ebcc917c4718f145d6b78a0e98205a19
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-01-26 02:10:04
Source code size: 2090 bytes / 94 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 720 / 151
Referenced in: [show references]