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

94
LINES

< > BotCompany Repo | #350 // RecogUtil 2

Lua code

get("#348") -- bright and rgb

function getLeftmostPoint(img, y)
  local x1 = 0
  while x1 < img.width and img.getInt(x1, y) >= 0xF00000 do
    x1 = x1+1
  end
  return x1
end

function getRightmostPoint(img, y)
  local x2 = img.width
  while x2 > 0 and img.getInt(x2-1, y) >= 0xF00000 do
    x2 = x2-1
  end
  return x2
end

function getWidth(img, y)
  local x2 = getRightmostPoint(img, y)
  local x1 = getLeftmostPoint(img, y)
  return math.max(0, x2-x1)
end

function getAverageWidth(img, y1, y2)
  local sum = 0
  for y = y1, y2-1 do
    sum = sum+getWidth(img, y)
  end
  return sum/(y2-y1)
end

function getAverageLeft(img, y1, y2)
  y1, y2 = y1 or 0, y2 or img.height -- default arguments
   
  local sum = 0
  for y = y1, y2-1 do
    sum = sum+getLeftmostPoint(img, y)
  end
  return sum/(y2-y1)
end

function getAverageLeft_debug(img, y1, y2)
  y1, y2 = y1 or 0, y2 or img.height -- default arguments
   
  local sum = 0
  for y = y1, y2-1 do
    local x = getLeftmostPoint(img, y)
    sum = sum+x
    print("y", y, "left", x)
  end
  return sum/(y2-y1)
end

function getMaxWidth(img, y1, y2)
  local max = 0
  for y = y1, y2-1 do
    max = math.max(max, getWidth(img, y))
  end
  return max
end

function upperWidth(img)
  return getAverageWidth(img, 0, math.floor(img.height/3))
end

function upperMaxWidth(img)
  return getMaxWidth(img, 0, math.floor(img.height/3))
end

function lowerWidth(img)
  return getAverageWidth(img, math.floor(img.height*2/3), img.height)
end

function lowerMaxWidth(img)
  return getMaxWidth(img, math.floor(img.height*2/3), img.height)
end

function upperToLowerWidth(img)
  return upperWidth(img) / lowerWidth(img)
end

function getCenterOfGravity(img)
  local sumx, sumy, sumdensity = 0, 0, 0
  for y=0, img.height-1 do
    for x=0, img.width-1 do
      local density = 1-bright(rgb(img.getInt(x,y)))
      sumdensity = sumdensity+density
      sumx = sumx+density*x
      sumy = sumy+density*y
    end
  end
  return sumx/sumdensity, sumy/sumdensity
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: 715 / 150
Referenced in: [show references]