Page 1 of 1
Change all shades of particular colour using a specific RGB
Posted: 2011-09-14T06:40:06-07:00
by alangbuchanan
Hello there,
I'm trying to create an application that will take an image and adjust all shades of a particular colour (red for example) to a corresponding shade of another colour (like blue). I have successfully done this using either -recolor or -color-matrix but in both cases I can't work out how to programatically calculate the transformation matrix values for a given target colour.
Say for example I have an image of a man wearing a red t-shirt. I can convert the image so that it looks like he is wearing a blue t-shirt using:
Code: Select all
convert C:\test\red.png -recolor "0 1 0 0 1 0 1 0 0" C:\test\blue.png
Or
Code: Select all
convert C:\test\red.png -color-matrix ^
"0.0 1.0 0.0 0.0, 0.0, ^
0.0 1.0 0.0 0.0, 0.0, ^
1.0 0.0 0.0 0.0, 0.0, ^
0.0 0.0 0.0 1.0, 0.0, ^
0.0 0.0 0.0 0.0, 1.0" C:\test\blue.png
That works really nicely, but how do I calculate the values to put into the matrix other than using trial and error? I have RGB values for all my colours defined in a database and I'd like to be able to automatically generate the required matrices using these if possible. I've spent a lot of time researching this on-line and reading up about inversion matrices and such like but am failing to get my head around it.
Perhaps I'm barking up the wrong tree with this method and there is another way (or it's impossible!)? Any help greatly appreciated.
I am using IM version 6.7.1-8 Q16 with Windows 7 command line.
Many thanks,
Alan
Re: Change all shades of particular colour using a specific
Posted: 2011-09-14T09:45:05-07:00
by fmw42
All you really need to do is convert to HSB, threshold on the H value you want to change to make a mask, then modulate the Hue in the whole image using -modulate and then use the mask to blend the two images so that only your hue value changes without changing the hue of the other colors.
See my bash script, huemap. Sorry I don't know how to translate that to windows.
Re: Change all shades of particular colour using a specific
Posted: 2011-09-14T19:56:20-07:00
by anthony
Each line is a linear formula
new_red = a*red + b*green +c*blue + d*alpha + e
making the first line of the matrix
a, b, c, d, e
The next line will work out the green, and so on.
That is it! The 5th line (if given) is a dummy to make the matrix a square, it is not used.
If given it is all zeros except the last which should be a value of 1.0 (for completeness)
UPDATE: After looking at the implementation I was wrong. You need 6 values to 'add' a constant.
The channels are Red, Green, Blue, Black, Alpha, Constant
The Black row/column is not applied unless the image is a CMYK image, but needs to be present in the array is you want to include alpha or constants to the martix math.
Note for just RGB, you can use a smaller array, such as a 3x3 array. This is overlayed on a 6x6 identity matrix. But that will not give you a constant part.
Making a tee-shirt change in a man may not work too well as skin color will be effected unless a mask is used.
An alturnative to using a matrix, is a -module to rotate the HUE in one of the hue colorspaces.
See Chroma Key Masking -- Modifying by areas of specific color
http://www.imagemagick.org/Usage/photos/#chroma_key
Re: Change all shades of particular colour using a specific
Posted: 2011-09-16T03:54:24-07:00
by alangbuchanan
Thank you both for your replies. Typically I've now been sidetracked onto something else but I shall try to make time to digest what you've said and perhaps try the hue modulation with a mask.
Thanks,
Alan
Re: Change all shades of particular colour using a specific
Posted: 2011-09-16T11:03:52-07:00
by fmw42
Actually the mask is not needed, though that might be another approach. See the description of my huemap, which converts to HSL, then creates a modified gradient to map the hue channel values, then recombines the channels and converts back to RGB
see
http://www.fmwconcepts.com/imagemagick/huemap/index.php
Re: Change all shades of particular colour using a specific
Posted: 2011-09-16T17:46:35-07:00
by fmw42
The hue approach will change all colors of that hue (all shades of a color), not just one specific RGB value to another. So I am not sure if that is what you want. Also -recolor has been deprecated in more current releases and is called -color-matrix. It has up to 6x6 matrix. see
http://www.imagemagick.org/script/comma ... lor-matrix
also see the recent discussion at
viewtopic.php?f=1&t=19491
Re: Change all shades of particular colour using a specific
Posted: 2012-02-18T21:45:07-07:00
by sujan.dasmahapatra
how can i convert all shades of white to red in an image. please give me some hints i am trying like this
Code: Select all
convert shirt0.jpg -recolor '1 0 0 0 0 0 1 1 1' shirtmod01.jpg
Re: Change all shades of particular colour using a specific
Posted: 2012-02-18T23:22:11-07:00
by fmw42
sujan.dasmahapatra wrote:how can i convert all shades of white to red in an image. please give me some hints i am trying like this
Code: Select all
convert shirt0.jpg -recolor '1 0 0 0 0 0 1 1 1' shirtmod01.jpg
It is best to start a new topic and not tack onto some old but not the same issue.