Commit f29501e3 authored by Kasalehlia's avatar Kasalehlia

added line normalization for simple lines

parent 9830dbd7
def sectionToPath(sec):
"""calculates the stitching path for this section,
returning a list of positions to be stitched"""
return reduce(lambda a,b: a+b, map(lambda a:[a,a+(1,1),a+(0,1),a+(1,0)], sorted(sec, key=lambda x:x[1])))
if len(sec) == 1: # single pixel
a = sec.pop()
return [a,a+(1,1),a+(0,1),a+(1,0)]
line = normalizeLine(sec)
if line:
return pathForLine(line)
else:
#raise ValueError('Image contains currently unsupported sections')
#print "unsupported section"
#return [(0,0),(0,0)]
return reduce(lambda a,b: a+b, map(lambda a:[a,(a[0]+1,a[1]+1),(a[0],a[1]+1),(a[0]+1,a[1])], sorted(sec, key=lambda x:x[1])))
return reduce(lambda a,b: a+b, map(lambda a:[a,a+(1,1),a+(0,1),a+(1,0)], sorted(sec, key=lambda x:x[1])))
# ROUTE SECTION TYPES
# SECTION TYPES ROUTING
def pathForLine(sec):
pass
pass
def pathForMultiLine(sec):
pass
pass
# SECTIONS
......@@ -54,13 +57,31 @@ def _findSingleSection(data, i, j, section, background):
# HELPERS
def normalizeLine(sec):
"""tries to convert a section to a list of pixels following a line"""
# simple line
if len(sec) < 2:
return None
if len([p for p in sec if len(getAdjacent(sec,p)) <= 2]) is len(sec):
starts = [p for p in sec if len(getAdjacent(sec,p)) == 1]
if len(starts): #line has 2 ends
start = starts[0]
else: #line is circular
start = iter(sec).next()
line = [start]
while len(line) < len(sec):
line.append([p for p in getAdjacent(sec,line[-1]) if p not in line][0])
return line
#TODO not so simple lines
return None
def chunks(l, n):
"""splits an iterable into *n*-sized chunks"""
for i in xrange(0, len(l), n):
yield l[i:i+n]
def getAdjacent(section, target):
"""counts the adjacent pixels within the section for a target"""
"""returns the adjacent pixels within the section for a target"""
if type(section) is set:
return [(target[0]+i,target[1]+j) for i in [-1,0,1] for j in [-1,0,1] if (target[0]+i,target[1]+j) in section and (i,j) != (0,0)]
elif type(section) is list:
......@@ -68,8 +89,8 @@ def getAdjacent(section, target):
return [section[i] for i in range(len(section)) if abs(i-index) == 1]
def T(a,b):
return Tp((a,b))
return Tp((a,b))
class Tp(tuple):
def __add__(self, other):
return Tp(x + y for x, y in zip(self, other))
def __add__(self, other):
return Tp(x + y for x, y in zip(self, other))
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment