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

47
LINES

< > BotCompany Repo | #339 // Embeddable Signature Maker

Lua code

brightnessThreshold = 0.9
maxSignatureLen = 10

function rgb(i)
  local r = bit32.band(bit32.rshift(i, 16), 255)
    local g = bit32.band(bit32.rshift(i, 8), 255)
    local b = bit32.band(i, 255)
  return r, g, b
end

function bright(r, g, b)
  return (r+g+b)/(255*3)
end

-- from Java class "SegmentCounter"

function countSegments(y, flip)
  local lastPix = 1
  local segments = 0
  for x = 0, (flip and h or w)-1 do
    local pix = bright(rgb(flip and img.getInt(y, x) or img.getInt(x, y)))
    if pix >= brightnessThreshold and lastPix < brightnessThreshold then
      segments = segments+1
    end
    lastPix = pix
  end
  if lastPix < brightnessThreshold then segments = segments+1 end
  return segments
end

function getSegmentSignature(flip)
  local s = ""
  
  local lastN = 0
  for y = 0, (flip and w or h)-1 do
    local n = math.min(countSegments(y, flip), 9)
    if n ~= lastN then
      if #s >= maxSignatureLen then return nil end
      s = s..n
    end
    lastN = n
  end
  if lastN == 0 and #s > 0 then
    s = s:sub(1, #s-1)
  end
  return s
end

Author comment

Began life as a copy of #334

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

2 comment(s) hidden. show

Snippet ID: #339
Snippet name: Embeddable Signature Maker
Eternal ID of this version: #339/1
Text MD5: 841909b563987c2791a18ce0639f0773
Author: stefan
Category:
Type: Lua code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-01-25 01:56:44
Source code size: 1113 bytes / 47 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 1075 / 175
Referenced in: [show references]