Stéphane
Stéphane

Reputation: 20340

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

Upvotes: 0

Views: 1358

Answers (2)

emcconville
emcconville

Reputation: 24419

DimChtz's answer would be the easiest, as the task is straight forward.

ImageMagick does support the formats

     UYVY* rw-   16bit/pixel interleaved YUV
      YUV* rw-   CCIR 601 4:1:1 or 4:2:2

And can be leveraged by something like...

 // Quickly reorder YUYV to UYVY
 unsigned char y1, u, y2, v;
 for ( int i = 0; i < buffer_length; i+=4 ) {
     y1 = buffer[i  ];
     u  = buffer[i+1];
     y2 = buffer[i+2];
     v  = buffer[i+3];
     buffer[i  ] = u;
     buffer[i+1] = y1;
     buffer[i+2] = v;
     buffer[i+3] = y2;
 }
 Magick::Image image;
 Magick::Blob blob( buffer, buffer_length );
 image.size("176x144");
 image.magick("UYVY");
 image.read(blob);
 // ... etc ...

I would suggest jumping over to ImageMagick's ImageMagick Program Interfaces board, and issue a feature request and/or seek guidance.

Upvotes: 1

DimChtz
DimChtz

Reputation: 4323

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

Upvotes: 2

Related Questions