Search code examples
pythonopencvjpeg

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)

Solution

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