Associate Color ?

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Associate Color ?

Post by sanjeevk »

Hi,
I want to do following with an image.
1. First pick one color ( Green) from color. But in this image there are some associated color with green like ( Associate Green) .

2. Suppose I am changing dark ( Green) with Blue.
same time associated Green also change in light Blue.

It can possible. Please suggest on the same.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Associate Color ?

Post by fmw42 »

Best I can suggest is to separate the green channel. Then threshold it to select those green colors you want to use and make a binary mask. Then use -modulate on the whole image to shift the green towards blue. Then use the mask to combine the original with the shifted color image so that only the greens are changed.

Alternately, separate each channel and process each channel in such a way as to convert only green to blue (like replacing the blue channel with the the green channel or merging the two), then recombine all the channels.

can you provide an example image (before and after)?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Associate Color ?

Post by anthony »

I understand what you are wanting to do. Find all 'green' looking colors, and
replace them with a different color while preserving the bightness.

Unfortunately this is not easy.

If you could mask the 'green' areas' then you could do a 'Hue' modulation on just those areas.


In other words the solution will very much depend on the types of image you plan to process. An specific example image may help us help you!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Associate Color ?

Post by fmw42 »

can you post links to any examples? perhaps some further ideas will come from seeing them!

If you change greens to blues, then what do you want to happen with the already existing blues?
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Associate Color ?

Post by sanjeevk »

We are trying to achieve the following: Modify similar looking colors to another color which are similar looking.

- We have images with associated colors in them . Lets say an image has 10 colors and out of them 3 colors P1, P2 and P3 are of similar tone , so they are associated to each other like all in Purple are similar looking.
- Now we want to modify the color of Purple to lets say Yellow, but we want Purple P1 to change to Yellow Y1 and P2 to Y2 and P3 to Y3.
- The problem is we know the RGB values of P1, P2 and P3 and the RGB value of Y1 but not of Y2 and Y3. And we want to modify all Purples to Yellows by saying P1 to change to Y1 (and accordingly we will modify P2 and P3 to Y2 and Y3)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Associate Color ?

Post by fmw42 »

post an example image so others can test with it.

if you have so few colors, then you should be able to make a mask for the colors you want to change and then just use -modulate on the image to shift the Purples to Yellows, then composite that back with the original using the mask.

Here is a simple example, but it needs some tuning:

original image (shift blue to red)
Image


# separate HSL components (show Hue channel)
convert cyclops.png -colorspace HSL -separate cyclops_hsl.png
Image

# threshold range of grayscales that correspond to blue in the hue channel to make mask
convert cyclops_hsl-0.png -black-threshold 50% -white-threshold 60% \
-fill black -opaque white -fill white +opaque black cyclops_hsl-0_mask.png
Image

# use modulate on original image to shift all colors towards red
convert cyclops.png -modulate 100,100,0 cyclops_mod_100x100x0.png
Image

# use mask to composite original with red shifted version where it was blue to make it red
convert cyclops.png cyclops_mod_100x100x0.png cyclops_hsl-0_mask.png \
-compose over -composite cyclops_blue2red.png
Image
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Associate Color ?

Post by anthony »

# separate HSL components (show Hue channel)
convert cyclops.png -colorspace HSL -separate cyclops_hsl.png
To save JUST the hue channel add a -channel setting before -separate otherwise you get mutliple numbered images (filename is modified by IM).

Code: Select all

  convert cyclops.png -colorspace HSL -channel R -separate cyclops_hue.png
In the cyclops example you may like to mask the oppisite. That is mask out the
club rather than the rest of the image. Alturnativally you can use a -fuzz color replacement in either RGB, or HSL to better define the range of colors or hues you want to convert.

The masking is a form of segmentation. and segmenting the image first may also be used to generate definitive areas which you want to modify.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Associate Color ?

Post by anthony »

Another alternative.

Ignore your original image for the moment. and work on just what colors you want transformed. Generate a 'hald' color table image...
http://www.imagemagick.org/Usage/color/#hald-clut

Code: Select all

  convert hald: hald_color_table.png
The hald is a map of all the RGB colors. that is it is a 3-dimensional color cube arranges to fit in a 2-dimenation image. You can now convert say all purple colors
in the HALD to yellow, and smooth the change across the near-purple colors as well.

Put you effort into this, and you can then apply it to LOTS of images very quickly and produce a better 'make purples yellow' conversion.


And please let us know how you go!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Associate Color ?

Post by fmw42 »

if you just have a few colors in your image and have only a few to change, you can also use my script, mapcolors. You provide a list of FROM and TO colors and it will only change those colors in the image.
sanjeevk
Posts: 26
Joined: 2009-08-26T02:17:36-07:00
Authentication code: 8675309
Location: India
Contact:

Re: Associate Color ?

Post by sanjeevk »

Hi All,

After doing some R&D with imagemagick . I think we can resolve the associate color problem using following steps.

1. we generate a gradient of a color.
e.g. convert -size 15x100 gradient:#0300FF gradient.png
2. Now we segment images of associate color and combine them to get one image of all associated color.
e.g. If in an image we have following associate color.
1. #F9E900
2. #F7F700
3. #F4DD09
a --> we extract each image according to there color,
b --> combine these images to get new one which have only these three color ( e.g. associate.png).

3. Now we use -clut option to change the color of image.

e.g. convert associate.png gradient.png -clut newImage.png


Please suggest on the same.

Thanks,
Sanjeev Kumar
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Associate Color ?

Post by fmw42 »

can you show each command in your process and an example image after each step or at least a before and after result?
Post Reply