Finding nearest neighbor pixel from a binary mask

172 views Asked by At

I have a matrix and a binary mask. For example: Matrix is

array([[ 1,  2,  3,  4],
       [ 5,  6,  7,  8],
       [ 9, 10, 11, 12],
       [13, 14, 15, 16]])

And binary mask is

array([[1, 1, 1, 1],
       [1, 1, 1, 1],
       [1, 0, 0, 1],
       [1, 0, 0, 1]])

I want to create a new matrix that has original matrix's entries in places where the binary mask is 0, and in nonzero places it should choose the closest element from the zero region. So the required output matrix in the example above is:

array([[10, 10, 11, 11],
       [10, 10, 11, 11],
       [10, 10, 11, 11],
       [14, 14, 15, 15]]).

Using opencv, I can obtain a matrix of distances for the binary matrix, via the command:

cv2.distanceTransformWithLabels(binary.astype('uint8'),cv2.DIST_L2 ,cv2.DIST_MASK_PRECISE)

The output of the command is

(array([[2.1968994, 2.       , 2.       , 2.1968994],
        [1.3999939, 1.       , 1.       , 1.3999939],
        [1.       , 0.       , 0.       , 1.       ],
        [1.       , 0.       , 0.       , 1.       ]], dtype=float32),
 array([[1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1]], dtype=int32))

However, it does not return the indices of closest element, only the distances from it (as can be seen in the example above). Any idea how to achieve the desired output using opencv or any other python package?

0

There are 0 answers