以下代码今天早上我刚验证好,提供给诸君分享。
void copy_yuvImage_to_IplImage_color(const unsigned char *input_img,IplImage* out_img)
{
IplImage *image,*yimg,*uimg,*vimg,*uuimg,*vvimg;
unsigned char *ptr_yimg,*ptr_uimg,*ptr_vimg;
int col,row;
int test1;
CvSize frameSize = {WIDTH,HIGHT};
CvSize frameSize_q = {WIDTH/2,HIGHT/2};
image = cvCreateImage(frameSize,IPL_DEPTH_8U,3);
yimg = cvCreateImage(frameSize,IPL_DEPTH_8U,1);
uimg = cvCreateImage(frameSize_q,IPL_DEPTH_8U,1);
vimg = cvCreateImage(frameSize_q,IPL_DEPTH_8U,1);
uuimg = cvCreateImage(frameSize,IPL_DEPTH_8U,1);
vvimg = cvCreateImage(frameSize,IPL_DEPTH_8U,1);
for(row=0;row<HIGHT;row++)
{
ptr_yimg = (unsigned char*)(yimg->imageData + row*yimg->widthStep);
for(col=0;col<WIDTH;col++)
{
*ptr_yimg++ = input_img[row*WIDTH+col];
}
}
for(row=0;row<HIGHT/2;row++)
{
ptr_uimg = (unsigned char*)(uimg->imageData + row*uimg->widthStep);
ptr_vimg = (unsigned char*)(vimg->imageData + row*vimg->widthStep);
for(col=0;col<WIDTH/2;col++)
{
*ptr_uimg++ = input_img[HIGHT*WIDTH + row*WIDTH/2 + col];
*ptr_vimg++ = input_img[(HIGHT*WIDTH+HIGHT*WIDTH/4) + row*WIDTH/2 + col ];
}
}
cvResize(uimg,uuimg,CV_INTER_LINEAR);
cvResize(vimg,vvimg,CV_INTER_LINEAR);
cvMerge(yimg,uuimg,vvimg,NULL,image);
cvCvtColor(image,out_img,CV_YCrCb2BGR);
cvReleaseImage(&image);
cvReleaseImage(&yimg);
cvReleaseImage(&vimg);
cvReleaseImage(&uimg);
cvReleaseImage(&uuimg);
cvReleaseImage(&vvimg);
}