Change all shades of particular colour using a specific RGB

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
alangbuchanan
Posts: 2
Joined: 2011-09-14T06:24:09-07:00
Authentication code: 8675308

Change all shades of particular colour using a specific RGB

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change all shades of particular colour using a specific

Post 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.
Last edited by fmw42 on 2011-09-14T20:17:18-07:00, edited 1 time in total.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Change all shades of particular colour using a specific

Post 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
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
alangbuchanan
Posts: 2
Joined: 2011-09-14T06:24:09-07:00
Authentication code: 8675308

Re: Change all shades of particular colour using a specific

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change all shades of particular colour using a specific

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change all shades of particular colour using a specific

Post 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
sujan.dasmahapatra
Posts: 44
Joined: 2012-02-18T21:42:01-07:00
Authentication code: 8675308

Re: Change all shades of particular colour using a specific

Post 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
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Change all shades of particular colour using a specific

Post 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.
Post Reply