1 | initial version |
Ok, can you guarantee they are above a certain size? Can you guarantee that they are distinct and well separated, like they are in that image? Then it's easy.
If you have a red and a blue touching, you should do it twice, to separate the two when you do connected components.
Note: If you do connectedcomponentswithstats, you get the bounding box and area as a return value and you don't even need to look at the map.
If the assumptions don't hold, it gets more complicated, but based on your sample image, this should work. If you need it to run really fast, there are possibly some shortcuts to take. But this is a good start, see if it works.