Question

I have a buffer which contains an image in YV12 format. Now I want to either convert this buffer to RGB format or create a Mat object from it directly! Can someone help me? I tried this code :

cv::Mat input(widthOfImg, heightOfImg, CV_8UC1, vy12Buffer);
cv::Mat converted;
cv::cvtColor(input, converted, CV_YUV2RGB_YV12);
Was it helpful?

Solution

That's possible.

    cv::Mat picYV12 = cv::Mat(nHeight * 3/2, nWidth, CV_8UC1, yv12DataBuffer);
    cv::Mat picBGR;
    cv::cvtColor(picYV12, picBGR, CV_YUV2BGR_YV12);
    cv::imwrite("test.bmp", picBGR);  //only for test

Opencv color conversion flags

The height is multiplied by 3/2 because there are 4 Y samples, and 1 U and 1 V sample stored for every 2x2 square of pixels. This results in a byte sample to pixel ratio of 3/2

4*1+1+1 samples per 2*2 pixels = 6/4 = 3/2

YV12 Format

Correction: In the last version of OpenCV (i use oldest 2.4.13 version) is color conversion code changed to COLOR_YUV2BGR_YV12

    cv::cvtColor(picYV12, picBGR, COLOR_YUV2BGR_YV12);

OTHER TIPS

here is the corresponding version in java (Android)...

This method was faster than other techniques like renderscript or opengl(glReadPixels) for getting bitmap from yuv12/i420 data stream (tested with webrtc i420 ).

long startTimei = SystemClock.uptimeMillis();

Mat picyv12 = new Mat(768,512,CV_8UC1);  //(im_height*3/2,im_width), should be even no...
picyv12.put(0,0,return_buff); // buffer - byte array with i420 data
Imgproc.cvtColor(picyv12,picyv12,COLOR_YUV2RGB_YV12);// or use COLOR_YUV2BGR_YV12 depending on output result

long endTimei = SystemClock.uptimeMillis();
Log.d("i420_time", Long.toString(endTimei - startTimei));

Log.d("picyv12_size", picyv12.size().toString()); // Check size
Log.d("picyv12_type", String.valueOf(picyv12.type())); // Check type

Utils.matToBitmap(picyv12,tbmp2); // Convert mat to bitmap (height, width) i.e (512,512) - ARGB_888

save(tbmp2,"itest"); // Save bitmap

That's impossible.

Y'UV420p is a planar format, meaning that the Y', U, and V values are grouped together instead of interspersed. The reason for this is that by grouping the U and V values together, the image becomes much more compressible. When given an array of an image in the Y'UV420p format, all the Y' values come first, followed by all the U values, followed finally by all the V values.

but cv::Mat is a RGB color model, and arranged like B0 G0 R0 B1 G1 R1... So,we can't create a Mat object from a YV12 buffer directly.

Here is an example:

cv::Mat Yv12ToRgb( uchar *pBuffer,long bufferSize, int width,int height )
{
    cv::Mat result(height,width,CV_8UC3);
    uchar y,cb,cr;

    long ySize=width*height;
    long uSize;
    uSize=ySize>>2;

    assert(bufferSize==ySize+uSize*2);

    uchar *output=result.data;
    uchar *pY=pBuffer;
    uchar *pU=pY+ySize;
    uchar *pV=pU+uSize;

    uchar r,g,b;
    for (int i=0;i<uSize;++i)
    {
        for(int j=0;j<4;++j)
        {
            y=pY[i*4+j];
            cb=ucharpU[i];
            cr=ucharpV[i];

                //ITU-R standard
            b=saturate_cast<uchar>(y+1.772*(cb-128));
            g=saturate_cast<uchar>(y-0.344*(cb-128)-0.714*(cr-128));
            r=saturate_cast<uchar>(y+1.402*(cr-128));

            *output++=b;
            *output++=g;
            *output++=r;
        }
    }
    return result;
}

You can try as YUV_I420 array

char filePath[3000];
int width, height;
cout << "file path = ";
cin >> filePath;
cout << "width = ";
cin >> width;
cout << "height = ";
cin >> height;

FILE *pFile = fopen(filePath, "rb");
unsigned char* buff = new unsigned char[width * height *3 / 2];
fread(buff, 1, width * height* 3 / 2, pFile);
fclose(pFile);


cv::Mat imageRGB;
cv::Mat picI420 = cv::Mat(height * 3 / 2, width, CV_8UC1, buff);
cv::cvtColor(picI420, imageRGB, CV_YUV2BGRA_I420);
imshow("imageRGB", imageRGB);
waitKey(0);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top