The floodFill function can also return a region as a return value. So, you can do one raster scan of each pixel: every time you reach an untouched pixel, color it in some color (black) and save the region of this region along with the coordinates of the pixel, continue until the entire image is covered.
In the end, you will have a set of core regions for one pixel in each region.
You need to restore a specific region that you can use floodFill again by painting this region in a specific color.
source share