1.6. Getting Started with Images

1.6.1. Goals

  • Here, you will learn how to read an image, how to display it and how to save it back

  • You will learn these functions : cv2.imread(), cv2.imshow() , cv2.imwrite()

  • Optionally, you will learn how to display images with Matplotlib

1.6.2. Using Matplotlib

Matplotlib is a plotting library for Python which gives you wide variety of plotting methods. You will see them in coming articles. Here, you will learn how to display image with Matplotlib. You can zoom images, save it etc using Matplotlib.

>>> %matplotlib inline
>>>
>>> import numpy as np
>>>
>>> import cv2
>>>
>>> from matplotlib import pyplot as plt
>>>
>>>
>>> img = cv2.imread('../../cvdata/messi5.jpg',0)
>>>
>>> plt.imshow(img, cmap = 'gray', interpolation = 'bicubic')
>>>
>>> plt.xticks([]), plt.yticks([])  # to hide tick values on X and Y axis
>>>
>>> plt.show()
_images/sec06-view_2_0.png

Plenty of plotting options are available in Matplotlib. Please refer to Matplotlib docs for more details. Some, we will see on the way.

Warning:

Color image loaded by OpenCV is in BGR mode. But Matplotlib displays in RGB mode. So color images will not be displayed correctly in Matplotlib if image is read with OpenCV. Please see the exercises for more details.