Page 1 of 1

Converting to cielab with a specified illuminant

Posted: 2016-04-11T15:04:28-07:00
by tom_dl
Imagemagick sensibly converts RGB to Lab using a reference white illuminant of D65. Adobe Photoshop converts RGB to Lab using a reference white illuminant of D50. I can see this must be the case having done several conversions while following along with Bruce Lindbloom's excellent calculator. How might I get Imagemagick to convert using white points other than D65? The -white-point option doesn't help at all. Ideally, I'm hoping for a line looking something like this:

Code: Select all

convert input.tif -white-point 0.34567,0.35851 -colorspace lab output.tif
(those are the xy coordinates of D50)

Any help would be very much appreciated.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-11T15:54:11-07:00
by snibgo
I don't know the answer.

From a search of the source code, it seems the setting "-white-point" is metadata that isn't used by ImageMagick code at all. And it doesn't seem to be passed to LCMS for any colour processing.

The answer might be to find colour profiles that can convert between D50 and D65.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-12T03:31:11-07:00
by tom_dl
Thanks very much for your answer. Using profiles seems like a good possible workaround for now. I'm guessing this may be a feature request if not present.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-12T07:01:35-07:00
by tom_dl
I believe the functionality I am looking for is a chromatic adaptation transform. LCMS is capable of this, so I can't see it would be a lot of work to implement.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-12T07:25:49-07:00
by snibgo
Yes, it seems so.

http://www.color.org/faqs.xalter has this:
Q. How can I convert D50 to D65?

A. The conversion from D50 to D65 requires a chromatic adaptation transform (CAT). Various methods are in common use - most of which employ a 3 x 3 matrix transformation. The coefficients of the matrix depend upon the illuminants one is converting to and from and the assumptions one wants to make about the best 'visual space' for doing this. The most popular among many users seems to be the linear part of what is known as the Bradford transform, though more recent transforms perform slightly better. [...]
http://ninedegreesbelow.com/photography ... ofile.html has "Table 4: sRGB D65 to ICC D50 Bradford Chromatic Adaptation Matrix". It also discusses issues that are beyond my own knowledge.

It would seem that conversion from D65 to D50, in sRGB space, can be done by:

Code: Select all

convert in.tiff -color-matrix \
"1.047886003 0.022918765 -0.050216095 \
 0.029581782 0.990483518 -0.017078708 \
-0.009251881 0.015072607 0.751678134" out.tiff
... but the columns and rows may need to be interchanged.

For consistency, I think you should also update the "-white-point" metadata.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-14T09:10:15-07:00
by tom_dl
I tried this matrix and interchanging rows and columns. However, I didn't get the same results as Lindbloom's calculator, or Photoshop's D50 conversion. Can anyone else test this and verify? Is a color matrix just not the right way to do this, or am I missing something? Again, I very much appreciate the help.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-17T16:29:09-07:00
by tom_dl
Further investigation has allowed me to achieve success with the L* component, but not the a or b components. To apply the matrix, you must convert to XYZ space first:

Code: Select all

convert in.tif -colorspace xyz -color-matrix \
"1.047886003 0.022918765 -0.050216095 \
0.029581782 0.990483518 -0.017078708 \
-0.009251881 0.015072607 0.751678134" \
-colorspace lab out.tif
Similarly, using Bruce Lindbloom's matrix for RGB->XYZ using D50 illuminant (http://brucelindbloom.com/Eqn_RGB_XYZ_Matrix.html) gives a correct L* component but wrong a and b components:

Code: Select all

convert in.tif -colorspace rgb -color-matrix \
"0.4360747  0.3850649  0.1430804 \
0.2225045  0.7168786  0.0606169 \
0.0139322  0.0971045  0.7141733" \
-colorspace lab out.tif
Any suggestions on how to get correct a and b components?

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-23T16:27:39-07:00
by tom_dl
For anyone interested, I have found the best way to achieve a D50 cielab conversion is to use cctiff (supplied as part of Argyll CMS). The following line makes the desired conversion:

Code: Select all

cctiff -t1 -ir -p sRGB.icm input.tif output.tif
When I run

Code: Select all

convert output.tif txt:
the values are the same as Photoshop's and Lindbloom's D50 calculations.

I think this functionality would be a great addition to ImageMagick, but in the meantime, I hope this helps anyone who's stuck with cielab conversion in IM!

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-23T17:10:57-07:00
by fmw42
Did you try IM -intent relative? See http://www.imagemagick.org/script/comma ... php#intent

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-24T05:55:50-07:00
by tom_dl
I hadn't but just did, and it actually makes no difference. I also tried -black-point-compensation and the other intents, and always get exactly the same results. Should -intent change a cielab conversion (i.e. does the fact that it makes no difference imply it is a bug?)

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-24T06:17:01-07:00
by snibgo
As far as I know, "-intent" is a setting that applies only to "-profile". If there is no "-profile" operation, "-intent" does nothing.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-24T11:21:07-07:00
by fmw42
From his cctiff command above, I assume he was using a profile.

My thoughts were to try something like, though just a guess

Code: Select all

convert input.tif -intent relative -profile path2/sRGB.icm output.tif
or

Code: Select all

convert input.tif -profile path2/sRGB.icm -intent relative output.tif
Not sure of the order here.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-24T13:12:17-07:00
by snibgo
IM's "-intent" needs to come before "-profile".

A profile may not be applicable to all settings of "-intent". For example, sRGB.icc as supplied with ImageMagick has only two settings, not four.

Re: Converting to cielab with a specified illuminant

Posted: 2016-04-24T23:22:29-07:00
by yup
tom_dl wrote:Should -intent change a cielab conversion (i.e. does the fact that it makes no difference imply it is a bug?)
Can't tell about IM implementation, but in theory "-intent" should do nothing when converting from RGB color model to LAB, because gamut of any RGB color space (sRGB, AdobeRGB, PhotoRGB, etc.) is a subset of LAB.

Re: Converting to cielab with a specified illuminant

Posted: 2018-07-14T06:09:54-07:00
by tom_dl
Although >2yrs after the initial post, I should report that the color-matrix solution (through XYZ space) yields the correct results. The following command works:

Code: Select all

magick in.tif -colorspace xyz -color-matrix \
"1.0478112  0.0228866 -0.0501270
 0.0295424  0.9904844 -0.0170491
-0.0092345  0.0150436  0.7521316" \
-colorspace lab out.tif
The above matrix is taken from the "from D65 to D50" section of http://www.brucelindbloom.com/Eqn_ChromAdapt.html

I have no idea why it differs by a tiny amount from the one snibgo found on ninedegreesbelow.com

I think ImageMagick should store the matrices for all of these chromatic adaptation transforms and provide a simple option as a shortcut to them, so a command could be typed something like this:

Code: Select all

magick in.tif -illuminant D50 -colorspace lab out.tif 
I'll take this suggestion to the Issues section of the ImageMagick github to see if it sparks any interest.