13
$\begingroup$

Color fill, the ability to apply a desired color to an enclosed area, is one of the basic functions provided by most graphic applications. Is there a similar function provided in Mathematica? For example, how to obtain a red, green, blue and yellow horses silhouette from the picture below in Mathematica?

im = Import["https://i.sstatic.net/ZDeYq.jpg"]
ReplacePixelValue[im, {924, 221} -> Blue]

ReplacePixelValue seems only work on one pixel only.

Colorize seems provide no controls on obtaining a specific color.

ColorReplace seems only work on a bundle of the same color all together .

enter image description here

$\endgroup$
0

1 Answer 1

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

Mathematica graphics

$\endgroup$
3
  • $\begingroup$ Thank you so much. It seems there is a strange behaviour behind MorphologicalComponents, So I raised another question about it. $\endgroup$
    – Putterboy
    Commented Sep 11, 2014 at 7:57
  • $\begingroup$ Is this method only good for binarized pictures? $\endgroup$
    – Putterboy
    Commented Sep 11, 2014 at 10:30
  • $\begingroup$ @Putterboy Not at all: MorphologicalComponents[image,t] treats values above t as foreground. $\endgroup$ Commented Sep 12, 2014 at 19:16

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.