Search code examples
c++imagemagickyuvmagick++

How to use Magick++ to convert YUYV to RGB?


Reading images from a device via V4L2. The images are in YUV 4:2:2 format, aka V4L2_PIX_FMT_YUYV, aka YUY2.

What I'd like to do is either convert the blob of bytes to RGB, or better yet how to instantiate a Magick++ Image object and tell it the data is in YUYV instead of RGB24.

Can this be easily done? The Magick++ documentation is bare-bones and provides zero help: http://www.imagemagick.org/api/Magick++/classMagick_1_1Image.html


Solution

  • You can easily convert YUV422 to RGB888. Let data be the image data you load in YUV422 format then:

    u  = data[0];
    y1 = data[1];
    v  = data[2];
    y2 = data[3];
    ...
    ...
    

    and then:

    rgb[0] = yuv2rgb(y1, u, v);
    rgb[1] = yuv2rgb(y2, u, v);
    ...
    ...
    

    using the following formula for yuv2rgb:

    R = Y + 1.140*V
    G = Y - 0.395*U - 0.581*V
    B = Y + 2.032*U