2
$\begingroup$

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

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:

enter image description here

$\endgroup$

2 Answers 2

6
$\begingroup$

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

enter image description here

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

enter image description here

Use a smaller value for the distance parameter:

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

enter image description here

$\endgroup$
0
5
$\begingroup$

What you want to do can be done way easier:

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

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.