有效屏蔽 np.array 以剔除坏像素的位置 (Python 2.7)



我想删除坐标和视差数组中坏像素的位置。因此,我写了一些代码,但感觉有点迂回,而且对于任务来说有点太长了。代码背后的主要思想是,我希望删除所有包含视差值 -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]

创建有效掩码!=-17 。使用此掩码获取有效的行、列索引,这将是 X-Y 坐标。最后索引到带有掩码或行的输入数组中,col 索引用于过滤后的数据数组。因此,您无需执行所有这些扁平化业务。

因此,实施将是 -

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

最新更新