Wrong conversion from YUV to BGR using Color_YUV2BGR in opencv

穿精又带淫゛_ 提交于 2020-01-05 02:47:27

问题


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



来源:https://stackoverflow.com/questions/43057490/wrong-conversion-from-yuv-to-bgr-using-color-yuv2bgr-in-opencv

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!