Replace specific color of image

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?".
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

Replace specific color of image

Post by ashwini »

hi all,

I am trying to replace specific color of image with some other color using command line. I tried the following command for simple images
convert rose.jpg -fuzz 20% -fill yellow -opaque red rose.jpg

it works..but i have to specify the -fuzz , same command i tried for below image but the output image is not showing proper color i.e it doesn't consider the shading,border. i used this command

convert room.jpg -fuzz 15% -fill #84BE6A -opaque #d89060 room1.jpg

i know i am using very basic command for this.
Is it possible to do this using command line or any other way? I just started using Version: ImageMagick 6.7.1-0 2011-07-06 Q16 http://www.imagemagick.org

this is the source image
Image

thank you.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 »

You must quote rgb and hex colors. see http://www.imagemagick.org/script/color.php

try

convert room.jpg -fuzz 15% -fill "#84BE6A" -opaque "#d89060" room1.jpg

This is a global color change. My tests seem to indicate you are trying to change the wall to some green shade. But I don't think you will have much success. For this kind of thing, you need fairly uniform color and it needs to be considerably different from anything else.

If you want only a selected region, then you should try using -fuzz with either -floodfill or -draw "color ... floodfill"

see
http://www.imagemagick.org/script/comma ... #floodfill
http://www.imagemagick.org/Usage/draw/#color

An alternative is to manually create a mask of the region you want to recolor, process the whole image with your method above, and then use the original image, the processed image and the mask image to merge the color change only where desired. See http://www.imagemagick.org/Usage/compose/#compose
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

Re: Replace specific color of image

Post by ashwini »

Thanks for reply.

You are right there is no change in output image after encoding color in quote.
As you posted the links : I tried the -draw "color ... floodfill" command but in this, the offset need to be specified manually which I don’t want..

For more clarification this is the source image and I want to replace the regions which has red color to purple not giving the offset or co-ordinates manually.
I used this command:

convert veg.png -fuzz 15% -fill "#601848" -opaque "#c03030" cveg.png

I change the name of image for simplicity. I got the output which was not bad..but it doesn't look natural means there is light appearing on capsicum in original image and the resulted image is not giving proper color change.also the some part of red capsicum is not change due to shading of color as i am using command for single color replacement.

source img:
Image

output img after giving above command:
Image

i want the output image as below:
Image

also i wanted to know that is there is any way to get the region of same color in text file in the form of co-ordinates or other way
sorry for different size of images..
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 »

also i wanted to know that is there is any way to get the region of same color in text file in the form of co-ordinates or other way
convert your image to txt: format

see http://www.imagemagick.org/Usage/files/#txt


With regard to your color issue, the white areas inside the red->purple are too far from red so they are not caught by the color replacement. Did you try increasing the -fuzz value?

You may want to try to mask the image to show only the parts you want changed. Then process the image to change the hue only via -modulate so that the parts you want are the color you want. Then use the mask to combine the original and processed image so that you get the color you want for the white part of the mask the original image for the black part of the mask. Of course getting the right mask is not easy as the same techniques for isolating it are what you are using to change the color.

see
http://www.imagemagick.org/Usage/color_mods/#modulate
http://www.imagemagick.org/Usage/photos/#chroma_key


Where did you get the "correct" looking image? How was it created? If we know that, then perhaps there would be a way to get IM to do something similar.


If you are on Linux/Mac or Windows with Cygwin, you might try my script, huemap, at the link below.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Replace specific color of image

Post by anthony »

ashwini wrote:I got the output which was not bad..but it doesn't look natural means there is light appearing on capsicum in original image and the resulted image is not giving proper color change.also the some part of red capsicum is not change due to shading of color as i am using command for single color replacement.
That is what -opaque does. It is a direct color replacement.

At this time their is no 'shaded' or 'color vector' color replacement operator.
However there are masking techniques that can be used.

See IM Examples, Photo Handling, Chroma Key Masking
http://www.imagemagick.org/Usage/photos/#chroma_key

Essentially you create a mask to use it as a composition mask (as the above shows) to limit the effect of other global color change operations.

Watch out for the warning I give about doing masking with a red hue. That hue is split by the modulus of the hue values, so best idea is to 'modulate' the colors by '0' (180 shift), then mask around a 50% hue value (where red was shifted to)

I'll have to see about putting in a 'write mask' example, into the above link. That would 'simplify' the final operation, removing the need for three image composition, less processing as well as one less image in memory.

