6

I am trying to convert a YUV image to RGB using OpenCV. I am a complete novice at this. I have created a function which takes a YUV image as source and converts it into RGB. It is like this :

void ConvertYUVtoRGBA(const unsigned char *src, unsigned char *dest, int width, int height)
{
    cv::Mat myuv(height + height/2, width, CV_8UC1, &src);
    cv::Mat mrgb(height, width, CV_8UC4, &dest);

    cv::cvtColor(myuv, mrgb, CV_YCrCb2RGB);
    return;
}

Should this work? Do I need to convert the Mat into char* again? I am in a loss and any help will be greatly appreciated.

d1xlord
  • 239
  • 3
  • 4
  • 12
  • look into http://stackoverflow.com/a/2906294/3436942 – jbutler483 Sep 04 '14 at 12:59
  • possible duplicate of [Converting YUV into BGR or RGB in OpenCV](http://stackoverflow.com/questions/7954416/converting-yuv-into-bgr-or-rgb-in-opencv) – jbutler483 Sep 04 '14 at 12:59
  • I have already seen those posts and I was unable to use how it was specified their. Can you point out the problems with my code? – d1xlord Sep 04 '14 at 13:09
  • @d1xlord you have not specified which YUV format you are dealing with. Look at the following to see how many YUVs there are: fourcc.org/yuv.php. And do you really want RGB, you have specified a 4 channel Mat and the your function is called `ConvertYUVtoRGBA` so I assume you meant RGBA? Or perhaps BGRA? – Bull Sep 04 '14 at 14:36
  • @jbutler483 this should not be regarded as a duplicate of `Converting YUV into BGR or RGB in OpenCV`. Firstly I think the data here is in a different format, secondly the accepted answer of that question is, imo, not the best way to do it anyway. – Bull Sep 04 '14 at 14:39

1 Answers1

11

There is not enough detail in your question to give a certain answer but below is my best guess. I'll assume you want RGBA output (not RGB, BGR or BGRA) and that your YUV is yuv420sp (as this is what comes out of an Android camera, and it is consistent with your Mat sizes)

void ConvertYUVtoRGBA(const unsigned char *src, unsigned char *dest, int width, int height)
{
    //cv::Mat myuv(height + height/2, width, CV_8UC1, &src);
    cv::Mat myuv(height + height/2, width, CV_8UC1, src); // pass buffer pointer, not its address
    //cv::Mat mrgb(height, width, CV_8UC4, &dest);
    cv::Mat mrgb(height, width, CV_8UC4, dest);

    //cv::cvtColor(myuv, mrgb, CV_YCrCb2RGB);
    cv::cvtColor(myuv, mrgb, CV_YUV2RGBA_NV21);  // are you sure you don't want BGRA?
    return;
}

Do I need to convert the Mat into char again?*

No the Mat mrgb is a wrapper around dest and, the way you have arranged it, the RGBA data will written directly into the dest buffer.

Bull
  • 11,771
  • 9
  • 42
  • 53
  • Thanks a lot. Actually I had to send &src when creating Mat, else it wasn't compiling. I also had to change it BGRA(as you suggested) and use I420 instead of NV21. Thanks a lot for you answers once again. :D – d1xlord Sep 05 '14 at 05:46
  • This will only work properly if the YUV source is SD. It might look right with a basic visual inspection but the colors aren't actually correct. Unfortunately this is because the latest OpenCV source right now (2.4.10) for YUV conversion only supports the 601 colorspace of PAL/NTSC/SECAM and not 709 of HD or 2020 of UHD. – G Huxley Mar 24 '15 at 22:38
  • Hi, its a bit late but, how do you call this method? I have a byte array as src, the width and the height. How do i call it? I would appreciate any help. Thx – Ivan Oct 10 '17 at 08:03