how to convert an RGB image to numpy array?

Shan picture Shan · Oct 14, 2011 · Viewed 330.6k times · Source

I have an RGB image. I want to convert it to numpy array. I did the following

im = cv.LoadImage("abc.tiff")
a = numpy.asarray(im)

It creates an array with no shape. I assume it is a iplimage object.

Answer

Andrey Kamaev picture Andrey Kamaev · Oct 15, 2011

You can use newer OpenCV python interface (if I'm not mistaken it is available since OpenCV 2.2). It natively uses numpy arrays:

import cv2
im = cv2.imread("abc.tiff",mode='RGB')
print type(im)

result:

<type 'numpy.ndarray'>