Write or Clip Masks (rough draft)
http://www.imagemagick.org/Usage/maskin ... ping_masks

ASIDE: These will be even more efficient in IMv7 as internal operators will have access to the can abort the processing for non-writeable pixels. :-)
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

Re: Replace specific color of image

Post by ashwini »

hi..thanks for both
back to my capscicum image red->purple. I already tried by increasing fuzz value (i.e 15% to 20%)but it will replace some part of green color to purple which don't work.

the other way provided by both using
http://www.imagemagick.org/Usage/photos/#chroma_key
i.e masking i tried that same eg: changing shirt color from pink to blue..from command line it works till second last step i.e getting shirt_mask.png

after giving > convert shirt_mask.png -morphology Smooth Square -negate miff:- | convert shirt.jpg -clip-mask miff:- -modulate 100,100,25 shirt_blue.jpg
there is no change original image pink color remains as it is ..what i am doing wrong?

also i am fail to apply the same operation for my capscicum image .
what is the miff exactly? the answer to your question of getting corrected image is from link http://abadpour.com/files/pdf/sp-298.pdf

can u give proper example with my capscicum image or my first image i.e of kitchen room?

thanks in advance...
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 »

Interesting reference. But IM 6 does not have Principle Components Analysis. The OHTA colorspace, however, is a approximation of PCA (Karhunen-Loeve transformation) for 3 channel images. see http://en.wikipedia.org/wiki/HSV_color_space footnote 33 and 34. But there is no way right now to specifically segment the image into some elliptical region for any give color. However, IM 7 under development will allow any number of channels. As such, I believe that PCA should be one new function developed to deal with multichannel images. (Another would be ISOCLASS for automatic segmentation).

I tested my huemap on the image of peppers, and it changed the hue quite nicely, but it does nothing to change the saturation and lightness.

Original:

Image


Huemap Processing:

huemap -h 0,320 -t 20,20 pepper_large.png pepper_large_huemap_h0x320_t20x20.png

Image


One might be able to save the mask from huemap and use it to change the lightness and saturation as well, but I have not tested that concept.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 »

The OHTA colorspace, however, is a approximation of PCA (Karhunen-Loeve transformation) for 3 channel images. see http://en.wikipedia.org/wiki/HSV_color_space footnote 33 and 34. Besides RGB, PCA or KLT is another orthogonal image representation (colorspace for 3 channels). PCA on an RGB image computes 3 channels that are orthogonal with the channels ordered by the most information as approximated by the length of the axes of a gaussian distributed ellipsoid variances from the covariance matrix. see
http://dsp.vscht.cz/konference_matlab/m ... udrova.pdf
http://www.cs.otago.ac.nz/cosc453/stude ... onents.pdf
http://abadpour.com/files//pdf/ethesisl.pdf

Some other common uses for PCA are face recognition, super resolution images and noise reduction.

Here is an approximation of your color transformation by converting to OHTA doing the fuzzy replacement and converting back to RGB. It however uses a fuzzy cube in OHTA color space rather than a fuzzy ellipsoid as in your article referenced earlier.

Original:
Image


OHTA color replace:

convert -size 1x1 xc:"#c03030" -colorspace ohta -format "%[pixel: u.p{0,0}]" info:
ohta(37.6471%,78.2361%,35.8831%)

convert -size 1x1 xc:"#601848" -colorspace ohta -format "%[pixel: u.p{0,0}]" info:
ohta(25.098%,54.7066%,38.2361%)

convert pepper_large.png -colorspace ohta -fuzz 11% -fill "rgb(25.1%,54.7%,38.2%)" -opaque "rgb(37.6%,78.2%,35.9%)" -set colorspace ohta -colorspace rgb pepper_large_ohta_f11.png

(Looks like -set colorspace ohta is not needed and can be removed from the above as it works without it, though does not seem to hurt to have it there)

Image

You can try to adjust the fuzz value a percent or two either way to get a better result.
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

Replace specific color of image

Post by ashwini »

hello..thanks for reply

I am sorry that i am posting this reply late..
as u given the solution your script i.e huemap . i installed cygwin and tried to run the script which was downloded..but i am facing problem ..i gone through the steps given in viewtopic.php?f=4&t=9631 .
I also added below code in script before IM command

# set directory for location of IM commands -- no trailing /
imdir="" # suggestions imdir="/usr/local/bin" or imdir="/usr/bin"

