import cv2
%matplotlib notebook
from matplotlib import pyplot as plt
imread
under Python returns a NumPy arrayshape
atribute keeps the array's dimensionsIn the OpenCV’s Python wrapper, the imread
function returns an image as a NumPy array. The array dimensions can be read from the shape
attribute:
lenna = cv2.imread('data/lenna.tiff', cv2.IMREAD_GRAYSCALE)
lenna.shape
uint8
arraysGrayscale images are commonly represented by 2D arrays of 8 bits unsigned integers, corresponding to values from 0 ("black") to 255 ("white"). In NumPy, this data type (dtype
) is named uint8
.
lenna.dtype
lenna
lenna[0,0]
plt.imshow(lenna, cmap=plt.cm.gray)
plt.colorbar()
uint8
arrayColor images in RGB are commonly represented by 24 bits, 8 bits for each one of the three channels (red, green and blue). In NumPy, a $M \times N$ color image can be represented by a $M \times N \times 3$ uint8
array.
mandrill = cv2.imread('data/mandrill.tiff')
mandrill.shape
mandrill.dtype
mandrill[2,3]
imread
returns color images in BGR orderThe output of the last command above shows the value at pixel $(2, 3)$ is 29, 46, 54. The image was loaded by OpenCV using the imread
procedure. OpenCV loads color images in BGR
order, so 29, 46 and 54 correspond to the values of blue, green and red respectively. The
triplet is returned as a 3-d vector (a unidimensional array). Direct access to the color value can be done indexing the color dimension - mandrill[2,3,1]
returns the green channel
value, 46.
mandrill[2,3,1]
plt.imshow(mandrill)