# Why 'imencode' taking so long ?

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 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)

( 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 ?

( 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

( 2019-01-18 06:20:14 -0500 )edit
1

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.

( 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(), mat.data ;)

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

( 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 : "mat.data" & 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 ?

( 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 ?

( 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.

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

Sort by » oldest newest most voted

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.

more

Official site

GitHub

Wiki

Documentation