Re: Bounding box on clusters in a 2D list

2005-04-25 Thread bearophileHUGS
Then you can probably use something like this: . def boxesArea(m, foreground=1, background=0): . maxr = len(m) . maxc = len(m[0]) . newCol = 2 . oldCol = foreground . for r,row in enumerate(m): . for c,e in enumerate(row): . if e == oldCol: .

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread Lonnie Princehouse
def floodFill4(m, r, c, newCol, oldCol): stack = [ (r, c) ] while stack: r, c = stack.pop() if c >=0 and c < maxc and r >=0 and r< maxr and m[r][c]==oldCol: m[r][c] = newCol stack += [ (r+1, c), (r-1, c), (r, c+1), (r, c-1) ] -- http://mail.python.org/mailman/listinfo/pyth

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread [EMAIL PROTECTED]
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 "

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread bearophileHUGS
In my first post you can find two URLs with better flood filling algorithms. You can also modify the easy recursive function, using a list-based stack intead of recursive calls. Bye, Bearophile -- http://mail.python.org/mailman/listinfo/python-list

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread [EMAIL PROTECTED]
Just was wondering how to integrate the floodfill with the stack.I guess it will get rid of recursion problem. You mean read all the elements of the array to a stack and then push and pop based on conditions? -- http://mail.python.org/mailman/listinfo/python-list

Re: Bounding box on clusters in a 2D list

2005-04-25 Thread [EMAIL PROTECTED]
bearphile, Is there a way I could do the floodfill rather iteratively than recursive. It somehow breaks although I made some optimisations to the code. -- http://mail.python.org/mailman/listinfo/python-list

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread bearophileHUGS
[EMAIL PROTECTED]: > hi Bearphile! That really gives me an idea.Thanks much for that. Yes as > you said the algorithm reaches a maximium recursion depth for larger > sets i tried. You can improve the little flood filling function, avoiding the "bad" Python recursivity. > Do you see where I am he

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread Bengt Richter
On 24 Apr 2005 09:44:49 -0700, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: >Richter,yes what I am looking for is for cluster with rectangular >bounding boxes as you explained in the first figure. > Sorry, not your fault, but I'm still not clear on diagonal connection/separation. E.g., (removin

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread [EMAIL PROTECTED]
hi Bearphile! That really gives me an idea.Thanks much for that. Yes as you said the algorithm reaches a maximium recursion depth for larger sets i tried.I still have a question. if m = [[0,0,0,0],[0,1,1,0,0],[0,0,1,0,0],[0,0,0,0]] all it does is count the number of 1's and return us the number in

Re: Bounding box on clusters in a 2D list

2005-04-24 Thread [EMAIL PROTECTED]
Richter,yes what I am looking for is for cluster with rectangular bounding boxes as you explained in the first figure. -- http://mail.python.org/mailman/listinfo/python-list

Re: Bounding box on clusters in a 2D list

2005-04-23 Thread Bengt Richter
On 23 Apr 2005 13:17:55 -0700, "[EMAIL PROTECTED]" <[EMAIL PROTECTED]> wrote: >If I have > >ex: x = [[1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0], > [1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0], > [1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0], > [0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]] >what I wan

Re: Bounding box on clusters in a 2D list

2005-04-23 Thread bearophileHUGS
I hope this is what you need, sometimes understanding the question is one of the hardest parts :-) If you can use a graph data structure, you can create a graph, and then you can find the lenght of all its connected components (indentations reduced to 2 spaces): . def mat2graph(g, m, good=None, w

Bounding box on clusters in a 2D list

2005-04-23 Thread [EMAIL PROTECTED]
If I have ex: x = [[1,1,1,0,0,0,0,0,0,0,0,1,1,1,1,0,0,0], [1,1,1,0,1,1,0,0,0,0,0,1,1,1,1,0,0,0], [1,1,1,0,1,1,1,0,1,1,0,1,1,1,0,0,0,0], [0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0]] what I want is a boundingbox over the region where we find clusters of 1's.So for instance in th