0

How can I convert JPEG images to DCT 8x8 using OpenCv and C++?Like where do I start exactly?

stbb24
  • 11
  • 1
  • 1
  • 6

2 Answers2

1

libjpeg is your friend ...

The following code snippet loads DCT coefficients from a JPEG image (disclaimer: it's been pulled off a four years old project of mine - YMMV):

struct jpeg_decompress_struct srcinfo;
struct jpeg_error_mgr jsrcerr;
jvirt_barray_ptr * src_coef_arrays;
FILE * fp;  

srcinfo.err = jpeg_std_error(&jsrcerr);
jpeg_create_decompress(&srcinfo);
fp = fopen(filename, "rb");
jpeg_stdio_src(&srcinfo, fp);
(void) jpeg_read_header(&srcinfo, TRUE);
src_coef_arrays = jpeg_read_coefficients(&srcinfo);
fclose(fp);

...
// use coefficients
...

(void) jpeg_finish_decompress(&srcinfo);
jpeg_destroy_decompress(&srcinfo);

Accessing the coefficients is a bit hairy - I recommend that you consult the source code of the jpegtran utility that's bundled with libjpeg, and see how it's done there.

ZungBang
  • 409
  • 2
  • 8
0

I don't think there is much sense in what you are trying to accomplish. JPEG image is already full of DCT's. You need to understand first what you are trying to do, OpenCV+C++ only makes messier

Pavel P
  • 15,789
  • 11
  • 79
  • 128
  • I'm trying to watermark a JPEG image. but for me to do that I have to convert the image into smaller blocks of 8x8. And what do you mean by "PEG image is already full of DCT's"? – stbb24 Apr 19 '12 at 08:58
  • I suggest you do some reading - the JPEG wikipedia entry would probably be a good starting point. – ZungBang Apr 19 '12 at 12:59
  • @stbb24: JPEG internally uses DCT on blocks of pixels to compress image data. To add watermark, I'm 95% sure you don't want to deal with that stuff. Use libjpeg (or any other alternative lib), decompress JPEG to regular RGB (or YUV if you are comfortable with it), apply your watermark to the image and compress it. If you are certain that you want to take only certain blocks of DCT, modify them and save them back with your watermark applied, then you'll need deeper understanding of libjpeg. Ask on their mailing list. If such thing is possible, I'm sure you'll get quick solution. – Pavel P Apr 19 '12 at 17:10