Ask Your Question

Why 'imencode' taking so long ?

asked 2019-01-18 01:06:39 -0500

R.Lal gravatar image

I'm encoding my image from a opencv "Mat" to a "vector" buffer in order to send it over a socket.

int rows = 4096;
int cols = 4096;
vector<uchar> buffer;

Mat imageFrame(rows, cols, CV_8UC1, dataPointer );    
    /* dataPointer --> Pointer containing image pixel data */
imencode(".bmp", imageFrame, buffer);

The "imencode" function takes time in orders of 20-40 ms.

{ Just for the information purpose, the imageFrame Mat is generated in order of 10ms. }

My System Specs are :

Processor : Intel Core i7-6700TE @2.4 GHz (8-CPUs)

RAM : 16GB (DDR4)

Operating System : Windows 10 Pro 64-bit

Bus Speed : 100 MHz

Please tell me how to speed that up.

Furthermore, Is there any other encoding scheme, besides "BitMap", which can be done faster ?

edit retag flag offensive close merge delete


Is there any other encoding scheme, besides "BitMap"

yes, and you probably should choose ".jpg", ".webp" or ".png" for better compression

(no idea about the timing, though, also, since all of this is done from 3rdparty libs, not much hope to change anything)

berak gravatar imageberak ( 2019-01-18 06:04:30 -0500 )edit

Thanks for suggestion. I tried with above schemes as well, but the time taken is even worse ( in order of 200 ms). If there is no way to speed up the encoding practise, then perhaps is there any way to send the image (cv::Mat) over network from c++ & recreate it back to image from Python, without using encoding at all ?

I'm using ZeroMQ for doing the same task, but it takes 50-60ms for whole process [ prime cause, is encoding (>30ms) ]. I want to reduce that time period to maybe around 30ms, if possible.

Any suggestions ?

R.Lal gravatar imageR.Lal ( 2019-01-18 06:16:22 -0500 )edit

i'm just guessing, but the time spent compressing it will be won back, when you try to send it over the wire. it'll also reduce packet fragmentation

berak gravatar imageberak ( 2019-01-18 06:20:14 -0500 )edit

I don't worry about "fragmentation issues", all I worry about is getting minimum time for image-sending & lossless sending of data over network.

Any more ideas or suggestions are most welcomed. I'm open to all other possibilities or libraries, besides ZeroMQ, provided it reduces time.

R.Lal gravatar imageR.Lal ( 2019-01-18 08:06:13 -0500 )edit

if it's a local network, you might as well try to send mat.rows, mat.cols, mat.type(), ;)

what about the image size ? any way you can reduce that ?

berak gravatar imageberak ( 2019-01-18 08:13:50 -0500 )edit

Yeah, it'll be a local network with 10 gigabit Ethernet. My image will always be of fixed size : 4096x4096 pixels with bit-depth = 8, which constitutes the file size of 16 MB for each file (No compression is done, in order to save precious milliseconds of time.)

Will try your suggestion for sending matrix directly as : "" & get back to you with the timings. Just a wild doubt, that at receiver python end, will I able to construct back the image from the matrix directly by the PIL or OpenCV libraries, right ?

R.Lal gravatar imageR.Lal ( 2019-01-18 08:21:48 -0500 )edit

well, you'll receive bytes, and have to recreate a numpy object from that. not a python person here, but it shouldn't be a biggie ;)

and: 4096x4096 -- what on earth do you need that high resolution for ?

berak gravatar imageberak ( 2019-01-18 08:24:56 -0500 )edit

4096x4096x8bit isn't the biggest I've seen.

imencode is meant for long-term storage, where file-size is much more important than speed. You should look at zlib, LZ4, and others. There is a line, below which the space saved reduces transmission time more than you spend compressing it, and you want to be below that. You'll have to measure your actual network throughput to find the proper ratio between time and space, and actual compression rates.

Tetragramm gravatar imageTetragramm ( 2019-01-19 14:24:25 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2019-04-13 04:07:36 -0500

R.Lal gravatar image

I've covered the both the scenarios : here

Conclusions :

1) Since, my network was 10 GigaBit local network, it was more preferable for me to directly send the opencv matrix without using any encode. Moreover, zero quality loss was also one of my requirement.

2) If you have limited network bandwidth, I suggest to use encoding schemes like : 'jpg', 'png', etc.

Time Calculations :

(A) Encoding with bmp : 20-40ms

(B) Encoding with jpg : 50-70 ms

(C) Encoding with png: 200-250ms

So, choose encoding schemes wisely.

Disclaimer : The timings are strictly restricted to my hardware/machine. It may/will vary accordingly on your machines.

edit flag offensive delete link more

Question Tools

1 follower


Asked: 2019-01-18 01:06:39 -0500

Seen: 3,322 times

Last updated: Apr 13 '19