Converting Lab Values to RGB values in opencv

前端 未结 2 1119
小鲜肉
小鲜肉 2021-02-10 18:00

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 w

相关标签:
2条回答
  • 2021-02-10 18:38

    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;
    
    0 讨论(0)
  • 2021-02-10 18:39

    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.
    0 讨论(0)
提交回复
热议问题