c++opencvcomputer-visionrgbcielab

Converting single pixel from RGB to LAB with OpenCV (C++)


I am trying to convert a RGB value to the LAB color space using OpenCV. While doing some research I found someone with a similar goal here and have tried to replicate this approach.

After some messing around I was able to get the following code to compile and run:

int main(){
    int r_a = 168, g_a = 93, b_a = 201, r_b = 60, g_b = 117, b_b = 59;

    cv::Mat3f rgb_a (cv::Vec3f(r_a, g_a, b_a));
    cv::Mat3f rgb_b (cv::Vec3f(r_b, g_b, b_b));
    cv::Mat3f lab_a;
    cv::Mat3f lab_b;

    cv::cvtColor(rgb_a,lab_a,cv::COLOR_RGB2Lab);
    cv::cvtColor(rgb_b,lab_b,cv::COLOR_RGB2Lab);

    std::cerr << ">> rgb_a = " << rgb_a << "\n";
    std::cerr << ">> rgb_b = " << rgb_b << "\n";
    std::cerr << ">> lab_a = " << lab_a << "\n";
    std::cerr << ">> lab_b = " << lab_b << "\n";

    return 0;
}

When I run this, both LAB values are calculated as [100, 0, 0].

After a bit more browsing I found someone else had a similar issue when using OpenCV in python, see this question.

I was able to replicate this working solution in Python, but am still unable to find a fix for c++.

Any idea on how I can fix this? Is it a matrix shape issue? I am quite unfamiliar with the exact image formats for OpenCV in c++.


Solution

  • Posting an answer here in case anyone in the future runs into the same issue.

    As @M. Spiller pointed out I needed to scale my vector.

    I divided each value by 255.0 and then the conversion was able to execute correctly!