Ask Your Question
0

Failing to display YUV image correctly

asked 2015-03-10 06:53:41 -0600

Engin gravatar image

updated 2015-03-10 07:35:36 -0600

Hello,

I am writing a program that must read from a 4:2:0 chroma subsampled yuv video file and display it on screen. Program is working but shows wrong colors. And I could not find where error is.

Picture I see:

Picture I see

Picture I must see:

Picture I must see

Unfortunately, I must use OpenCV 2.1 and C API (iplimage...).

My code:

int main()
{
    int iFrameWidth = 640;
    int iFrameHeight = 480;

    FILE *fYUV0 = fopen( "C:\\YUV_Videos\\flamenco2_0.yuv", "rb" );

    char *cFileBuffer0 = new char[ iFrameWidth*iFrameHeight*3/2 ];

    IplImage *iplY420Frame = cvCreateImageHeader( cvSize(iFrameWidth  , iFrameHeight  ), IPL_DEPTH_8U, 1 );
    IplImage *iplU420Frame = cvCreateImageHeader( cvSize(iFrameWidth/2, iFrameHeight/2), IPL_DEPTH_8U, 1 );
    IplImage *iplV420Frame = cvCreateImageHeader( cvSize(iFrameWidth/2, iFrameHeight/2), IPL_DEPTH_8U, 1 );

    IplImage *iplY444Frame = cvCreateImage( cvSize(iFrameWidth, iFrameHeight), IPL_DEPTH_8U, 1 );
    IplImage *iplU444Frame = cvCreateImage( cvSize(iFrameWidth, iFrameHeight), IPL_DEPTH_8U, 1 );
    IplImage *iplV444Frame = cvCreateImage( cvSize(iFrameWidth, iFrameHeight), IPL_DEPTH_8U, 1 );

    IplImage *iplYUV444Frame = cvCreateImage( cvSize(iFrameWidth, iFrameHeight), IPL_DEPTH_8U, 3 );

    IplImage *iplRGBFrame0 = cvCreateImage( cvSize(iFrameWidth, iFrameHeight), IPL_DEPTH_8U, 3 );

    while( fread(cFileBuffer0, 1, iFrameWidth*iFrameHeight*3/2, fYUV0) )
    {
        cvSetData( iplY420Frame, cFileBuffer0, iFrameWidth );
        cvSetData( iplU420Frame, cFileBuffer0 + iFrameWidth*iFrameHeight, iFrameWidth/2 );
        cvSetData( iplV420Frame, cFileBuffer0 + iFrameWidth*iFrameHeight*5/4, iFrameWidth/2 );      
        cvResize( iplY420Frame, iplY444Frame );
        cvResize( iplU420Frame, iplU444Frame );
        cvResize( iplV420Frame, iplY444Frame );
        cvMerge( iplY444Frame, iplU444Frame, iplV444Frame, NULL, iplYUV444Frame );
        cvCvtColor( iplYUV444Frame, iplRGBFrame0, CV_YCrCb2BGR );

        cvNamedWindow( "View0" );
        cvShowImage( "View0", iplRGBFrame0 );
        cvWaitKey( 1000/25 );
    }//end-of-while
    cvDestroyWindow( "View0" );
    return 0;
}//end-of-main

PS: Using CV_YCrCb2RGB instead of CV_YCrCb2BGR as above, does not help too. I still get some artificial vertical lines.

edit retag flag offensive close merge delete

Comments

2

Several things

  • Drop the arcane C-API, OpenCV has moved on to the C++ interface since the 2.x API!
  • Don't use IplImages but rather the Mat elements
  • Did you consider that OpenCV stores images in BGR format instead of RGB format?
  • The new C++ interface can directly read YUV data from an image stream!

And again, stay away from C-interface :)

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-10 07:33:28 -0600 )edit

I know, but I must use C-interface if I can. Because my code will be integrated with an old code. :(

Reason of error is, I think, cvResize(). When I shrink the Y component, merge with U and V, convert, I get correct colors then -but with a small sized image (320x240).

I think I need help with this cvResize() function.

Engin gravatar imageEngin ( 2015-03-10 07:44:38 -0600 )edit

Why not changing the c old code to new c++ code?

thdrksdfthmn gravatar imagethdrksdfthmn ( 2015-03-10 09:01:32 -0600 )edit

Old code is not mine and it is pretty large for me to convert from c to c++.

Engin gravatar imageEngin ( 2015-03-10 12:24:20 -0600 )edit
1

Than why not build a bridge class between new and old code? Really what you are doing now will give you a huge amount of problems to tackle...

StevenPuttemans gravatar imageStevenPuttemans ( 2015-03-11 02:58:28 -0600 )edit

I want to parse uyvy (4:2:2) 10 bit raw data frame as parsed from yuv420.Any one can help me out!?

sagar.patel gravatar imagesagar.patel ( 2018-11-05 04:00:34 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-09-18 06:46:36 -0600

Nina107 gravatar image

Hello bro, you got mistake here: cvResize( iplY420Frame, iplY444Frame ); cvResize( iplU420Frame, iplU444Frame ); cvResize( iplV420Frame, iplY444Frame ); => correct: cvResize( iplY420Frame, iplY444Frame ); cvResize( iplU420Frame, iplU444Frame ); cvResize( iplV420Frame, iplV444Frame ); Thank you for your program it helps me a lot!

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2015-03-10 06:52:18 -0600

Seen: 2,713 times

Last updated: Mar 10 '15