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

57
LINES

< > BotCompany Repo | #410 // shrinkRectangle

Lua code

-- needs rgb functions and rectangle functions

function shrinkRectangle(r, img)
  local w, h = img.width, img.height
  
  local function g(x, y)
    return bright(rgb(img.getInt(x, y)))
  end
  
  local x1, y1, x2, y2 = r.x, r.y, r.x+r.width, r.y+r.height
  
  -- left side
  
  while x1 < x2 do
    for y=y1, y2-1 do
      if g(x1, y) <= 0.5 then goto end1 end
    end
    x1=x1+1
  end
  ::end1::
    
  -- top side
  
  while y1 < y2 do
    for x=x1, x2-1 do
      if g(x, y1) <= 0.5 then goto end2 end
    end
    y1=y1+1
  end
  ::end2::
    
  -- right side
  
  while x2 > x1 do
    for y=y1, y2-1 do
      if g(x2-1, y) <= 0.5 then goto end3 end
    end
    x2=x2-1
  end
  ::end3::
    
  -- bottom side
  
  while y2 > y1 do
    for x=x1, x2-1 do
      if g(x, y2-1) <= 0.5 then goto end4 end
    end
    y2=y2-1
  end
  ::end4::
  
  if x2 <= x1 or y2 <= y1 then
    return r
  else
    return newRectangle(x1, y1, x2-x1, y2-y1)
  end
end

test run  test run with input  download  show line numbers   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt, znullqslnikg

No comments. add comment

Snippet ID: #410
Snippet name: shrinkRectangle
Eternal ID of this version: #410/1
Text MD5: 473dfa909d260ec847a41eacafd5d020
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-06 00:08:54
Source code size: 1006 bytes / 57 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 789 / 209
Referenced in: [show references]