1

我有这个程序要讨论,我认为它具有挑战性。这里我有一个 yml 文件,其中包含图像的数据。图像具有存储在此 yml 文件中的 x、y、z 值和强度数据。我已经使用 opencv 加载数据并且它在掩码中工作正常..但是我在动态附加创建的掩码时遇到问题..这是我为解决问题而编写的代码:

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 ):# adds row to the mask
    iy = y + 1
    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 ):# adds column to the mask 
    ix = x + 1
    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 ?
                p = addrow(data, points, x, y, ix , iy  )
                q = addcolumn(data, points, x, y, ix , iy )
                dist = distance(centre, point) # While should not go in infinite
                                                #loop as dist is changing here
                print dist 
        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 ComputeClasses(data):
    for y in range(0, data.height):
        for x in range(0, data.width):
            CreateMask(data, y, x)


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

随意提出解决此问题的替代方法/想法。

提前致谢。

4

0 回答 0