使用opencv中的Color_YUV2BGR从YUV到BGR的错误转换

时间:2017-03-27 22:28:40

标签: c++ opencv computer-vision yuv bgr

我想将一组YUV值转换为BGR。我的代码如下:

yuv = [100, 50, 150]
cv::Mat bgr_mat;
cv::Mat yuv_mat(1,1,CV_8UC3,cv::Scalar(yuv[0],yuv[1],yuv[2]));
cv::cvtColor(yuv_mat,bgr_mat,cv::COLOR_YUV2BGR);
cv::Vec3b bgr = bgr_mat.at<cv::Vec3b>(0,0);
cout << "b: " << (float)bgr.val[0] << " , g: " << (float)bgr.val[1] << " , r: " << (float)bgr.val[2] << endl;

我得到的输出是 - b:125,g:118,r:0

但预期输出为b:0,g:112,r:128

有人可以告诉我哪里出错了吗?

1 个答案:

答案 0 :(得分:1)

如果您使用的是Opencv 2.4这是一个已知的issue

根据类似question的回复 有一种可能的工作方式:

yuv = [100, 50, 150]
cv::Mat bgr_mat;
cv::Mat yuv_mat(1,1,CV_8UC3,cv::Scalar(yuv[0],yuv[1],yuv[2]));
cv::cvtColor(yuv_mat,bgr_mat,cv::COLOR_YUV2BGR);
cv::cvtColor(bgr_mat,bgr_mat,cv::COLOR_BGR2RGB);
cv::Vec3b bgr = bgr_mat.at<cv::Vec3b>(0,0);
cout << "b: " << (float)bgr.val[0] << " , g: " << (float)bgr.val[1] << " , r: " << (float)bgr.val[2] << endl;

输出:

  

b:0,g:118,r:125