【问题标题】:Efficient masking of an np.array to cull locations of bad pixels (Python 2.7)有效屏蔽 np.array 以剔除坏像素的位置(Python 2.7)
【发布时间】:2017-10-26 08:00:27
【问题描述】:

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

【问题讨论】:

    标签: python arrays sorting numpy culling


    【解决方案1】:

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

    因此,实现将是 -

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

    【讨论】:

      猜你喜欢
      • 2013-02-11
      • 1970-01-01
      • 1970-01-01
      • 2017-08-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多