# set up aliases to IM functions
if [ "$imdir" != "" ]; then
alias animate="$imdir/animate"
alias compare="$imdir/compare"
alias composite="$imdir/composite"
alias conjure="$imdir/conjure"
alias convert="$imdir/convert"
alias display="$imdir/display"
alias identify="$imdir/identify"
alias import="$imdir/import"
alias mogrify="$imdir/mogrify"
alias montage="$imdir/montage"
alias stream="$imdir/stream"
fi

first time when i run this script i got errors like $'\r':command not find which i solved by giving $dos2unix.exe huemap.sh but still i am unable to run script sucessfully.
now its giving erros like.

1. d:\image\huemap.sh not found.
2. dirname:missing argument
3. unable to open image 'huemap'...etc
4. huemap:command not found

i downloded huemap script in my d:\image folder and in same folder source image is present.

I am giving this command as currently i am in d:\image
$bash huemap.sh huemap -h 0,320 -t 20,20 veg.png hueveg.png

i also tried ..
$bash d:\\image\\huemap.sh huemap -h 0,320 -t 20,20 veg.png hueveg.png

what i am doing wrong ? Is there is problem of giving wrong path?



thanks,
ashwini

ashwini

Posts: 6
Joined: Mon 30 Jan, 2012 10:55 pm
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 »

Sorry my scripts are bash unix scripts and do not run on Windows unless you have Cygwin installed.

It is always a good idea to identify your IM version and platform when posting.
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

Replace specific color of image

Post by ashwini »

hi..
i installed cygwin on windows platform then i tried huemap script.
now can you read my previous post regarding script and check where i am wrong?
I am new to this script concept..Image
IM vesion:ImageMagick 6.7.1-0 2011-07-06 Q16.


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

Re: Replace specific color of image

Post by fmw42 »

Did you install Cygwin and then the cygwin compatible version of IM? (see http://www.imagemagick.org/script/binary-releases.php) If so, then first tests simple IM commands from your IM/Cygwin and be sure they work before trying any script.
# set directory for location of IM commands -- no trailing /
imdir="" # suggestions imdir="/usr/local/bin" or imdir="/usr/bin"
You must set imdir to the path to where IM (convert) resides.
$bash huemap.sh huemap -h 0,320 -t 20,20 veg.png hueveg.png
You must remove one or the other of huemap.sh or huemap. Use the one that corresponds to the actual name of your downloaded or renamed file.

You need to provide the full path to where you have your script and where your images reside, but in unix notation not windows. Use forward slashes not backslashes.

You may or may not need to use "bash" at the beginning of the command.

I don't use windows and cannot say too much more. But there are some notes at http://www.imagemagick.org/Usage/windows/ for using cygwin.

Some other windows user who uses cygwin would likely have to go from here as I don't have any experience, but know that others have been successful.


Another alternative would be to use PHP exec() to run the script.
ashwini
Posts: 10
Joined: 2012-01-30T05:55:49-07:00
Authentication code: 8675308

Re: Replace specific color of image

Post by ashwini »

thanks fred,
I tried your script with proper path ..and its work fine.. :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Replace specific color of image

Post by fmw42 »

ashwini wrote:thanks fred,
I tried your script with proper path ..and its work fine.. :)
Glad to hear you got it working.

Fred
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Replace specific color of image

Post by anthony »

ashwini wrote: # set directory for location of IM commands -- no trailing /
imdir="" # suggestions imdir="/usr/local/bin" or imdir="/usr/bin"

# set up aliases to IM functions
if [ "$imdir" != "" ]; then
alias animate="$imdir/animate"
alias compare="$imdir/compare"
alias composite="$imdir/composite"
alias conjure="$imdir/conjure"
alias convert="$imdir/convert"
alias display="$imdir/display"
alias identify="$imdir/identify"
alias import="$imdir/import"
alias mogrify="$imdir/mogrify"
alias montage="$imdir/montage"
alias stream="$imdir/stream"
fi
May I suggest you creat a command in your personal 'bin' or 'scripts' directory.
For example create a directory called "bin" in your home.
Add to your ".profile"

Code: Select all

   export PATH="$HOME/bin:$PATH"
now create a script called "im"

Code: Select all

#!/bin/sh
# Run a personal IM command
imdir="/usr/local/bin"
export PATH="$imdir:$PATH"
exec "$@"
Now when you run "im convert ....." you run your version of "convert"
The script can also do other setups needed, such as library stuff.

For more info on this technique see..
Creating a Personal ImageMagick
http://www.imagemagick.org/Usage/api/#personal
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply