OpenCV CV_RGB2Lab问题

时间:2014-04-07 20:16:11

标签: c++ opencv

我现在遇到使用opencv的CV_RGB2Lab功能的问题。我正在针对this site运行一个示例,由于某种原因,我得到的值与我在该网站上获得的值不同。以前有人遇到过这个问题吗?以下是我所谈论的测试案例。对不起,如果这不是初始化矩阵的最佳方法。

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);
}

1 个答案:

答案 0 :(得分:0)

问题在于如何指定RGB颜色。在openCV中,使用CV_32F格式的RGB颜色定义在0-1范围内,而CV_8U则在0-255范围内。所以如果你这样做:

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

然后你会得到正确的结果。所有这些规范都可以在cvtColor函数文档here中找到。