Ask Your Question
0

YUYV422 to BGR

asked Feb 9 '16

Bilityuk gravatar image

updated Feb 9 '16

Good morning! Can anybody help me to convert between YUYV422 and BGR pixel formats?

1) This way i load the pixel data: cv::Mat cameraFrame = cv::Mat(h, w,CV_8UC3,(char*)YUYV422_data); 
2) i need to convert cv::Mat YUYV422  to cv::Mat BGR 
3) after processing i need to convert cv::Mat BGR to (char*)YUYV422_data.

Thank you!

Preview: (hide)

3 answers

Sort by » oldest newest most voted
4

answered Feb 9 '16

See the function cvtColor with the following (undocumented) flag: COLOR_YUV2RGB_Y422 which is the same as COLOR_YUV2RGB_UYVY. If you need other flags, don't hesitate to have a look at imgproc.hpp which shows all of them.

// Convert from yuv to rgb
cv::cvtColor( yuv, rgb, COLOR_YUV2RGB_UYVY );
// Convert from rgb to yuv
cv::cvtColor( rgb, yuv, COLOR_RGB2YUV );
Preview: (hide)

Comments

Thank you it works for me

Bilityuk gravatar imageBilityuk (Feb 10 '16)edit
1

answered Jul 3 '19

Just for the sake of completion I'm adding this answer.

#include "opencv2/opencv.hpp"

int main(int, char**)
{
    cv::Size szSize(600, 600);
    uchar *b_Buffer = new uchar[szSize.width * szSize.height * 2];
    cv::Mat mSrc(szSize,CV_8UC2, b_Buffer);

    string binFile("dump1.yuv");
    ifstream File_VideoFile;
    File_VideoFile.open(binFile, ios::in | ios::binary);

    if (!File_VideoFile.is_open())
    {
        std::cerr << "[ERROR] cannot open the YUV Input File " << binFile << endl;
        std::cerr << std::endl;
        assert(0);
    }
    File_VideoFile.read((char*)b_Buffer, sizeof(uchar)*szSize.width*szSize.height*2);
    File_VideoFile.close();

    cv::Mat mSrc_BGR(szSize, CV_8UC3);

    cvtColor(mSrc, mSrc_BGR, COLOR_YUV2BGR_YUYV);

    imshow("Image BGR", mSrc_BGR);

    waitKey(0);
    delete[] b_Buffer;
    return 0;
}
Preview: (hide)
1

answered Nov 6 '18

To build on Mathieu Barnachon's earlier answer, looking through opencv2/imgproc.hpp is key. When using webcams, a common format is YUYV 4:2:2. (Apparently in the Windows world that format is known as YUY2.) To convert that format to BGR which can then be used natively in OpenCV, you want to use the ColorConversionCodes enum COLOR_YUV2BGR_YUYV which is actually an alias for COLOR_YUV2BGR_YUY2.

cv::Mat bgr_mat;
cv::cvtColor(camera_mat, bgr_mat, cv::COLOR_YUV2BGR_YUYV);

If you plan on using the mat in OpenCV, remember to convert to BGR, not RGB.

For additional details on the format, look up fourcc YUYVor see https://www.linuxtv.org/downloads/v4l...

Preview: (hide)

Question Tools

1 follower

Stats

Asked: Feb 9 '16

Seen: 22,882 times

Last updated: Jul 03 '19