Extracting a mask of the 'pink areas.
I figured the best idea was to extract 'pinks' from form HSL space. A Technique called
Chroma Key (also known as Green or Blue Screen.
First attempt...
Code: Select all
convert shirt.jpg -colorspace HSL -channel Hue -separate +channel shirt_hue.jpg


Hmmm two problems... First Pink is near red which is at the division where Hue 'rolls over', but I figured I could use -modulate to adjust the hue away from that 'discontinuity' in the hue. So a 1/3 hue roll (33.3 modulate value), and a color lookup of the pink area gets me the 'pink' hue color of gray64 in the image.
The other problem is the gray background!!!!! Gray is has very little hue, so I need to remove any areas with little to no saturation from my final mask, or I'll be changing things in the background.
This is not needed if I limit changes to hue rolls, which does not effect unsaturated colors. But for completeness I will remove that too...
Code: Select all
convert shirt.jpg -modulate 100,100,33.3 -colorspace HSL \
-channel Hue,Saturation -separate +channel \
\( -clone 0 -background none -fuzz 5% +transparent grey64 \) \
\( -clone 1 -background none -fuzz 10% -transparent black \) \
-delete 0,1 -alpha extract -compose multiply -composite \
shirt_mask.png



that just leaves a number of small isolated 'specks' that can be removed with some morphology
Code: Select all
convert shirt_mask.png -morphology Smooth Square shirt_mask_clean.png
Now you have a mask you can use it to limit (clip) the areas of change when you (somehow) recolor your image. In this case I'll just shift the hues using modulate
http://www.imagemagick.org/Usage/color_mods/#modulate
WARNING: I use a 'clip-mask' in this case... Something I have not done before. However Clip masks are masks of areas that are NOT to change. As such they need to be negated, thus the complication in this example.
Code: Select all
convert shirt_mask_clean.png -negate miff:- | \
convert shirt.jpg -clip-mask miff:- -modulate 100,100,25 shirt_blue.jpg



Yes there is a slight 'pink' border, and one area of skin that turned blue, (and is squarish due to JPEG noise). The Mask needs a little more work. But it is not bad.
The real problem is that using that is so close to skin color is just asking for trouble! Why do you think people use 'green' and 'blue' screens!
ASIDE: Also I would NOT use JPEG as source or working images. Only use it for your final images only!
The above will be used (with your permission for use of the shirt) in IM Examples, Photos, Chroma Keying.
now online
http://www.imagemagick.org/Usage/photos/#chroma_key