I tried to use `ColorReplace` to change the colors obtained from `MorphologicalComponents`. However some of the changes didn't follow the rules specified, Is there any reasons? im2 = Binarize@Import["https://i.sstatic.net/ZDeYq.jpg"]; im3 = Colorize@ MorphologicalComponents@im2 colors = DominantColors[im3, 5] ![enter image description here][1] Now trying to change the colors accordingly, ColorReplace[im3, { colors[[2]] -> Red, colors[[3]] -> Blue, colors[[4]] -> Green, colors[[5]] -> Yellow }] Two of the changes did not follow the rules specified: ![enter image description here][2] [1]: https://i.sstatic.net/5gZkM.gif [2]: https://i.sstatic.net/aNxL2.gif