Ask Your Question

how to detect discontinuity in object boundaries

asked 2020-02-16 23:08:08 -0500

zelda gravatar image

i have shapes in image having background. i want to remove background from it for that i made logic which works ok for shapes having full boundary around them but cant work if their is discontinuity or leakage in boundary . so am wondering how can we find discontinuity in shape boundary so that my code not erase pixel it that area


image with full boundary .

image description

so when i have image like this which have no leakage in boundary i run flood fill algo on image which return me all green pixel locations and i turn their alpha to zero due to which i am left with these two shapes and green background is removed.

image description

but sometime i have images in which shapes boundary is not complete it have somewhere discontinuity in it , so am wondering how i can detect that shape in image outer boundary is not complete so that i not use flood fill method to remove background because i do not want to remove anything inside shape , alternatively i remove background by looping though pixels from left to right , right to left , top to bottom , bottom to top and check for pixel color if not equal to red then remove it else skip due to which background is remove from all most all image from all sides. but for this alternative method i need to know that their is discontinuity in shape boundary.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2020-02-17 08:26:10 -0500

kbarni gravatar image

updated 2020-02-18 10:00:56 -0500

It seems that you don't need to detect the "broken contours", rather avoid to fill the inside of the those shapes. To achieve this, probably the best solution is to apply a morphological closing on the shape (morphologyEx function). This will close the gaps in the shape and you can apply the flood fill to remove the background.

If you want to detect the narrow gaps, probably a black hat operation (also morphologyEx) will give you these points.

[EDIT] Here are the results of the closing operation on the second image (binarized) using a 7x7 pixel rectangular kernel. The gaps were closed:

image description

Here, you can use floodfill to erase the background. Then, you can copy the alpha mask to the original image if you want to keep the original shapes.

edit flag offensive delete link more


thanks i will try it , not sure why they give so complex names morphologyEX , lol

zelda gravatar imagezelda ( 2020-02-18 06:09:22 -0500 )edit

These are the extended morphology operators, which mean that several morphology operations are performed in the function (e.g. the closing is a dilatation followed by an erosion).

kbarni gravatar imagekbarni ( 2020-02-18 09:45:02 -0500 )edit

OOPS! Sorry, I deleted answer. I thought it was js or c++ but not python.

supra56 gravatar imagesupra56 ( 2020-02-20 20:20:52 -0500 )edit

@surpa56 , you can add python answer too , its just about logic can be replicated to c++ and js

zelda gravatar imagezelda ( 2020-02-21 03:07:56 -0500 )edit

You can use he code from this tutorial. The only changes I made was the kernel size and I added a binarization step at the beginning (threshold on the red channel).

kbarni gravatar imagekbarni ( 2020-02-21 03:25:20 -0500 )edit

Question Tools

1 follower


Asked: 2020-02-16 23:04:35 -0500

Seen: 524 times

Last updated: Feb 18 '20