0

I'm having issues using the opencv's CV_RGB2Lab function right now. I'm running an example against this site and for some reason the values I get back aren't the same as the ones I get on that website. Anyone run into this issue before? Below is a test case of what I'm talking about. Sorry if it's not the best way to initialize the matrices.

void testLabConversion() {
  cv::Mat image(2, 2, CV_32FC3); // Temp Mat variable
  std::vector<cv::Mat> rgb_planes;           // Split along the channels
  split(image, rgb_planes);

  rgb_planes[0].at<float>(0,0) = 250; rgb_planes[1].at<float>(0,0) = 150; rgb_planes[2].at<float>(0,0) = 210;
  rgb_planes[0].at<float>(0,1) = 111; rgb_planes[1].at<float>(0,1) = 203; rgb_planes[2].at<float>(0,1) =  73;
  rgb_planes[0].at<float>(1,0) = 255; rgb_planes[1].at<float>(1,0) = 254; rgb_planes[2].at<float>(1,0) = 253;
  rgb_planes[0].at<float>(1,1) =   0; rgb_planes[1].at<float>(1,1) =   1; rgb_planes[2].at<float>(1,1) = 250;

  merge(rgb_planes, image); // Merge the channels back together.

  cv::Mat lab;
  cv::cvtColor(image, lab, CV_RGB2Lab);        // Convert to Lab format
  split(lab, rgb_planes);

  cv::Mat compare(2, 2, CV_32FC3); // Temp Mat variable
  std::vector<cv::Mat> comp_planes;           // Split along the channels
  split(compare, comp_planes);

  comp_planes[0].at<float>(0,0) = 74.05513399632304; comp_planes[1].at<float>(0,0) =  45.378087487482844; comp_planes[2].at<float>(0,0) = -14.622711663587662;
  comp_planes[0].at<float>(0,1) = 73.91543571217753; comp_planes[1].at<float>(0,1) =  -50.51869054958624; comp_planes[2].at<float>(0,1) =   54.66579060349854;
  comp_planes[0].at<float>(1,0) = 99.70362552637009; comp_planes[1].at<float>(1,0) = 0.16050508518777873; comp_planes[2].at<float>(1,0) =  0.5824734325686975;
  comp_planes[0].at<float>(1,1) = 31.63288764075348; comp_planes[1].at<float>(1,1) =   77.86147327459526; comp_planes[2].at<float>(1,1) = -106.17348604606846;

  for (int i=0;i<3;++i)
    for (int j=0;j<2;++j)
      for (int k=0;k<2;++k)
        TS_ASSERT_DELTA(comp_planes[i].at<float>(j,k), rgb_planes[i].at<float>(j,k), 1e-6);
}
Kevin Melkowski
  • 463
  • 1
  • 5
  • 17
  • Is your problem the same as this one? http://stackoverflow.com/questions/11386556/converting-an-opencv-bgr-8-bit-image-to-cie-lab – Mark Ransom Apr 07 '14 at 20:25

1 Answers1

0

The problem lies in how you specify the RGB colors. In openCV RGB colors using the CV_32F format are defined to be in the range 0-1, while CV_8U is in the range 0-255. So if you do:

...
merge(rgb_planes, image); // Merge the channels back together.
image = (1.0/255.0) * image;
...

Then you will get the correct result. All these specifications can be found in the cvtColor function documentation here.

Reed Richards
  • 4,178
  • 8
  • 41
  • 55