0

How would I go about converting an image in YUV colorspace to a JPEG image?

I have a raw image data saved in a char* variable:

char* frame = (char*)camera->getFrame(); // YUV colorspace image data

I need to convert this to a JPEG image data instead. I don't want to save it to disk because I will be sending it in a stream.

Dima
  • 38,860
  • 14
  • 75
  • 115
Richard Knop
  • 81,041
  • 149
  • 392
  • 552

2 Answers2

1

OpenCV itself does not export this functionality. Cleanest is to use libjpeg for encoding. See the answers to these questions:

Community
  • 1
  • 1
ypnos
  • 50,202
  • 14
  • 95
  • 141
  • Well, that's not good. I need to compress images before streaming them on a system with closed embedded linux. It only has OpenCV and I cannot install any additional libraries. Is there some other way how to compress images before streaming them so they are lighter in size? I need to save badwidth. Is using gzip a good idea? – Richard Knop Nov 18 '10 at 20:13
  • In the accepted answer of my second link, you will find that OpenCV internally has functions for encoding to JPEG without writing to a file. It is just that in newer versions of OpenCV they might change without further notice, as they are not part of the API. If you're stuck on a closed system with specific library setting, you may not care for that anyway. Furthermore, the zip algorithm will always be helpful but you should not expect a reduction of more than 50% with it.. – ypnos Nov 18 '10 at 22:35
1

Check the opencv src for the file cvcolor.cpp. This has all the color conversions in it.

I suggest you modify the existing routines near this line:

/* BGR/RGB -> YCrCb */

They are almost exactly what you need for YUV encoding. if its 4:4:4 and not 4:2:2 or 4:1:1

for jpg compression

  • The jpg encoder and decoder are in grfmt_jpeg.cpp which happens to #include "jpeglib.h"

  • You can call these directly

Neon22
  • 590
  • 5
  • 17