Wrong conversion from YUV to BGR using Color_YUV2B

2019-08-02 07:21发布

问题:

I want to convert a single set of YUV value to BGR. My code is as follows:

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;

The output I get is - b: 125, g: 118, r: 0

But the expected output is b: 0, g: 112, r: 128

Can somebody please tell me where am I going wrong?

回答1:

If you are using Opencv 2.4 It is a known issue

Acording to responses on a similar question There is a possible workarond:

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;

Output:

b: 0 , g: 118 , r: 125