[EMAIL PROTECTED] wrote:

> hi if I have an array
>
> say x = [[2,2,0,0,1,1],
>         [1,1,0,0,1,1],
>         [1,1,0,0,1,1]]
> I basically want to group regions that are non zero like I want to get
> the coordinates of non zero regions..as (x1,y1,x2,y2)
> [(0,0,2,1),(0,4,2,5)] which show the top left(x1,y1) and bottom
> right(x2,y2) corners of each group.hope i am clear.

given your definitions, neither (0, 0, 2, 1) nor (0, 4, 2, 5) are clusters
in your data.  assuming that your description is wrong but your data is
correct, and your clusters are always this simple, here's a snippet that
does what I think you want:

x = [[2,2,0,0,1,1],
     [1,1,0,0,1,1],
     [1,1,0,0,1,1]]

# http://www.pythonware.com/products/pil/
import Image

h = len(x)
w = len(x[0])

data = []
for row in x:
    data.extend(row)

im = Image.new("L", (w, h), None)
im.putdata(data)

def runlength(x):
    out = []
    u = 0
    for i, v in enumerate(x):
        if v:
            if not u:
                lo = i
        elif u:
            out.append((lo, i))
        u = v
    if u: out.append((lo, i+1))
    return out

xx, yy = im.getprojection()

for y in runlength(yy):
    y0, y1 = y
    for x in runlength(xx):
        x0, x1 = x
        print (y0, x0, y1-1, x1-1)

</F> 



-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to