4

I am trying to convert the Lab values to its corresponding RGB values.I don't want to convert Lab image to RGB image but some values of L a and b.The function cvCvtColor only works for images.Can anybody tell me how to do this.

Thanks;

Code :

CvMat* rgb = cvCreateMat(centres->rows,centres->cols,centres->type);
cvCvtColor(centres,rgb,CV_Lab2BGR);
ATG
  • 732
  • 3
  • 11
  • 21

2 Answers2

10

I don't know how to do it in OpenCV, but if something else is alright I've implemented it in C. See function color_Lab_to_LinearRGB and color_LinearRGB_to_RGB.

Here's the code:

double L, a, b;
double X, Y, Z;
double R, G, B;

// Lab -> normalized XYZ (X,Y,Z are all in 0...1)

Y = L * (1.0/116.0) + 16.0/116.0;
X = a * (1.0/500.0) + Y;
Z = b * (-1.0/200.0) + Y;

X = X > 6.0/29.0 ? X * X * X : X * (108.0/841.0) - 432.0/24389.0;
Y = L > 8.0 ? Y * Y * Y : L * (27.0/24389.0);
Z = Z > 6.0/29.0 ? Z * Z * Z : Z * (108.0/841.0) - 432.0/24389.0;

// normalized XYZ -> linear sRGB (in 0...1)

R = X * (1219569.0/395920.0)     + Y * (-608687.0/395920.0)    + Z * (-107481.0/197960.0);
G = X * (-80960619.0/87888100.0) + Y * (82435961.0/43944050.0) + Z * (3976797.0/87888100.0);
B = X * (93813.0/1774030.0)      + Y * (-180961.0/887015.0)    + Z * (107481.0/93370.0);

// linear sRGB -> gamma-compressed sRGB (in 0...1)

R = R > 0.0031308 ? pow(R, 1.0 / 2.4) * 1.055 - 0.055 : R * 12.92;
G = G > 0.0031308 ? pow(G, 1.0 / 2.4) * 1.055 - 0.055 : G * 12.92;
B = B > 0.0031308 ? pow(B, 1.0 / 2.4) * 1.055 - 0.055 : B * 12.92;
Cory Nelson
  • 29,236
  • 5
  • 72
  • 110
0

I think the only way to do what you want is:

  • look at the OpenCV source code or documentation for cvCvtColor and implement the equations yourself (they're all explicitly given on that page)
  • create a dummy image with the few Lab values you want to convert (say 1 by n by 3, where n is the number of values you want to convert) and use cvCvtColor around that.
mathematical.coffee
  • 55,977
  • 11
  • 154
  • 194
  • Thanks for your reply..Can you please explain this.Because I have used this cvtColor function but it is giving me error.I am adding the code I have used. – ATG Feb 21 '12 at 05:37