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

55
LINES

< > BotCompany Repo | #479 // Embeddable border finder (getcrop2)

Lua code

get("#348") -- rgb functions

brightnessThreshold = 0.9

function getcrop2(img)
  local w, h, g = img.width, img.height, img.getInt
  
  local x1, y1, x2, y2 = 0, 0, w, h
  
  -- left side
  
  while x1 < x2 do
    for y=y1, y2-1 do
      if bright(rgb(g(x1, y))) <= brightnessThreshold then goto out0 end
    end
    x1=x1+1
  end
  ::out0::
    
  -- top side
  
  while y1 < y2 do
    for x=x1, x2-1 do
      if bright(rgb(g(x, y1))) <= brightnessThreshold then goto out1 end
    end
    y1=y1+1
  end
  ::out1::
    
  -- right side
  
  while x2 > x1 do
    for y=y1, y2-1 do
      if bright(rgb(g(x2-1, y))) <= brightnessThreshold then goto out2 end
    end
    x2=x2-1
  end
  ::out2::
    
  -- bottom side
  
  while y2 > y1 do
    for x=x1, x2-1 do
      if bright(rgb(g(x, y2-1))) <= brightnessThreshold then goto out3 end
    end
    y2=y2-1
  end
  ::out3::
  
  if x2 <= x1 or y2 <= y1 then -- weird result? return full image
    x1, y1, x2, y2 = 0, 0, w, h
  end
  
  return x1, y1, x2, y2
end

Author comment

Began life as a copy of #346

Assumes light background

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: #479
Snippet name: Embeddable border finder (getcrop2)
Eternal ID of this version: #479/1
Text MD5: 3e44e068229f1d1ff9a50cbfe2f82e8c
Author: stefan
Category: image recognition
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-03-08 17:45:03
Source code size: 1062 bytes / 55 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 642 / 154
Referenced in: [show references]