Ask Your Question
0

YUYV422 to BGR

asked 2016-02-08 23:55:16 -0600

Bilityuk gravatar image

updated 2016-02-09 00:19:05 -0600

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!

edit retag flag offensive close merge delete

3 answers

Sort by » oldest newest most voted
4

answered 2016-02-09 04:10:02 -0600

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 );
edit flag offensive delete link more

Comments

Thank you it works for me

Bilityuk gravatar imageBilityuk ( 2016-02-09 21:28:29 -0600 )edit
1

answered 2019-07-03 07:30:12 -0600

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;
}
edit flag offensive delete link more
1

answered 2018-11-06 13:23:01 -0600

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...

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2016-02-08 23:55:16 -0600

Seen: 22,207 times

Last updated: Jul 03 '19