Inshu 0 Newbie Poster

I have this programme to discuss and I think its a challenging one.. Here I have a yml file which contains the data for an image. The image has x,y,z values and intensity data which is stored in this yml file. I have used opencv to load the data and its working fine with masking.. but I am having problems in dynamically appending the masks created.. Here is the code I made for solving the problem :

import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin

mask_size = 9
mask_size2 = mask_size / 2

f = open("Classified_Image1.txt", "w")


def distance(centre, point):
    ''' To find out the distance between centre and the point '''
    dist = sqrt(
            ((centre[0]-point[0])**2) +
            ((centre[1]-point[1])**2) +
            ((centre[2]-point[2])**2)
            )
    return dist 


def CalcCentre(points): # Calculates centre for a given set of points
    centre = array([0,0,0])
    count = 0
    for p in points:
        centre = add(centre, array(p[:3]))
        count += 1
    centre = dot(1./count, centre)
    print centre
    return centre


def addrow(data, points, x, y, ix , iy, dy  ):# adds row to the mask
    iy = y + dy
    for dx in xrange(-mask_size2 , mask_size2 + 2):
        ix = x + dx 
    rowpoints = addpoints(data, points, iy, ix)
    return rowpoints

def addcolumn(data, points, x, y, ix , iy , dx ):# adds column to the mask 
    ix = x + dx 
    for dy in xrange(-mask_size2-1 , mask_size2 + 1): 
        iy = y + dy 

    columnpoints = addpoints(data, points, iy, ix)
    return columnpoints

def addpoints (data, points, iy, ix): # adds a list of relevant points 
    if 0 < ix < data.width and 0 < iy < data.height:
        pnt = data[iy, ix]
        if pnt != (0.0, 0.0, 0.0):
            print ix, iy
            print pnt
            points.append(pnt)

    return points

def CreateMask(data, y, x):
    radius = 0.3
    points = []
    for dy in xrange(-mask_size2, mask_size2 + 1):
        ''' Masking the data '''
        for dx in xrange(-mask_size2, mask_size2 + 1):
            ix, iy = x + dx, y + dy
            points = addpoints(data, points, iy , ix )


    if len(points) > 3:
        centre = CalcCentre(points)
        distances = []
        for point in points :
            dist = distance(centre, point)
            distances.append(dist)
        distancemax = max(distances)
        print distancemax 

        if distancemax  < radius:
            ''' Dynamic Part of the Programme'''
            while dist < radius: # Going into infinite loop .. why ?
                #if ix != 299:
                p = addrow(data, points, x, y, ix , iy , dy  )
                #else :
                q = addcolumn(data, points, x, y, ix , iy , dx )
                dist = distance(centre, point) # While should not go in infinite
                                                #loop as dist is changing here
                print dist # To check distance is changing or not..
        print len(p),  p
        print len(q),  q
        points = p + q        
        points = list(set(points))  # To remove duplicate points in the list
        print len(points), points




def AccessData(data):
    for y in range(0, data.height):
        for x in range(0, data.width):
            CreateMask(data, y, x)

##            if data[y, x] != (0.0, 0.0, 0.0):
##                print data[y, x]

if __name__ == "__main__":
    data = cv.Load("Z:/data/xyz_00000_300.yml")
    print "data loaded"
    AccessData(data)

Feel free to suggest alternative methods/ideas to solve this problem.

Thanks in advance.