yuv nv12图像保存颜色偏紫

如果resize先于cvtColor做,

1
2
3
4
Mat imgYUV = Mat(yuv_info.height * 3 / 2, yuv_info.width, CV_8UC1, yuv_data);
resize(imgYUV, imgYUV, Size(imgYUV.cols / 4, imgYUV.rows / 4), 0, 0, INTER_LINEAR);
Mat read_image_0;
cvtColor(imgYUV, read_image_0, CV_YUV2BGR_NV12);

保存的结果图像会偏紫,看上去蓝色或红色都变成了紫色,原因是resize把nv12格式中的U分量和V分量合并了。正确写法如下:

1
2
3
4
5
Mat imgYUV = Mat(yuv_info.height * 3 / 2, yuv_info.width, CV_8UC1, yuv_data);
Mat read_image_0;
cvtColor(imgYUV, read_image_0, CV_YUV2BGR_NV12);
resize(read_image_0, read_image_0, Size(read_image_0.cols / 4, read_image_0.rows / 4), 0, 0, INTER_LINEAR);
imwrite("/tmp/test.jpg", read_image_0);