Shan
Shan

Reputation: 19243

How to remove image components based on the mask in opencv python?

I have two binary images. Lets say image A and B.

Both contains object which have pixel value 1 and background has pixel value 0.

Image B is a mask image.

I want to keep components in 'Image A' which are touching objects of 'Image B' even by one pixel.

What I have tried

I have tried numpy.logical_and it gives me the intersection.

Now I have to loop all the components of 'Image A' to check all the pixels if the intersected pixel lies in any of the components of image A and its very slow.

What I am looking for

Is there any library function in opencv which does it all?

Or any python lib which is useful

Could anybody help me with the sample code or something.

Thanks

Upvotes: 0

Views: 2199

Answers (1)

M4rtini
M4rtini

Reputation: 13539

from skimage.morphology import label
labels_a = label(a, 4, 0)  #CCL analysis with 4 way connectivity and 0 as background
output = np.zeros_like(a)  #output as zeros with same shape as a
intersecting_pixels_indicies = np.nonzero(a&b) #getting the indicies where a&b is true(1)

objectIDs = set()
for i,j in zip(*intersecting_pixels_indicies):
    objectIDs.add(labels_a[i,j]) #get the ID's of the indicies.

for i in range(output.shape[0]):
    for j in range(output.shape[1]):
        if output[i,j] in objectIDs: #if this id is in or list. add the value of a to output.
            output[i,j] = a[i,j]

You could try this and see if it's fast enough, and if it¨s giving you the results you want.

First a connected component analysis is done on the array, with 4 way connectivity. All pixels that is connected to the same "object" will get the same value. So if you check the value of a cell, you will get the ID of the related object. or -1 if background.
see: http://en.wikipedia.org/wiki/Connected-component_labeling for a more indepth explenation of connected components labeling.

Then the indicies of where a and b is 1 is extracted by np.nonzero(a&b). Then the program will loop over all these indicies and check the corresponding ID of the labeled array. Adding it to a set of Objects that we are interested it.
We now have the ID of all the objects we want to extract. So we simply loop over the array, for each cell we check if the value is in our list of interesting objectID's. If it is, we add the value of a to the output, if not we do nothing(or we could set it to zero, but the output is instantiated with all zeros)

Upvotes: 3

Related Questions