0

I have binary images, and I would like to know if a given dark (0) pixel is inside a closed region (the pixel is surrounded by a closed line of bright pixels) or open region (there is a path of dark pixels from this target pixel to the boundary).

For instance, in the image closed_region the pixel at (200,200) is inside a closed region. In the other image open_region, the pixel at (200,200) is in an open region.

I used the watershed algorithm of scipy with markers on the target pixel and a boundary dark pixel, which runs in ~10ms (order of magnitude). But I have to repeat this process multiple time in my project, and I wanted to know if there are faster algorithms to do this, or other methods to adress the problem.

I am using Python, so I thought about coding a "truncated" watershed by just exploring the neighbours of the target pixel and see if we can reach the boundary but I doubt it will be faster.

EDIT

I modified the images so that you can load them as png. I work with the binary image:

img = cv.imread('closed_region.png')
img = cv.cvtColor(img, cv.COLOR_RGB2GRAY) # color to gray
_, img = cv.threshold(img,10,1,0) # binary img

and I currently use the following to see if a target pixel (200,200) is in an open (same as (0,0) here) or closed region.

from scipy.ndimage import watershed_ift
markers = np.zeros(img.shape, dtype=np.int8)
markers[200,200] = 2
markers[0,0] = 1
water = watershed_ift(img, markers)
4
  • 1
    source picture please. minimal reproducible example Commented Feb 4 at 16:18
  • Sorry for that, I edited the question with the png images. I saw the opencv implementation was a bit faster than the scipy one, but I still think that watershed does "too much" for my goal... Commented Feb 5 at 10:17
  • is this to say those are source data, i.e. not the result of any processing on any data preceding them? Commented Feb 5 at 11:38
  • They are created from (longitude, latitude) data (roads on a map), except from the conversion "coordinates -> array" there are no other pre-processing. The second ("open") image is here a filtering of the 1st one. If you need context, I would like to see if there is a path that lies inside a given "contour shape" i.e shape with a hole (that I use to filter the map). So I first filter roads inside the shape, and then see if it forms a continuous path around the center of the shape. Commented Feb 5 at 12:19

1 Answer 1

1

I found the answer to the problem: opencv "cv.floodFill" function does exactly what I want, and is muche faster than scipy watershed (by a factor of ~100).

Sign up to request clarification or add additional context in comments.

1 Comment

This was going to be my answer: Flood fill from the edges in to find all points connected to the boundary. Then query if your point is in that set. Should be very fast. There are other techniques that also could be fast, for example using connected component analysis (labeling) of the background regions. But the flood fill is the natural solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.