如何使用cvCvConvert将YUV422psemi格式转换为RGB
2个回答
展开全部
法一:OpenCV自带cvCvtColor
说明:这种方法会出现图片“泛白”,具体原因网上是说cvCvtColor这个函数左右协议不同,不太懂。
代码:
void FileWriteFrames(){
char *filename = "E:\\openCV\\zhang\\yuvSource\\football_cif.yuv";
ifstream readMe(filename, ios::in | ios::binary); // 打开并读yuv数据
IplImage *image, *rgbimg, *yimg, *uimg, *vimg, *uuimg, *vvimg;
cvNamedWindow("yuv",CV_WINDOW_AUTOSIZE);
rgbimg = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 3);
image = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 3);
yimg = cvCreateImageHeader(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 1); // 亮度分量
uimg = cvCreateImageHeader(cvSize(ISizeX/2, ISizeY/2), IPL_DEPTH_8U, 1); // 这两个都是色度分量
vimg = cvCreateImageHeader(cvSize(ISizeX/2, ISizeY/2), IPL_DEPTH_8U, 1);
uuimg = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 1);
vvimg = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 1);
int nframes;
for(nframes = 0; nframes < FCount; nframes ++){
char nframesstr[20];
readMe.read((char*)Y[nframes],ISizeX*ISizeY);
//readMe.seekg(-ISizeX*ISizeY, ios::cur);
//readMe.read((char*)buf[nframes],ISizeX*ISizeY+ISizeX/2*ISizeY/2+ISizeX/2*ISizeY/2);
readMe.read((char*)buf[nframes],ISizeX/2*ISizeY/2);
readMe.read((char*)buf2[nframes],ISizeX/2*ISizeY/2);
cvSetData(yimg,Y[nframes],ISizeX);
//cvSetData(uimg,buf[nframes] + ISizeX*ISizeY, ISizeX/2);
cvSetData(uimg,buf[nframes], ISizeX/2);
cvSetData(vimg,buf2[nframes], ISizeX/2);
cvResize(uimg,uuimg, CV_INTER_LINEAR);
cvResize(vimg,vvimg, CV_INTER_LINEAR);
cvMerge(yimg,uuimg,vvimg,NULL,image); // 合并单通道为三通道
cvCvtColor(image,rgbimg,CV_YUV2BGR);
stringstream ss; // 类型转换统一转换为char* 类型
ss << nframes;
ss << ".jpg" ;
ss >> nframesstr;
cvShowImage("yuv", rgbimg);
cvSaveImage(nframesstr,rgbimg);
int c = cvWaitKey(30);
if((char)c == 27)
{
break;
}
}
readMe.close();
cvReleaseImage(&uuimg);
cvReleaseImage(&vvimg);
cvReleaseImageHeader(&yimg);
cvReleaseImageHeader(&uimg);
cvReleaseImageHeader(&vimg);
cvReleaseImage(&image);
cvDestroyWindow("yuv");
}
说明:这种方法会出现图片“泛白”,具体原因网上是说cvCvtColor这个函数左右协议不同,不太懂。
代码:
void FileWriteFrames(){
char *filename = "E:\\openCV\\zhang\\yuvSource\\football_cif.yuv";
ifstream readMe(filename, ios::in | ios::binary); // 打开并读yuv数据
IplImage *image, *rgbimg, *yimg, *uimg, *vimg, *uuimg, *vvimg;
cvNamedWindow("yuv",CV_WINDOW_AUTOSIZE);
rgbimg = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 3);
image = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 3);
yimg = cvCreateImageHeader(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 1); // 亮度分量
uimg = cvCreateImageHeader(cvSize(ISizeX/2, ISizeY/2), IPL_DEPTH_8U, 1); // 这两个都是色度分量
vimg = cvCreateImageHeader(cvSize(ISizeX/2, ISizeY/2), IPL_DEPTH_8U, 1);
uuimg = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 1);
vvimg = cvCreateImage(cvSize(ISizeX, ISizeY), IPL_DEPTH_8U, 1);
int nframes;
for(nframes = 0; nframes < FCount; nframes ++){
char nframesstr[20];
readMe.read((char*)Y[nframes],ISizeX*ISizeY);
//readMe.seekg(-ISizeX*ISizeY, ios::cur);
//readMe.read((char*)buf[nframes],ISizeX*ISizeY+ISizeX/2*ISizeY/2+ISizeX/2*ISizeY/2);
readMe.read((char*)buf[nframes],ISizeX/2*ISizeY/2);
readMe.read((char*)buf2[nframes],ISizeX/2*ISizeY/2);
cvSetData(yimg,Y[nframes],ISizeX);
//cvSetData(uimg,buf[nframes] + ISizeX*ISizeY, ISizeX/2);
cvSetData(uimg,buf[nframes], ISizeX/2);
cvSetData(vimg,buf2[nframes], ISizeX/2);
cvResize(uimg,uuimg, CV_INTER_LINEAR);
cvResize(vimg,vvimg, CV_INTER_LINEAR);
cvMerge(yimg,uuimg,vvimg,NULL,image); // 合并单通道为三通道
cvCvtColor(image,rgbimg,CV_YUV2BGR);
stringstream ss; // 类型转换统一转换为char* 类型
ss << nframes;
ss << ".jpg" ;
ss >> nframesstr;
cvShowImage("yuv", rgbimg);
cvSaveImage(nframesstr,rgbimg);
int c = cvWaitKey(30);
if((char)c == 27)
{
break;
}
}
readMe.close();
cvReleaseImage(&uuimg);
cvReleaseImage(&vvimg);
cvReleaseImageHeader(&yimg);
cvReleaseImageHeader(&uimg);
cvReleaseImageHeader(&vimg);
cvReleaseImage(&image);
cvDestroyWindow("yuv");
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询