remove a contour from image using skimage

I want to remove some contour from an image but I don't know how to achieve it using skimage? I do something like this in OpenCV using drawContour but I can't find the equivalent in skimage.

Assume I have a simple image like:

0 0 0 0 0 0 0 0

0 0 1 1 1 1 0 0

0 0 1 0 0 1 0 0

0 0 1 1 1 1 0 0

0 0 0 0 0 0 0 0

that has only one connected component.

I need to remove it by masking it.

The final result will be a 8 * 5 zero matrix!

a = '''0 0 0 0 0 0 0 0                                             
0 0 1 1 1 1 0 0
0 0 1 0 0 1 0 0
0 0 1 1 1 1 0 0
0 0 0 0 0 0 0 0'''
np.array([int(i) for i in a.split()], dtype=bool).reshape(5, 8)
cc = measure.regionprops(measure.label(a))[0]
# here is what I do for removing cc

What should I do to remove cc connected component using skimage?

Upvotes: 1

Views: 2334

Answers (1)

In this case, you need to turn the image to a binary image (as you have) and then follow these steps:

1 - Fill holes.

2 - recognize contour.

3 - create a black mask (false or 0) using the recognized contours.

from skimage import measure, draw
from scipy import ndimage as ndi


# I use this function "ndi.distance_transform_edt(thresh)" to turn my image to a binary image
def remove_contours(binary_image):
    binary_image = ndi.binary_fill_holes(binary_image)
    contours= measure.find_contours(binary_image, 0.9, fully_connected='high',
                                     positive_orientation='low')
    #Fill contours with False. (it could be 0 as well)
    for n, contour in enumerate(contours):
        draw.set_color(binary_image , draw.polygon(contour[:, 0], contour[:, 1]), False)
    return binary_image

Upvotes: 1

Related Questions