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: 791 / 209
Referenced in: #409 - Grid + Floodfill
#411 - Grid + Floodfill with hashes
#416 - Grid + Floodfill with rough hashes
#418 - Grid + Floodfill with string rep
#420 - Grid + Floodfill with hashes / limited start area (for start menus)
#421 - Grid + Floodfill with hashes / limited start area (for start menus), optimized
#422 - Grid + Floodfill with hashes (with virtual stack)
#423 - Grid + Floodfill with hashes (hacked for galculator)
#442 - Grid + Floodfill with hashes (size 3)
#444 - White Grid + Floodfill
#445 - White Grid
#461 - Grid + Floodfill with hashes (size 2)
#463 - Grid + Floodfill (size = 50)
#464 - Grid + Floodfill (size = 15)
#465 - Grid + Floodfill (size = 10)
#469 - Grid + Floodfill with hashes (for lighter elements)
#3000202 - Answer for stefanreich (>> T conversion bot)
#3000238 - Answer for stefanreich (>> t power bot)
#3000382 - Answer for ferdie (>> t = 1, f = 0)
#3000383 - Answer for funkoverflow (>> t=1, f=0 okay)