Convert yuv to IplImage in opencv

Source: Internet
Author: User

The first is a very stupid method:

The three components of yuv are written in three matrices respectively, and then merged and converted to the image format of the rgb component;

The Code is as follows:

[Html]
IplImage * image, * rgbimg, * yimg, * uimg, * vimg, * uuimg, * vvimg;

Rgbimg = cvCreateImage (cvSize (nWidth, nHeight), IPL_DEPTH_8U, 3 );
Image = cvCreateImage (cvSize (nWidth, nHeight), IPL_DEPTH_8U, 3 );

Yimg = cvCreateImageHeader (cvSize (nWidth, nHeight), IPL_DEPTH_8U, 1 );
Uimg = cvCreateImageHeader (cvSize (nWidth/2, nHeight/2), IPL_DEPTH_8U, 1 );
Vimg = cvCreateImageHeader (cvSize (nWidth/2, nHeight/2), IPL_DEPTH_8U, 1 );

Uuimg = cvCreateImage (cvSize (nWidth, nHeight), IPL_DEPTH_8U, 1 );
Vvimg = cvCreateImage (cvSize (nWidth, nHeight), IPL_DEPTH_8U, 1 );

CvSetData (yimg, pBuf, nWidth );
CvSetData (uimg, pBuf + nWidth * nHeight, nWidth/2 );
CvSetData (vimg, pBuf + long (nWidth * nHeight * 1.25), nWidth/2 );
CvResize (uimg, uuimg, CV_INTER_LINEAR );
CvResize (vimg, vvimg, CV_INTER_LINEAR );

CvMerge (yimg, uuimg, vvimg, NULL, image );
CvCvtColor (image, rgbimg, CV_YCrCb2BGR );

Another method is to compare the load.
First, convert it to the rgb format according to the principle.

Then, use the cvSetData () function to write data to generate an image in IplImage format.

First, define the conversion formula:

[Html]
# Define MR (Y, U, V) (Y + (1.403) * (V-128 ))
# Define MG (Y, U, V) (Y-(0.344) * (U-128)-(0.714) * (V-128 ))
# Define MB (Y, U, V) (Y + (1.773) * (U-128 )))

Yuv to rgb functions:
[Html]
Void YUV420_C_RGB (char * pYUV, unsigned char * pRGB, int height, int width)
{
Char * pY = pYUV;
Char * pU = pYUV + height * width;
Char * pV = pU + (height * width/4 );
 
 
Unsigned char * pBGR = NULL;
Unsigned char R = 0;
Unsigned char G = 0;
Unsigned char B = 0;
Char Y = 0;
Char U = 0;
Char V = 0;
Double tmp = 0;
For (int I = 0; I {
For (int j = 0; j <width; ++ j)
{
PBGR = pRGB + I * width * 3 + j * 3;
 
Y = * (pY + I * width + j );
U = * pU;
V = * pV;
 
// B
Tmp = MB (Y, U, V );
// B = (tmp> 255 )? 255: (char) tmp;
// B = (B <0 )? 0: B;
B = (unsigned char) tmp;
// G
Tmp = MG (Y, U, V );
// G = (tmp & gt; 255 )? 255: (char) tmp;
// G = (G <0 )? 0: G;
G = (unsigned char) tmp;
// R
Tmp = MR (Y, U, V );
// R = (tmp> 255 )? 255: (char) tmp;
// R = (R <0 )? 0: R;
R = (unsigned char) tmp;
 
 
* PBGR = R;
* (PBGR + 1) = G;
* (PBGR + 2) = B;

 
If (I % 2 = 0 & j % 2 = 0)
{
* PU ++;
// * PV ++;
}
Else
{
If (j % 2 = 0)
{
* PV ++;
}
}
}

}
}

The code for writing IplImage is as follows:
[Html]
Unsigned char * pRGB = NULL;
 
PRGB = (unsigned char *) malloc (nSize * sizeof (unsigned char *) * 2 );

YUV420_C_RGB (pBuf, pRGB, nWidth, nHeight );

IplImage * image; www.2cto.com
Image = cvCreateImageHeader (cvSize (nWidth, nHeight), IPL_DEPTH_8U, 3 );
CvSetData (image, pRGB, nWidth * 3 );
All programs run
The compiling environment is vs2008.


Author: xuhongwei0411

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.