pythonarrayssortingnumpyculling

Efficient masking of an np.array to cull locations of bad pixels (Python 2.7)


I want to remove the locations of bad pixels in my coordinate- and disparity-arrays. Therefore I wrote some code but it feels a bit circuitous and a little too long for the task. The main idea behind the code is that I want all array entries removed that contain a disparity value of -17. The same should happen for my pixel coordinate arrays of my 2000x2000 image. Here is my code using a mask and flattened arrays. ( in the end I want 3 arrays containing x, y and the disparity value sorted in the same order not containing the entries and coordinates of the bad pixels) Thanks for any hints that improve this code!

#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]

Solution

  • Create a mask of valid ones !=-17. Use this mask to get the valid row, col indices, which would be the X-Y coordinates. Finally index into the input array with the mask or the row, col indices for the filtered data array. Thus, you won't need to do all of that flattening business.

    Hence, the implementation would be -

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