Table Of Contents
Table Of Contents

imdecode

mxnet.image.imdecode(buf, *args, **kwargs)[source]

Decode an image to an NDArray.

Note: imdecode uses OpenCV (not the CV2 Python library). MXNet must have been built with USE_OPENCV=1 for imdecode to work.

Parameters:
  • buf (str/bytes or numpy.ndarray) – Binary image data as string or numpy ndarray.
  • flag (int, optional, default=1) – 1 for three channel color output. 0 for grayscale output.
  • to_rgb (int, optional, default=1) – 1 for RGB formatted output (MXNet default). 0 for BGR formatted output (OpenCV default).
  • out (NDArray, optional) – Output buffer. Use None for automatic allocation.
Returns:

An NDArray containing the image.

Return type:

NDArray

Example

>>> with open("flower.jpg", 'rb') as fp:
...     str_image = fp.read()
...
>>> image = mx.img.imdecode(str_image)
>>> image
<NDArray 224x224x3 @cpu(0)>

Set flag parameter to 0 to get grayscale output

>>> with open("flower.jpg", 'rb') as fp:
...     str_image = fp.read()
...
>>> image = mx.img.imdecode(str_image, flag=0)
>>> image
<NDArray 224x224x1 @cpu(0)>

Set to_rgb parameter to 0 to get output in OpenCV format (BGR)

>>> with open("flower.jpg", 'rb') as fp:
...     str_image = fp.read()
...
>>> image = mx.img.imdecode(str_image, to_rgb=0)
>>> image
<NDArray 224x224x3 @cpu(0)>