OpenCV实现RGB与YUV的转换

1、ARGB -> RGB

//unsigned char *data 存的是ARGB的裸数据;

cv::Mat argbImg;
cv::Mat rgbImg(cy, cx,CV_8UC3);
yuvImg.create(cy, cx, CV_8UC4);
memcpy(argbImg.data, data, len);
cv::cvtColor(argbImg, rgbImg, CV_RGBA2RGB);
flip(rgbImg, rgbImg, 0);    //垂直翻转

2、YUV -> RGB

//unsigned char *data 存的是YUYV的裸数据;

cv::Mat yuvImg;
cv::Mat rgbImg(cy, cx,CV_8UC3);
yuvImg.create(cy , cx, CV_8UC2);
memcpy(yuvImg.data, data, len);
cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_YUYV);

3、I420 -> RGB

//unsigned char *data 存的是I420的裸数据;

cv::Mat yuvImg;
cv::Mat rgbImg(cy, cx,CV_8UC3);
yuvImg.create(cy * 3/2, cx, CV_8UC1);
memcpy(yuvImg.data, data, len);
cv::cvtColor(yuvImg, rgbImg, CV_YUV2BGR_I420);

OpenCV提供的转换函数实现YUV到RGB的转换:

bool YV12ToBGR24_OpenCV(unsigned char* pYUV,unsigned char* pBGR24,int width,int height) 
{ 
if (width < 1 || height < 1 || pYUV == NULL || pBGR24 == NULL) 
return false; 
Mat dst(height,width,CV_8UC3,pBGR24); 
Mat src(height + height/2,width,CV_8UC1,pYUV); 
cvtColor(src,dst,CV_YUV2BGR_YV12); 
return true; 
}

 

你可能感兴趣的:(C++,图形图像处理)