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

47
LINES

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

Lua code

1  
brightnessThreshold = 0.9
2  
maxSignatureLen = 10
3  
4  
function rgb(i)
5  
  local r = bit32.band(bit32.rshift(i, 16), 255)
6  
    local g = bit32.band(bit32.rshift(i, 8), 255)
7  
    local b = bit32.band(i, 255)
8  
  return r, g, b
9  
end
10  
11  
function bright(r, g, b)
12  
  return (r+g+b)/(255*3)
13  
end
14  
15  
-- from Java class "SegmentCounter"
16  
17  
function countSegments(y, flip)
18  
  local lastPix = 1
19  
  local segments = 0
20  
  for x = 0, (flip and h or w)-1 do
21  
    local pix = bright(rgb(flip and img.getInt(y, x) or img.getInt(x, y)))
22  
    if pix >= brightnessThreshold and lastPix < brightnessThreshold then
23  
      segments = segments+1
24  
    end
25  
    lastPix = pix
26  
  end
27  
  if lastPix < brightnessThreshold then segments = segments+1 end
28  
  return segments
29  
end
30  
31  
function getSegmentSignature(flip)
32  
  local s = ""
33  
  
34  
  local lastN = 0
35  
  for y = 0, (flip and w or h)-1 do
36  
    local n = math.min(countSegments(y, flip), 9)
37  
    if n ~= lastN then
38  
      if #s >= maxSignatureLen then return nil end
39  
      s = s..n
40  
    end
41  
    lastN = n
42  
  end
43  
  if lastN == 0 and #s > 0 then
44  
    s = s:sub(1, #s-1)
45  
  end
46  
  return s
47  
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

Comments [hide]

ID Author/Program Comment Date
559 #1000610 Edit suggestion:
!636
!629

main {
static Object androidContext;
static String programID;

public static void main(String[] args) throws Exception {
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

}}
2015-08-18 20:25:42  delete 
557 #1000604 (pitcher) 2015-08-18 00:07:22

add comment

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: 972 / 156
Referenced in: [show references]