Changing color of an object in an image

Consider the following image:

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.

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

{h,s,b}=ColorSeparate[i,"HSB"];

colourchange[c_,from_,tol_,to_]:=Module[
{offset=Mod[c-from+0.5,1]-0.5},
If[Abs[offset]>tol,c,to]];

Manipulate[
ColorCombine[
{ImageApply[colourchange[#,ImageValue[h,pos],tol,ColorConvert[to,Hue][[1]]]&,h],s,b},
"HSB"],
{{to,Blue,"Change to"},Blue},
{{tol,-0.01,"Tolerance"},-0.01,0.5},
{{pos,{100,50}},Locator}]


-
You could also add a polygon defined by other locators, to define the mask of impact... – P. Fonseca Apr 27 '12 at 10:11
Or use another locator as a brush – Simon Woods Apr 27 '12 at 10:48
...and I see a full image-processing graphical suit a la Photoshop implemented in Mathematica coming up soon... – István Zachar Apr 27 '12 at 13:25
@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? – John Apr 27 '12 at 18:44

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

k = ColorSeparate@ColorConvert[i = Import["http://i.stack.imgur.com/Qr7Tx.jpg"], "HSB"]
Image@Replace[ImageData@k[[1]], {x_ /; ! .03 < x < .7 -> 1},2],
.99], 5], 5];

ccb = ColorCombine[{Image@
Replace[ImageData@#1, {x_ /; x > .7 -> x - 1/3, x_ /; x < .03 -> (1 - x) - 1/3},
2], #2, #3}, "HSB"] & @@ k1



-
Nice. What should one change to change the from and to colors? Or it only works for red and blue? – Rojo Apr 26 '12 at 18:30
+1 Nice. What would you have done if there were other instances of red in the picture that you did not want to alter? – David Carraher Apr 26 '12 at 18:51
@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]] – belisarius Apr 26 '12 at 19:19
@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[] :) – belisarius Apr 26 '12 at 21:28
Congrats on your (soon to be awarded) populist badge! :) – rm -rf Apr 30 '12 at 2:30
i = Import["http://i.stack.imgur.com/Qr7Tx.jpg"];