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