Attention! This forum will be made read-only by Dec-20. Please migrate to Most of existing active users should've received invitation by e-mail.
Ask Your Question

How to encode jpeg images with same huffman and quantization tables in OpenCV?

asked 2015-01-13 18:07:33 -0500

bertmg gravatar image

updated 2015-01-14 01:54:26 -0500

Hi, I'm pretty new to opencv and image processing, so please forgive me if im asking something really silly.

I would like to know if its possible to tell the encoder to use certain huffman table and quantization table when encoding jpeg, so that I can encode multiple images (of same size, component, etc.) with their headers interchangeable.

Basically I'm trying to send a image block by block after splitting it first. And it would be very convenient if they all share the same header so i dont need to send them multiple times. I assume that as long as I can specify the Huffman and Quantization tables they use, they would end up with the same header?

  • Edit: I cant simply put the images in a single Mat and encode them as 1 big image, because whether or not to send the next one depends on the result of the previous image sent. So I have to send them separately.
edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2015-01-13 22:08:28 -0500

Haris gravatar image

One way could be,

  • Load images to Mat which give you continuous BGR pixel array,
  • Append all Mat together to form a big Mat, use Mat::push_back().
  • Encode the final Mat using imencode() and send it.
  • At the receiver side use imdecode() to get the pixels in BGR order.
  • Use ROI to split to images(as you already know image height).

Here is an example

Mat img1=imread("1.png",CV_LOAD_IMAGE_COLOR);
Mat img2=imread("2.png",CV_LOAD_IMAGE_COLOR);
Mat combined=img1.clone();

vector<uchar> buf1,buf2,buf3;
vector<int> param = vector<int>(2);
param[1]=100;//default(95) 0-100

imencode(".jpg",img1,buf1 ,param);
imencode(".jpg",img2,buf2 ,param);
imencode(".jpg",combined,buf3 ,param);

Mat decoded = imdecode(Mat(buf),CV_LOAD_IMAGE_COLOR);
edit flag offensive delete link more



thanks for the reply. but there is a slight complication that I cannot send both image at the same time. For example, img 1 is sent first, and whether or not to send img 2 depends on the receiver's condition. The same pattern continues to img 3, 4, etc.

bertmg gravatar imagebertmg ( 2015-01-13 22:44:37 -0500 )edit

Then you may aware of jpg header, and use imencode() for getting encoded byte array, then separate header,send..etc...

Haris gravatar imageHaris ( 2015-01-14 02:46:26 -0500 )edit

sorry i didnt quite get what u mean. Do u mean imencode on the combined image or separately? how do i encode them with same header? thx

bertmg gravatar imagebertmg ( 2015-01-14 09:38:45 -0500 )edit

I meant imencode for each image, and if you are able to identify the header from encoded byte array(and if it identical to all next images), you can skip the header for other images.

Haris gravatar imageHaris ( 2015-01-14 11:39:43 -0500 )edit

thanks for the help. Turns out their headers are already the same as long as i dont change the encoding parameters.

bertmg gravatar imagebertmg ( 2015-01-14 18:53:42 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-01-13 18:07:33 -0500

Seen: 2,372 times

Last updated: Jan 14 '15