Search for an algorithm to find the border of a color region

I have a canvas with an image painted on it.

When the user clicks on the image, I need to find the area of ​​color that the user clicked on. An area is defined as a set of four-sided connected pixels with the same color as the pixel that was clicked.

I need an area in the shape that I could use to set the clipping path on the canvas so that I can fill the area with, say, a gradient, etc.

Are there any efficient algorithms for finding the boundary? Something more optimal than the algorithms for filling the bay (I do not need to fill, I just need to find a way around my region).

+6
source share
1 answer

I believe Moore’s environment tracking algorithm will do what you want. By definition, The Moore Neighborhood considers 8-connectedness, but you should be able to easily configure it for 4-connectedness. The regions you received are likely to be better if you test 8-connectivity, but your application may have special requirements.

Wikipedia has a good algorithm diagram here . I have worked with this in the past and have had great success - very fast.

+4
source

Source: https://habr.com/ru/post/913212/


All Articles