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

78
LINES

< > BotCompany Repo | #101 // Rectangle finder

Lua code

-- load image to recognize
loadSnippet("#100")()

minWidth=10
minHeight=5
bgcolor=0

function isSet(pixel)
  return pixel ~= nil and pixel ~= bgcolor
end

function on(x, y)
  return x >= 0 and y >= 0 and x < width and y < height
    and isSet(pixels[y*width+x])
end

function check(x, y, pivot)
  if pivot == 2 then return true end
  local is = on(x, y) == true
  local should = pivot == 1
  --print("check", x, y, is, should)
  return is == should
end

function detectNode(x, y, a, b, c, d, e, f, g, h, i)
  --print("xy", x, y, "abcdef", a, b, c, d, e, f)
  return check(x-1, y-1, a)
    and check(x, y-1, b)
    and check(x+1, y-1, c)
    and check(x-1, y, d)
    and check(x, y, e)
    and check(x+1, y, f)
    and check(x-1, y+1, g)
    and check(x, y+1, h)
    and check(x+1, y+1, i)
end

function detect(x1, y1)
  if not detectNode(x1, y1, 0, 0, 0, 0, 1, 1, 0, 1, 0) then return end
  --print("Corner found! ", x1, y1)
  local x=x1+1
  while on(x, y1) and not on(x, y1-1) and not on(x, y1+1)
    and x < width do x=x+1 end
  if x<x1+minWidth then return end
  local y=y1+1
  while on(x, y) and not on(x-1, y) and not on(x+1, y1)
    and y < height do y=y+1 end
  if y<y1+minHeight then return end

  -- TODO: check bottom and left

  return {x1, y1, x-x1+1, y-y1+1}
end

function clearRectangle(p)
  local x1, y1, w, h = unpack(p)
  for x=0, w-1 do
    pixels[(x1+x)+y1*width] = bgcolor
    pixels[(x1+x)+(y1+h-1)*width] = bgcolor
  end
  for y=1, h-2 do
    pixels[x1+(y1+y)*width] = bgcolor
    pixels[x1+w-1+(y1+y)*width] = bgcolor
  end
end

positions = {}

for y1=0,height-1 do
  for x1=0,width-1 do
    local p = detect(x1,y1)
    if p then
      print("Rectangle found: "..tostring(p[1]).." "..tostring(p[2]).." "..tostring(p[3]).." "..tostring(p[4]))
      clearRectangle(p)
      table.insert(positions, p)
    end
  end
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

Image recognition results

Recognizer Recognition Result Visualize Recalc
#308 1916 [visualize]

Snippet ID: #101
Snippet name: Rectangle finder
Eternal ID of this version: #101/1
Text MD5: d1c0bfa8f9b1de883bff904af4ea7947
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2014-01-07 23:12:18
Source code size: 1916 bytes / 78 lines
Pitched / IR pitched: Yes / Yes
Views / Downloads: 1019 / 233
Referenced in: [show references]