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.stack.imgur.com/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


1 Answer 1

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

Mathematica graphics

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

Your Answer

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

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