Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在图像中创建遮罩并使用Opencv在python中动态附加它们_Python_Dynamic_Image Processing_Opencv_Masking - Fatal编程技术网

在图像中创建遮罩并使用Opencv在python中动态附加它们

在图像中创建遮罩并使用Opencv在python中动态附加它们,python,dynamic,image-processing,opencv,masking,Python,Dynamic,Image Processing,Opencv,Masking,我有这个计划要讨论,我认为这是一个具有挑战性的计划。。这里我有一个yml文件,其中包含图像的数据。该图像具有存储在此yml文件中的x、y、z值和强度数据。我已经使用opencv加载了数据,它可以很好地进行掩蔽。。但是我在动态附加创建的掩码时遇到问题。。以下是我为解决此问题而编写的代码: import cv from math import floor, sqrt, ceil from numpy import array, dot, subtract, add, linalg as lin m

我有这个计划要讨论,我认为这是一个具有挑战性的计划。。这里我有一个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)
导入cv
从数学导入层,sqrt,ceil
从numpy导入数组,点,减,加,linalg作为lin
遮罩尺寸=9
遮罩尺寸2=遮罩尺寸/2
f=打开(“分类图像1.txt”,“w”)
def距离(中心、点):
''找出中心和点之间的距离''
dist=sqrt(
((中心[0]-点[0])**2)+
((中心[1]-点[1])**2)+
((中心[2]-点[2])**2)
)
返回区
def CalcCentre(点):#计算给定点集的中心
中心=数组([0,0,0])
计数=0
对于p in点:
中心=相加(中心,数组(p[:3]))
计数+=1
中心=点(1/计数,中心)
印刷中心
返回中心
def addrow(数据、点、x、y、ix、iy):#将行添加到掩码
iy=y+1
对于X范围内的dx(-mask_size2,mask_size2+2):
ix=x+dx
行点=添加点(数据、点、iy、ix)
返回点
def addcolumn(数据、点、x、y、ix、iy):#将列添加到掩码
ix=x+1
对于X范围内的dy(-mask_size2-1,mask_size2+1):
iy=y+dy
columnpoints=添加点(数据、点、iy、ix)
返回点
def addpoints(数据、点、iy、ix):#添加相关点的列表
如果03:
中心=计算中心(点)
距离=[]
对于点到点:
距离=距离(中心、点)
距离。追加(距离)
距离最大=最大(距离)
打印距离最大值
如果distancemax<半径:''程序的动态部分''
#距离<半径:#进入无限循环。。为什么?
p=addrow(数据、点、x、y、ix、iy)
q=添加列(数据、点、x、y、ix、iy)
dist=距离(中心、点)#而不应无限远
#循环,因为这里的距离正在改变
打印区
打印透镜(p),p
打印透镜(q),q
点数=p+q
点=列表(设置(点))#删除列表中的重复点
打印透镜(点),点
def计算类(数据):
对于范围内的y(0,数据高度):
对于范围内的x(0,数据宽度):
CreateMask(数据,y,x)
如果名称=“\uuuuu main\uuuuuuuu”:
数据=恒载(“Z:/data/xyz_00000_300.yml”)
打印“已加载数据”
计算机类(数据)
请随意提出解决此问题的替代方法/想法


提前感谢。

问题是无限循环吗?如果既不修改
中心
也不修改
,为什么
距离
会改变?