# NumPy¶

In [2]:
import cv2

%matplotlib notebook
from matplotlib import pyplot as plt

/usr/local/lib/python2.7/dist-packages/matplotlib/font_manager.py:273: UserWarning: Matplotlib is building the font cache using fc-list. This may take a moment.
warnings.warn('Matplotlib is building the font cache using fc-list. This may take a moment.')


## Images as NumPy arrays¶

• OpenCV imread under Python returns a NumPy array
• The shape atribute keeps the array's dimensions

In 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:

In [4]:
lenna = cv2.imread('data/lenna.tiff', cv2.IMREAD_GRAYSCALE)
lenna.shape

Out[4]:
(512, 512)
• Grayscale images can be represented as uint8 arrays

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

In [5]:
lenna.dtype

Out[5]:
dtype('uint8')
In [6]:
lenna

Out[6]:
array([[162, 162, 162, ..., 170, 155, 128],
[162, 162, 162, ..., 170, 155, 128],
[162, 162, 162, ..., 170, 155, 128],
...,
[ 43,  43,  50, ..., 104, 100,  98],
[ 44,  44,  55, ..., 104, 105, 108],
[ 44,  44,  55, ..., 104, 105, 108]], dtype=uint8)
In [7]:
lenna[0,0]

Out[7]:
162
In [11]:
plt.imshow(lenna, cmap=plt.cm.gray)
plt.colorbar()

Out[11]:
<matplotlib.colorbar.Colorbar at 0x7f1fd469b510>

### Color images¶

• A RGB color image can be represented by a $M \times N \times 3$ uint8 array

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

In [12]:
mandrill = cv2.imread('data/mandrill.tiff')
mandrill.shape

Out[12]:
(512, 512, 3)
In [13]:
mandrill.dtype

Out[13]:
dtype('uint8')
In [14]:
mandrill[2,3]

Out[14]:
array([29, 46, 54], dtype=uint8)
• The color triplet can be retrived indexing the pixel position
• Indexing can be used to retrieve a specifig channel
• OpenCV imread returns color images in BGR order

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

In [15]:
mandrill[2,3,1]

Out[15]:
46
In [17]:
plt.imshow(mandrill)