Images as Numpy Arrays
To understand image processing intuitively, it is important to recognize that images read and processed with opencv are saved as numpy data types. The numpy library offers a plethora of utility methods and functions for array and matrix operations. A few examples of these are cropping, splitting and merging images.
import cv2
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
%config InlineBackend.figure_format = 'retina'
Reading and visualizing a sample image
# reading the image
dog = cv2.imread('dog.jpg', cv2.IMREAD_COLOR)
dog = cv2.cvtColor(dog, cv2.COLOR_)
# plotting the image
plt.figure(figsize=(9,7))
plt.imshow(dog)
The image of the dog above is stored as a $numpy.ndarray$ object. Because it is numpy array, attributes and methods like shape and index processing can be applied to the image for manipulation. A basic example of this is calling the shape method on the image to return it's dimensions.
type(dog), dog.shape
(numpy.ndarray, (7360, 4912, 3))
Notice that the image above is 7360 pixels high (height dimension), 4912 pixels wide (width) with 3 channels (Red, Blue, Green). Knowing the dimension of the object is helpful in more complex operations such as cropping, splitting e.t.c