Color Correction with curves

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
papoola
Posts: 10
Joined: 2010-07-16T08:25:16-07:00
Authentication code: 8675308

Color Correction with curves

Post by papoola »

Hi everyone,

I searched all over internet and still haven't found a solution for this challenge.

I am dying to know how to implement color correction in the way we normally do it in Photoshop by using curves with a very basic bent graphic like shown in these pictures: I am developing a Java program (using ImageMagick) for correcting colors of photos. User should be able to enter 4 parameters to correct colors Density (higher density results in darker image) Red Green and Blue values in order to see results immediately. Previously I used -color-matrix which was fast enough but found out that it did a linear correction (unlike in Photoshop curves).

I know I could find solution in -fx but there are two problems with that:
  • I don't know the right formula for -fx
  • It is very slow
If someone could tell me how formula should look like I could play with it until I find right parameter. If I would find the right formula then I could use that formula to generate a lookup table. Using this lookup table maybe I would be able to call -color-matrix with right values in order to get same color correction effects.

I also tried -tint which is a faster solution that works well with one parameter. But as soon as I combine parameters like this:
convert -fill white -tint 120 -fill red -tint 20 C:\test.JPG c:\test2.jpg
image becomes very dark and operation takes more time than -color-matrix.

I read in description of -tint that it changes the midtones. Perhaps this is what I am looking for but I want it to run fast and use 4 parameters I mentioned earlier.

Could some one help me with this challenge?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Correction with curves

Post by fmw42 »

In IM, the closest to curves is to use -gamma with different values for each channel. However, I have a script that allows you to create a curve from a few points and apply it. Since my script applies the curve to all channels, you would need to separate the channels and apply a curve to each. Then combine the channels back again. This is a bash unix script using IM. See curves in my scripts at the link below.
papoola
Posts: 10
Joined: 2010-07-16T08:25:16-07:00
Authentication code: 8675308

Re: Color Correction with curves

Post by papoola »

This is a great honor to have maker of Fred's ImageMagick Scripts (so probably Fred himself) helping me out with this. I found links to that page so many times on internet and found very useful information there. As a matter of fact I have had a quick look at curves script before. Thank you for your fast response and your effort and wonderful page offering scripts to help people work with imagemagick easier. Though I was hoping that scripts were in PHP so get it running under windows would have been easier.

Indeed I also looked to see if -gamma would help me and I have to say that it gets very close to desired results (as you said in your reply). Unfortunately -gamma is not good enough in my case. Your script might be answer to this problem however my problem is a bit more complicated than I explained.

Java software that I am developing is gonna replace old software (which is made by other company / is not using IM). Since new software (which is using IM) requires same correction values (DRGB) from database it should render images exactly in same way as old software did. So this would make things more difficult since I don't know which correction curve old software used and how it looked like!

So I came with a new idea which might be do-able. If I would render an image using old software with D=0 R=0 G=0 B=0 and render same image again this time with D=0 R=4 G=0 B=0 and compare them using IM compare, perhaps I could get shape of curve and use that information to call your script and get a lookup table.

I did following tests:
colorcorrect.jpg (rendered with old software D=0 R=0 G=0 B=0)
colorcorrect_r4.jpg (rendered with old software D=0 R=4 G=0 B=0)
colorcorrect_r20.jpg(rendered with old software D=0 R=20 G=0 B=0)
colorcorrect_d10.jpg (rendered with old software D=10 R=0 G=0 B=0)

compare -verbose -metric MAE c:\colorcorrect.jpg c:\colorcorrect_r4.jpg null:
red: 2.59663 (0.0101829)
green: 0.402568 (0.0015787)
blue: 0.794319 (0.00311498)
all: 1.26451 (0.00495885)

compare -verbose -metric MAE c:\colorcorrect.jpg c:\colorcorrect_r20.jpg null:
red: 10.8151 (0.0424122)
green: 0.385422 (0.00151146)
blue: 0.641358 (0.00251513)
all: 3.9473 (0.0154796)

compare -verbose -metric MAE c:\colorcorrect.jpg c:\colorcorrect_d10.jpg null:
red: 7.83482 (0.0307248)
green: 13.4417 (0.0527124)
blue: 13.1893 (0.0517229)
all: 11.4886 (0.0450533)

any idea if that would be possible by comparing more of these and use these values for reproducing same color correction effect?
if so how should I use these values to call curves script exactly?
if LUT has been made how can I apply it to images very fast??

I would really appreciate if you or someone else would have got answer for this?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Correction with curves

Post by fmw42 »

Look at -hald-clut for duplicating some graylevel or color transformation. You can also just apply your transformation to a gradient image. Then you can use that as input to -clut (or even make a profile). Also see my tidbits pages at http://www.fmwconcepts.com/imagemagick/ ... mage2graph and http://www.fmwconcepts.com/imagemagick/ ... raph2image

