Interpreting cv2.imencode result

Viewed 30354

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

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