Consider the following image:

Red ball in a non-red environment

How can I change all red colors in this image into (for example) blue.


Here's a version using Manipulate with a Locator to pick the colour to replace. There is also a tolerance control which determines how wide a range of hues to replace.




{{to,Blue,"Change to"},Blue},

enter image description here

  • 1
    $\begingroup$ You could also add a polygon defined by other locators, to define the mask of impact... $\endgroup$ – P. Fonseca Apr 27 '12 at 10:11
  • 2
    $\begingroup$ Or use another locator as a brush $\endgroup$ – Simon Woods Apr 27 '12 at 10:48
  • 4
    $\begingroup$ ...and I see a full image-processing graphical suit a la Photoshop implemented in Mathematica coming up soon... $\endgroup$ – István Zachar Apr 27 '12 at 13:25
  • 3
    $\begingroup$ @Simon Woods: nice approach! I like that one but still: see the two people on the left hand side of the ball? The went blue too. How one avoid that? $\endgroup$ – John Apr 27 '12 at 18:44

I wanted to change only the color of the ball, leaving all other red objects untouched:

getReds[x_Image] := First@ColorSeparate[x, "Hue"]
isolateSphere[x_Image] := SelectComponents[Binarize[getReds[x], .9], Large]
makeMask[x_Image] := Image@Graphics[ Disk @@ (1 /. 
                        ComponentMeasurements[isolateSphere[x], {"Centroid","BoundingDiskRadius"}]), 
                        {PlotRange -> Thread[{1, #}], ImageSize -> #} &@ImageDimensions@x]
getAreaToChange[x_Image] := ImageMultiply[i, ColorNegate@makeMask[x]]
shiftColors[x_Image] := Image[ImageData[getAreaToChange[x]] /. 
                                                      p: {r_, g_, b_} /; r > .3 :> RotateLeft[p, 1]]
finishIt[x_Image] :=  ImageAdd[ImageMultiply[x, makeMask[x]], ColorConvert[shiftColors[x], "RGB"]]

{#, getReds@#, isolateSphere@#, makeMask@#, getAreaToChange@#, shiftColors@#, finishIt@#} &

Mathematica graphics

Comparing side to side:

Mathematica graphics

  • $\begingroup$ Nice. What should one change to change the from and to colors? Or it only works for red and blue? $\endgroup$ – Rojo Apr 26 '12 at 18:30
  • $\begingroup$ +1 Nice. What would you have done if there were other instances of red in the picture that you did not want to alter? $\endgroup$ – DavidC Apr 26 '12 at 18:51
  • 2
    $\begingroup$ @Rojo In fact Red is the most difficult case, since it is at the beginning and at the end of the Hue[] table. Try Graphics[Raster[Table[{h, s, 1}, {s, 0, 1, .01}, {h, 0, 1, .01}], ColorFunction -> Hue]] $\endgroup$ – Dr. belisarius Apr 26 '12 at 19:19
  • 6
    $\begingroup$ @DavidCarraher That happens usually in image processing. Things like "reddish" and "the big thingie in the middle" are not as easy to program as CylindricalDecomposition[] :) $\endgroup$ – Dr. belisarius Apr 26 '12 at 21:28
  • 2
    $\begingroup$ Congrats on your (soon to be awarded) populist badge! :) $\endgroup$ – rm -rf Apr 30 '12 at 2:30

A one-liner:

i = Import["http://i.stack.imgur.com/Qr7Tx.jpg"];

Image[ImageData[i] /. {r_, g_, b_} /; r > g && r > b -> {b, g, r}]

enter image description here

  • 2
    $\begingroup$ ImageApply[RotateLeft, i] have a same effect. :) $\endgroup$ – yode Jun 28 '16 at 8:59

First Method

If you just want to get different color of your ball,but don't want to name the color.I will recommend you this solution,which makes your life easier.In the meantime,it will change other place's color,not just the ball,like CSP's answer

img = Import["http://i.stack.imgur.com/Qr7Tx.jpg"];
Select[ColorCombine /@ Tuples[ColorSeparate[img], {3}], 
 ImageMeasurements[ColorDistance[ColorConvert[#, "Grayscale"], #], 
    "Total"] > 8000 &]

You get 20 different color ball by a line code.

Second Method

If you just want to change your ball's color with a specified color and want maintain other place,like Dr. belisarius' answer,I make a custom function for you to do this

ChangeColor[img_, color_] := 
 Module[{mask = 
         ColorCombine /@ Permutations[ColorSeparate[img]]][[1, 2]]]], 
     9]}, ImageAdd[ImageSubtract[img, ImageMultiply[img, mask]], 
       ColorSeparate[ColorConvert[ImageMultiply[img, mask], "HSB"]]], 
         ConstantImage[color, ImageDimensions[img], 
          ColorSpace -> "HSB"], mask]]]], "HSB"], "RGB"]]]


ChangeColor[img, #] & /@ RandomColor[5]

  • $\begingroup$ It seems that the background change in the first solution, so I prefer the second one $\endgroup$ – Wjx Jun 28 '16 at 9:07
  • $\begingroup$ @Wjx Thanks. :) $\endgroup$ – yode Jun 28 '16 at 9:10
  • 1
    $\begingroup$ ChanVeseBinarize[] looks to be the key component here. $\endgroup$ – J. M.'s torpor Jun 28 '16 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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