# Why ColorReplace does not follow the rules specified?

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["http://i.stack.imgur.com/ZDeYq.jpg"];
im3 = Colorize@ MorphologicalComponents@im2
colors = DominantColors[im3, 5]


Now trying to change the colors accordingly,

ColorReplace[im3, {
colors[[2]] -> Red,
colors[[3]] -> Blue,
colors[[4]] -> Green,
colors[[5]] -> Yellow
}]


One of the changes did not follow the rules specified:

The default tolerance (distance) of ColorReplace is such that first and third horses are considered similar:

ColorReplace[im3, colors[[2]] -> Red]


Use a smaller value for the distance parameter:

ColorReplace[im3, {
colors[[2]] -> Red,
colors[[3]] -> Blue,
colors[[4]] -> Green,
colors[[5]] -> Yellow
},
0.01
]


What you want to do can be done way easier:

i = Binarize@Import["http://i.stack.imgur.com/ZDeYq.jpg"];
Colorize[MorphologicalComponents@i,
ColorRules -> {1 -> Red, 2 -> Blue, 3 -> Green, 4 -> Yellow}]


• Is that the color order different from the rule your specified? – Putterboy Sep 11 '14 at 8:14
• The colors are in the order I specified. Keep in mind that the ordering depends on the order of components provided by MorphologicalComponents – paw Sep 11 '14 at 8:18