Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Interpreting cv2.imencode result

What is buffer returned by cv2.imencode represent?

Here is example for 1x1 pix image.

import cv2
impor numpy as np

img= np.zeros((1,1,3),np.uint8)
en= cv2.imencode('.jpg',img)
print type(en)
<type 'tuple'>
print en[1].shape
(631, 1)

For some reason size of buffer not changed when the size of image changed:

img= np.zeros((10,10,3),np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(631, 1)

Update: it's have different size for bigger image.

img= np.zeros((1000,1000,3),np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(16503, 1)

With random data:

img= (np.random.rand(1,1,3)*255).astype(np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(634, 1)

img= (np.random.rand(10,10,3)*255).astype(np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(899, 1)

img= (np.random.rand(1000,1000,3)*255).astype(np.uint8)
en= cv2.imencode('.jpg',img)
en[1].shape
(1175962, 1)
like image 274
mrgloom Avatar asked Jan 19 '26 17:01

mrgloom


2 Answers

As per cv2.imencode documentation

Note: cvEncodeImage returns single-row matrix of type CV_8UC1 that contains encoded image as array of bytes.

So basically the output depends upon the image format you define .png, .jpg, etc, each format has it's own serilization conventions and cv2.imencode does precisely that. It also contains some meta-data related to that image format, ex: compression level, etc. along with pixel data.

like image 140
ZdaR Avatar answered Jan 21 '26 08:01

ZdaR


In-memory manipulation is a little less than documented in the introductory tutorials, and most advice online directs you to write to disk and then read from the disk.

Alternately, what you may be trying to do is create an in memory representation for sending to video, or off to a database, or ... disks are not the only places they go.

Your original code creates an in-memory buffer that has not been interpreted by CV2. This is equivalent to imwrite

img= (np.random.rand(1000,1000,3)*255).astype(np.uint8)
en = cv2.imencode('.jpg',img)

When written to disk results in a 1-pixel wide JPEG. To have CV2 correctly interpret the data, you need to decode it (equivalent to imread)

de = cv2.imdecode(en,cv2.IMREAD_GRAYSCALE)

This causes CV2 to correctly interpret the data in such a way as to make it suitable for writing to image files, frames, or video.

cv2.imwrite('test.jpg',de)
video.write(de)
like image 22
Jefferey Cave Avatar answered Jan 21 '26 07:01

Jefferey Cave



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!