Introduction to Python OpenCV¶

The first step to learning OpenCV is understanding the basics. This guide will show you how to open, display, modify and save images. A proper understanding of these building blocks is necessary before moving on to more advanced topics.

Prerequisites¶

1. Python
2. OpenCV
3. Numpy
4. Matplotlib

Let me know if you need help installing these dependencies; I would be happy to help.

Experience with numpy is very useful, but not necessary. If you don't understand any of the syntax I use take a look at this page

A Hello World in OpenCV¶

The most basic thing you can do with OpenCV would probably be opening and displaying an image. It makes a good 'hello world' project because it is easy and provides a great entry point to discussion on how OpenCV works.

Here is the image we will use. I suggest you take a look at it so you know what the correct output should look like.

In [44]:
import cv2
import matplotlib.pyplot as plt
plt.imshow(image)
plt.show()


As you can see this output is wrong. It seems like all the red areas have become blue!

Unlike most modern applications, For historic reasons OpenCV uses the BGR color space as default intead of the more traditional RGB. This means that whenever we mix OpenCV with other libraries (like matplotlib) we need to be careful to convert to the appropriate colorspace as needed.

OpenCV images are 3 dimensional numpy arrays. The dimensions of the array are width, height, color. Since the color space of our image is BGR, the color dimension will contain arrays of size 3. To convert our image to RGB we just need to swap the 0th and 2nd indices of the color dimension. With numpy we can do this in just one line.

Lets try displaying our image again

In [48]:
image = cv2.imread('4.2.03.tiff')
# Switches blue and red components to convert image to RGB
image[:, :, (2, 0)] = image[:, :, (0, 2)]
plt.imshow(image)
plt.show()


We can write a function to make future conversion easier:

In [52]:
def switchRB(image):
image[:, :, (2, 0)] = image[:, :, (0, 2)]
return image


Basic Image Editing¶

Since OCV images are just numpy arrays, editing images is straightforward. Lets say we want to remove the red channel of an image.

In [55]:
rgb_image = switchRB(cv2.imread('4.2.03.tiff'))
rgb_image[:, :, 0] = 0
plt.imshow(rgb_image)
plt.show()


We can convert a color image to grayscale by averaging the values of each pixel.

In [59]:
rgb_image = switchRB(cv2.imread('4.2.03.tiff'))
gray_image = np.mean(rgb_image, axis=2)
# Change cmap to prevent matplotlib from mapping our grayscale values to different colors
plt.imshow(gray_image, cmap='gray')
plt.show()