During the previous code writing process, each frame in the YUV video must be extracted and saved as an image. There are two common methods on the Internet. The first method is to use opencv to bring cvCvtColor, but this method has a bug and the image obtained will be white. The second method is the formula.
Method 1: opencv comes with cvCvtColor
Note: This method will show an image "Extensive white". The specific reason is that the cvCvtColor function has different protocols on the Internet and is hard to understand.
Code:
Void FileWriteFrames () {char * filename = "E: \ openCV \ zhang \ yuvSource \ football_cif.yuv"; ifstream readMe (filename, ios: in | ios :: binary); // open and read yuv data 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); // brightness component uimg = cvCreateImageHeader (cvSize (ISizeX/2, ISizeY/2), priority, 1 ); // both are color components vimg = cvCreateImageHeader (cvSize (ISizeX/2, ISizeY/2), watermark, 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 ); // merge a single channel into three-channel cvCvtColor (image, rgbimg, CV_YUV2BGR); stringstream ss; // type conversion is uniformly converted to char * type 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 ");}
Method 2: Formula
Code:
Bool YUV420_To_BGR24 (unsigned char * puc_y, unsigned char * puc_u, unsigned char * puc_v, unsigned char * puc_rgb, int width_y, int height_y) {if (! Puc_y |! Puc_u |! Puc_v |! Puc_rgb) {return false;} // initialization variable int baseSize = width_y * height_y; int rgbSize = baseSize * 3; BYTE * rgbData = new BYTE [rgbSize]; memset (rgbData, 0, rgbSize);/* variable Declaration */int temp = 0; BYTE * rData = rgbData; // r component address BYTE * gData = rgbData + baseSize; // g component address BYTE * bData = gData + baseSize; // B component address int uvIndex = 0, yIndex = 0; // YUV-> RGB conversion matrix // double Yuv2Rgb [3] [3] = {1, 0, 1.4022, // 1,-0.3456,-0. 7145, // 1, 1.771, 0}; for (int y = 0; y
Complete code see: http://download.csdn.net/detail/lu597203933/7362687
See blog: http://blog.csdn.net/dreamd1987/article/details/7259479 #