Converting YUV into BGR or RGB in OpenCV

匿名 (未验证) 提交于 2019-12-03 08:57:35

问题:

I have a TV capture card that has a feed coming in as a YUV format. I've seen other posts here similar to this question and attempted to try every possible method stated, but neither of them provided a clear image. At the moment the best results were with the OpenCV cvCvtColor(scr, dst, CV_YUV2BGR) function call.

I am currently unaware of the YUV format and to be honest confuses me a little bit as it looks like it stores 4 channels, but is only 3? I have included an image from the capture card to hope that someone can understand what is possibly going on that I could use to fill in the blanks.

The feed is coming in through a DeckLink Intensity Pro card and being accessed in a C++ application in using OpenCV in a Windows 7 environment.

Update

I have looked at a wikipedia article regarding this information and attempted to use the formula in my application. Below is the code block with the output received from it. Any advice is greatly appreciated.

BYTE* pData;      videoFrame->GetBytes((void**)&pData);      m_nFrames++;      printf("Num Frames executed: %d\n", m_nFrames);      for(int i = 0; i imageData[i] = pData[i] + pData[i+2]*((1 - 0.299)/0.615);         m_RGB->imageData[i+1] = pData[i] - pData[i+1]*((0.114*(1-0.114))/(0.436*0.587)) - pData[i+2]*((0.299*(1 - 0.299))/(0.615*0.587));         m_RGB->imageData[i+2] = pData[i] + pData[i+1]*((1 - 0.114)/0.436);     }

回答1:

It looks to me like you're decoding a YUV422 stream as YUV444. Try this modification to the code you provided:

for(int i = 0, j=0; i imageData[i] = pData[j] + pData[j+3]*((1 - 0.299)/0.615);     m_RGB->imageData[i+1] = pData[j] - pData[j+1]*((0.114*(1-0.114))/(0.436*0.587)) - pData[j+3]*((0.299*(1 - 0.299))/(0.615*0.587));     m_RGB->imageData[i+2] = pData[j] + pData[j+1]*((1 - 0.114)/0.436);     m_RGB->imageData[i+3] = pData[j+2] + pData[j+3]*((1 - 0.299)/0.615);     m_RGB->imageData[i+4] = pData[j+2] - pData[j+1]*((0.114*(1-0.114))/(0.436*0.587)) - pData[j+3]*((0.299*(1 - 0.299))/(0.615*0.587));     m_RGB->imageData[i+5] = pData[j+2] + pData[j+1]*((1 - 0.114)/0.436); }

I'm not sure you've got your constants correct, but at worst your colors will be off - the image should be recognizable.



回答2:

In newer version of OPENCV there is a built in function can be used to do YUV to RGB conversion

cvtColor(src,dst,CV_YUV2BGR_YUY2);

specify the YUV format after the underscore, like this CV_YUYV2BGR_xxxx



回答3:

The BlackMagic Intensity software return YUVY' format in bmdFormat8BitYUV, so 2 sources pixels are compressed into 4bytes - I don't think openCV's cvtColor can handle this.

You can either do it yourself, or just call the Intensity software ConvertFrame() function

edit: Y U V is normally stored as

There is a Y (brightness) for each pixel but only a U and V (colour) for every alternate pixel in the row.

So if data is an unsigned char pointing to the start of the memory as shown above.

pixel 1, Y = data[0] U = data[+1] V = data[+3]
pixel 2, Y = data[+2] U = data[+1] V = data[+3]

Then use the YUV->RGB coefficients you used in your sample code.



回答4:

It may be the wrong path, but many people (I mean, engineers) do mix YUV with YCbCr.

Try to

cvCvtColor(src, dsc, CV_YCbCr2RGB) 

or CV_YCrCb2RGB or maybe a more exotic type.



回答5:

Maybe someone is confused by color models YCbCr and YUV. Opencv does not handle YCbCr. Instead it has YCrCb, and it implemented the same way as YUV in opencv.

From the opencv sources https://github.com/Itseez/opencv/blob/2.4/modules/imgproc/src/color.cpp#L3830:

case CV_BGR2YCrCb: case CV_RGB2YCrCb: case CV_BGR2YUV: case CV_RGB2YUV:     // ...     // 1 if it is BGR, 0 if it is RGB     bidx = code == CV_BGR2YCrCb || code == CV_BGR2YUV ? 0 : 2;      //... converting to YUV with the only difference that brings      //    order of Blue and Red channels (variable bidx)

But there is one more thing to say.
There is currently a bug in conversion CV_BGR2YUV and CV_RGB2YUV in OpenCV branch 2.4.* .

At present, this formula is used in implementation:

Y = 0.299B + 0.587G + 0.114R U = 0.492(R-Y) V = 0.877(B-Y)

What it should be (according to wikipedia):

Y = 0.299R + 0.587G + 0.114B U = 0.492(B-Y) V = 0.877(R-Y)

The channels Red and Blue are misplaced in the implemented formula.

Possible workaround to convert BGR->YUV while the bug is not fixed :

  cv::Mat source = cv::imread(filename, CV_LOAD_IMAGE_COLOR);   cv::Mat yuvSource;       cvtColor(source, yuvSource, cv::COLOR_BGR2RGB); // rearranges B and R in the appropriate order   cvtColor(yuvSource, yuvSource, cv::COLOR_BGR2YUV);   // yuvSource will contain here correct image in YUV color space


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!