有效掩蔽np.数组剔除坏像素的位置(Python2.7)

2024-06-25 23:19:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我想删除坐标和视差数组中坏像素的位置。因此,我写了一些代码,但它觉得有点迂回和有点太长的任务。代码背后的主要思想是,我希望删除包含视差值-17的所有数组项。我的2000x2000图像的像素坐标阵列也会发生同样的情况。 下面是我使用掩码和扁平数组的代码。(最后,我希望3个数组包含x,y和视差值,按相同的顺序排序,不包含坏像素的条目和坐标) 感谢您提供任何改进此代码的提示!在

#define 2000x2000 index arrays
xyIdx = np.mgrid[0:disp.shape[0],0:disp.shape[1]]
xIdx = xyIdx[1]
yIdx = xyIdx[0]

#flatten indice-arrays and the disparity-array  
xIdxFlat = xIdx.flatten()
yIdxFlat = yIdx.flatten()
dispFlat = disp.flatten()

#create mask with all values = 1 'true'
mask = np.ones(dispFlat.shape, dtype='bool')

#create false entrys in the mask wherever the minimum disparity or better 
#said a bad pixel is located
for x in range(0,len(dispFlat)):
    if  dispFlat[x] == -17:
        mask[x] = False 

#mask the arrays and remove the entries that belong to the bad pixels        
xCoords = np.zeros((xIdxFlat[mask].size), dtype='float64')
xCoords[:] = xIdxFlat[mask]
yCoords = np.zeros((yIdxFlat[mask].size), dtype='float64')
yCoords[:] = yIdxFlat[mask]
dispPoints = np.zeros((dispFlat[mask].size), dtype='float64')
dispPoints[:] = dispFlat[mask]

Tags: the代码npmask像素数组shapedtype
1条回答
网友
1楼 · 发布于 2024-06-25 23:19:06

创建一个有效的掩码!=-17。使用此掩码可以获得有效的行col index,即X-Y坐标。最后用掩码或行索引输入数组,col索引过滤后的数据数组。因此,你不需要做所有的扁平化业务。在

因此,实施将是-

mask = disp != -17
yCoords, xCoords = np.where(mask) # or np.nonzero(mask)
dispPoints = disp[yCoords, xCoords] # or disp[mask]

相关问题 更多 >