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

54
LINES

< > BotCompany Repo | #480 - Horizontal split with crop 2 (include)

Lua code

get("#348") -- rgb functions
get("#479") -- getcrop2
get("#337") -- map(list, f)
get("#388") -- rectangle functions

brightnessThreshold = 0.9

function horizontalsplit(img)
  local function isEmpty(x)
    for y=0, img.height-1 do
      if bright(rgb(img.getInt(x, y))) <= brightnessThreshold then
        return false
      end
    end
    return true
  end

  local lastEmpty, start = true, nil
  local segments = {}
  
  for x=0, img.width-1 do
    empty = isEmpty(x)
    if empty and not lastEmpty then
      table.insert(segments, {start, x})
    elseif not empty and lastEmpty then
      start = x
    end
    lastEmpty = empty
  end
  
  if not lastEmpty then
    table.insert(segments, {start, img.width})
  end
  
  if #segments <= 1 then -- don't bother with only 1 segment
    return {}
  end
  
  local result = {}
  for _, seg in ipairs(segments) do
    local sx1, sx2 = unpack(seg)
    
    local cropped = {
      width = sx2-sx1,
      height = img.height,
      getInt = function(x, y)
        return img.getInt(x+sx1, y)
      end
    }
    local x1, y1, x2, y2 = getcrop2(cropped)
    table.insert(result, newRectangle(x1+sx1, y1, x2-x1, y2-y1))
  end
  return result
end

Author comment

Began life as a copy of #478

test run  test run with input  download  show line numbers   

Travelled to 10 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz

No comments. add comment

Snippet ID: #480
Snippet name: Horizontal split with crop 2 (include)
Eternal ID of this version: #480/1
Text MD5: 66e3cd56a59ddf696cdc124b7de77631
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-02-25 19:35:22
Source code size: 1245 bytes / 54 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 460 / 73
Referenced in: [show references]

Formerly at http://tinybrain.de/480 & http://480.tinybrain.de