Bounding box on clusters in a 2D list
superprad at gmail.com
superprad at gmail.com
Mon Apr 25 17:13:34 EDT 2005
Bearophile,I have written the floodfill in python with stack. But i
think I am making something wrong I dont get what i need.Need your
opinion.the code follows
def connected(m, foreground=1, background=0):
def floodFill4(m, r,c, newCol, oldCol):
if newCol == oldCol:
print "empty stack"
return -1
while(m[r].pop(c)):
m[r][c] = newCol
if r+1 < maxr and m[r+1][c]==oldCol:
if m[r+1][c] == 0:return
if r-1 >= 0 and m[r-1][c]==oldCol:
if m[r-1][c] == 0:return
if c+1 < maxc and m[r][c+1]==oldCol:
if m[r][c+1] == 0:return
if c-1 >= 0 and m[r][c-1]==oldCol:
if m[r][c-1] == 0:return
maxr = len(m)
maxc = len(m[0])
newCol = 2
oldCol = foreground
for r in xrange(maxr):
for c in xrange(maxc):
if m[r][c] == oldCol:
floodFill4(m, r,c, newCol=newCol, oldCol=oldCol)
newCol += 1
More information about the Python-list
mailing list