2
votes

For the purpose of visualizing certain data, I'm supposed to utilize the colorings. Namely, the C++ code I have on disposal is outputting using cairo-graphics, and the function is based on RGB space. The data I work with should, on the other hand, utilize "certain slice" of the CIELab color space.

The question is: what would be the most appropriate way to do this with C++? Perhaps some conversion that might still rely on the RGB function provided by cairo (though I doubt this alternative, since RGB is device-dependent)?

2

2 Answers

4
votes

OpenCV has some built-in color conversions. You could use them, or use them as an inspiration for your own color conversion routine. See the doc about color conversion in OpenCV (cv::cvtColor)

One possible way to use it could be :

#include <cv.h>
#include <highgui.h>
int main()
{
    cv::Mat imgRgb = cv::imread("file.bmp");
    cv::Mat imgLab;
    cv::cvtColor(imgRgb, imgLab, Cv::CV_BGR2Lab);

    //access Lab values
    int y = 50;
    int x = 20;
    double L = imgLab.at<double>(y,x)[0]; //beware the order : openCV treats images as matrixes, thus the y param come first
    double a = imgLab.at<double>(y,x)[1];
    double b = imgLab.at<double>(y,x)[2];   
}

Beware: I did not test or compile this code, this is just a draft.

4
votes

I have compiled an tested the above. For future reference here is a function to convert RGB to CIELAB

#include <cv.h>
#include <highgui.h>
Mat BGR2CIELab(Mat const &rgb_src)
{
    Mat imgLab;
    cvtColor(rgb_src, imgLab, CV_BGR2Lab);

    //access Lab values
    int y = 50;
    int x = 20;
    Vec3d pix_bgr = imgLab.ptr<Vec3d>(y)[x]; //beware the order : openCV treats images as matrixes, thus the y param come first
    double L = pix_bgr.val[0];
    double a = pix_bgr.val[1];
    double b = pix_bgr.val[2];

    return imgLab;
}