The latter is an example of taking a curve (in this case a straight line) and making a lut image from a gradient to use to transform and image. Perhaps that is what you want to do or something like it. If you have the gui to draw the transformation curve, then you can easily turn it into a lut in this manner. (presuming I don't misunderstand your problem)

P.S. Thanks for the compliments.

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

Re: Color Correction with curves

Post by anthony »

Color Correction with Curves...

See IM Examples, Color Modifications for general stuff
http://www.imagemagick.org/Usage/colors/

For specifics...'Curves' Adjustments
http://www.imagemagick.org/Usage/color/#curves

Hald Adjustment
http://www.imagemagick.org/Usage/color/#hald-clut
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
papoola
Posts: 10
Joined: 2010-07-16T08:25:16-07:00
Authentication code: 8675308

Re: Color Correction with curves

Post by papoola »

Special thanks to Fred and Anthony for your quick responses.

Fred I am not intending to let the user change curve in GUI. All I need is to perform same correction actions like old software does. So graph 2 1d LUT script is not what I am looking for. Hald could be indeed the solution I am looking for. I am just not sure how quick it works in my case since I want to integrate it in correction software and use it with a every mouse click (+/- of D,R,G and B buttons).

Let me sure if I got this right:

1. I need to make a HALD-CLUT or just a image gradient
2. Then apply color correction to it (using old software) way I want it to be. I am especially interested to know how to reproduce one step of +/- D, +/-R, +/-G and +/-B in old software.
3. Call -clut with adjusted haldclut or gradient image to get same effect inside imagemagick

So these steps are logical and make sense to me.
Problem I am having now is that I am not able to make a HALD-CLUT image at step 1 using:

convert.exe HALD:8 c:\hald.jpg

this gives me these error messages on same computers (one with WinXP and one with Win7):

convert.exe: no decode delegate for this image format `8' @ error/constitute.c/R
eadImage/532.
convert.exe: missing an image filename `c:\hald.jpg' @ error/convert.c/ConvertIm
ageCommand/2970.


So other option at step 1 would be to make a gradient LUT image (instead of a hald-clut one) but I don't know how to make it? I need to make one similar to hald-clut which offers lot of variaties in color so applying a -clut would guarantee maximum similarity to result of color transformation in old software

Anthony thank you for mentioning those links!
I had scanned those pages before but link to hald-clut page is mostuseful to me.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Correction with curves

Post by fmw42 »

To use -hald-clut:

1) you create the hald-clut image first to the resolution you want
2) you process the hald-clut image with the processing you want to reproduce
3) you apply the hald-clut image to any other image to apply the same processing to it as in step 2)

see http://www.imagemagick.org/Usage/color/#hald-clut

Not sure this is what you want.

I really think you need to create a grayscale image, then process the grayscale image so that each channel has the color transformation curve you need applied to it. You can use -fx to create the transformations or something like my curves script to get the lut (or plmlut) or any way you know to calibrate the grayscale image with the color correction you need. Then use the modified grayscale image (which is now in color due to different transformations for each channel) to any image to color correct it, via -clut.

see
http://www.imagemagick.org/Usage/color/#clut
and
http://www.imagemagick.org/Usage/color/#fx_to_lut
and
http://www.imagemagick.org/Usage/color/#clut_alpha
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Color Correction with curves

Post by anthony »

papoola wrote:Problem I am having now is that I am not able to make a HALD-CLUT image at step 1 using:

convert.exe HALD:8 c:\hald.jpg

this gives me these error messages on same computers (one with WinXP and one with Win7):

convert.exe: no decode delegate for this image format `8' @ error/constitute.c/R
eadImage/532.
convert.exe: missing an image filename `c:\hald.jpg' @ error/convert.c/ConvertIm
ageCommand/2970.
That looks like you IM is to oold and does not have the HALD coder implemented. It was added to IM in IM v6.5.3-4 as specified in IM Examples....
http://www.imagemagick.org/Usage/color/#hald-clut
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
papoola
Posts: 10
Joined: 2010-07-16T08:25:16-07:00
Authentication code: 8675308

Re: Color Correction with curves

Post by papoola »

Fred 3 steps you mentioned are exact same 3 steps I had in mind. I was trying to make hald-clut image (in step 1) using:

Code: Select all

convert.exe HALD:8 c:\hald.jpg
But it still doesn't work.

Anthony HALD coder is apparantly not implemented in windows binaries of IM-6.6.2 and IM-6.6.3-0! I tried to make hald image using command above and got same error message!
Any idea how to get hald:8 working under windows?

Otherwise I will just (as fred has suggested) use a gradient grayscale image as clut image in step1 and let you guys know if I have succeeded.

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

Re: Color Correction with curves

Post by anthony »

check if hald is listed in

Code: Select all

convert -list format
EG:

Code: Select all

HALD*           r--   Identity Hald color lookup table image
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
papoola
Posts: 10
Joined: 2010-07-16T08:25:16-07:00
Authentication code: 8675308

Re: Color Correction with curves

Post by papoola »

no :(

Code: Select all

...
GRADIENT* r--   Gradual linear passing from one shade to another
GRAY* rw+   Raw gray samples
GRB* rw+   Raw green, red, and blue samples
GROUP4* rw-   Raw CCITT Group4
HISTOGRAM* -w-   Histogram of the image
HRZ* rw-   Slow Scan TeleVision
HTM* -w-   Hypertext Markup Language and a client-side image map
HTML* -w-   Hypertext Markup Language and a client-side image map
ICB* rw+   Truevision Targa image
ICO* rw+   Microsoft icon
...
so HALD* is not listed in my case!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Color Correction with curves

Post by fmw42 »

withdrawn -- sorry I overlooked your earlier response about version. I am not aware of any issues with Windows, but I am not a Windows user.

Can some Windows user verify they have problems, too, creating a hald image?
Post Reply