1 | initial version |
The best way would be to modify the connected components code. You have to change the conditions it uses from a binary yes, to an equals. You're going to get huge numbers of components in a natural image, so you'll need to filter by size and maybe other things.
If you know the exact colors, it's much easier. Just use inRange to extract a map of each color, then run connected components normally.