List Info

Thread: RGB to LAB conversion




RGB to LAB conversion
user name
2007-06-21 10:42:41
Hi,

I am trying to build a profile that I can use in photoshop
to imitate a 
LUT that I use in Shake. I use the forward sampler to go
from RGB to 
LAB, converting each RGB sample by calling a function to
apply the lut 
to that value then converting to LAB. The code looks
something like this:

int forward(unsigned short *in, unsigned short *out, void
*lut) {
 
  transformValue(lut, in);
 
  float rgb[3] = {float(in[0])/65535, float(in[1])/65535, 
float(in[2])/65535};
  float lab[3];
 
  rgb2lab(rgb, lab);

  cmsCIELab cmsLab;
  cmsLab.L = lab[0];
  cmsLab.a = lab[1];
  cmsLab.b = lab[2];
 
  cmsFloat2LabEncoded(out, &cmsLab);

  return 0;

}

The calculation I use to convert from RGB to LAB comes from

http://www.easyrgb.co
m/math.php . When I 'burn in' the input profile to 
an image using tifficc most colours get very close to what
the Shake LUT 
does but some are still not quite there. I'm wondering if
there is a 
correct calculation to use, or a better way of using lcms to
get closer.

One other issue is that when I convert from LAB to RGB in
the reverse 
sampler, the majority of RGB values are out of gamut. This
is 
unsurprising since LAB has a much larger gamut than RGB but
it means 
that only a small number of samples are actually
contributing which 
further reduces the accuracy. Is there a better way to do
this that will 
allow more, or all, samples to contribute?

Any hints or tips appreciated!

Thanks,
Ian

------------------------------------------------------------
-------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and
take
control of your XML. No limits. Just data. Click to get it
now.
http://sourcefor
ge.net/powerbar/db2/
_______________________________________________
Lcms-user mailing list
Lcms-userlists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lcms-user


Re: RGB to LAB conversion
country flaguser name
United States
2007-06-21 13:27:47
On Thursday 21 June 2007 08:42, Ian McGonigal wrote:
> Hi,
>
> I am trying to build a profile that I can use in
photoshop to imitate a
> LUT that I use in Shake. I use the forward sampler to
go from RGB to
> LAB, converting each RGB sample by calling a function
to apply the lut
> to that value then converting to LAB. The code looks
something like this:
>
> int forward(unsigned short *in, unsigned short *out,
void *lut) {
>
>   transformValue(lut, in);
>
>   float rgb[3] = {float(in[0])/65535,
float(in[1])/65535,
> float(in[2])/65535};
>   float lab[3];
>
>   rgb2lab(rgb, lab);
>
>   cmsCIELab cmsLab;
>   cmsLab.L = lab[0];
>   cmsLab.a = lab[1];
>   cmsLab.b = lab[2];
>
>   cmsFloat2LabEncoded(out, &cmsLab);
>
>   return 0;
>
> }
>
> The calculation I use to convert from RGB to LAB comes
from
> http://www.easyrgb.co
m/math.php . When I 'burn in' the input profile to
> an image using tifficc most colours get very close to
what the Shake LUT
> does but some are still not quite there. I'm wondering
if there is a
> correct calculation to use, or a better way of using
lcms to get closer.
>
> One other issue is that when I convert from LAB to RGB
in the reverse
> sampler, the majority of RGB values are out of gamut.
This is
> unsurprising since LAB has a much larger gamut than RGB
but it means
> that only a small number of samples are actually
contributing which
> further reduces the accuracy. Is there a better way to
do this that will
> allow more, or all, samples to contribute?
>
> Any hints or tips appreciated!
>
> Thanks,
> Ian

Some observations but I don't know if these will help.

I assume that your RGB to Lab conversion first does an RGB
to XYZ conversion 
and then converts from XYZ to Lab using the algorithms from
the web site.  
The RGB to XYZ conversion algorithm given on that web page
assumes the 
following about the RGB values:

1. sRGB gamma curve
2. D65 white point
3. 8 bits per channel

It is basically a matrix conversion with some added logic to
linearize the 
gamma, and also corrects for the fact that sRGB uses a
compound gamma curve, 
before applying the matrix.  If the above are not true the
conversion will 
give incorrect results.  Therefore unless what is returned
by the call to 
transformValue(lut, in) is in the sRGB color space the
conversion is not 
valid.

In addition, I assume you are doing the conversion in the
call to rgb2lab(rgb, 
lab) but you did not include that code.  The algorithm from
the web site 
normalizes the RGB values to a 0.0 to 1.0 range (var_R =
(R/255)) before 
converting the gamma to linear and then renormalizes to a
0.0 to 100.0 range 
before applying the conversion matrix.  But I see in your
code that you are 
normalizing to a 0.0 .. 1.0 range before calling
rgb2lab(rgb, lab).   Could 
this be an issue?

I am not sure why you are hand coding the XYZ to Lab
conversion (of course 
since I can't see the code I am not sure your are either)
since this is 
available off the self in lcms.  In addition, the lcms
function allows you to 
specify the white point to be used for the conversion.

The reverse transform would be converting to an sRGB color
space since this is 
the assumed input to the forward transform.   In theory, if
the forward 
transform is actually working correctly it should not create
any values in 
the Lab space that are outside of the input colors spaces
gamut and applying 
the reverse transform to any values that are the result of
the forward 
transform should only return values that are in gamut.  Of
course, some 
quantitization error will exist and some of the values from
the return trip 
will likely fall just out side of the starting gamut but by
a very small 
amount.

I also see some things you have written that indicate that
you may not 
understand of how this works at a fairly basic level. 
Specifically "This is 
unsurprising since LAB has a much larger gamut than
RGB..."  Much larger than 
the gamut of which RGB color space?  This is certainly true
for sRGB (perhaps 
this is what you intended to write) in which case we are
talking about a 
specific well characterized RGB color space.  But there are
other RGB color 
spaces that are much larger than sRGB that are used by at
least some users.  
One example is ProPhotoRGB which is almost as large as the
Lab space.  In 
addition, although it might not be useful, it would be
possible to define an 
arbitrary RGB color space that is much larger than the Lab
color space.  In 
other words unless we are talking about specific well
characterized color 
spaces terms like larger and smaller are meaningless.

Hal

------------------------------------------------------------
-------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and
take
control of your XML. No limits. Just data. Click to get it
now.
http://sourcefor
ge.net/powerbar/db2/
_______________________________________________
Lcms-user mailing list
Lcms-userlists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lcms-user


Re: RGB to LAB conversion
user name
2007-06-21 15:03:39
Ian,

what is your Shake LUT about?  RGB->RGB or Lab->Lab
...

One possible route:
Figure out what are the movements in your Shake LUT, bring
it in relation 
to a PCS colour space (LAB or XYZ), if needed, and use a
profiling package 
to interpolate the scattered data.
This seems a robust route to me, as it is allmost
independend from source 
or destination spaces.
The according ICC profiles are called abstract profiles.
Photoshop should 
support such.

Hope this makes any sense to you.


regards
Kai-Uwe Behrmann
--
developing for colour management 
www.behrmann.name + www.oyranos.org + www.cinepaint.org


------------------------------------------------------------
-------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and
take
control of your XML. No limits. Just data. Click to get it
now.
http://sourcefor
ge.net/powerbar/db2/
_______________________________________________
Lcms-user mailing list
Lcms-userlists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/lcms-user


[1-3]

about | contact  Other archives ( Real Estate discussion Medical topics )