In [1]:
import cv2

%matplotlib notebook
from matplotlib import pyplot as plt
/usr/local/lib/python2.7/dist-packages/matplotlib/ 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.')


  • 2D and 3D publication quality plotting library
  • It provides a large set of plotting tools
    • Histograms
    • Bar plots
    • Scatter plots
    • Vector fields

Matplotlib is a 2D plotting aimed to interactive computing and publication-quality image generation. It is a more powerful tool to display images than the standard OpenCV utilities. Interactive zooming, interpolation, automatic scaling and floating point array visualization are Matplotlib features that are not available in OpenCV. It also provides a large set of plotting tools, similar to R and Matlab, including line plots, scatter plots, bar plots, histograms and vector.

Matplotlib’s maintainers keep a gallery of plotting examples. Users can pick the desired plot from the gallery and inspect its source code, using it as a template or starting point for their own graphics.

OpenCV imshow vs. Matplotlib imshow

  • OpenCV imshow functions presents very limited capabilities
    • It can display 8 bits unsigned integer images
    • In general, it's unable to provide visualization for arrays presenting different data types
  • Matplotlib imshow is a more capable visualization tool
    • It is able to visualize arrays of different types
      • Provides different color mapping alternatives
    • It presents interactive zooming
    • Automatic scaling for large or small images
      • Different interpolation methods available

OpenCV presents an imshow function that lets the user display an image in a graphic window. It can show standard 8 bits color or grayscale images, but different data types need adaptions. For 16 bits and 32 bits integers, the pixels values are divided by 256, mapping a $[0, 65280]$ range to $[0, 255]$ before displaying. For 32 bits floating-point images, values are multiplied by 255, what maps the value range $[0, 1]$ to $[0, 255]$. Zooming is not supported and high-resolution images are not be fitted to the computer’s screen, making the visualization of large images inconvenient. Remote sensing data or high-resolution photographies usually needs some sort of scaling before displaying.

Matplotlib presents its own imshow function, able to display NumPy arrays as images. Differently of the OpenCV counterpart, this function is a more capable tool for scientific visualization. Images are fitted to the plotting window according to different interpolation methods (bilinear, bicubic, sinc and Lanczos, to name just a few). Interactive zooming is available, a convenient feature when the user is inspecting the results of image processing routines. Matplotlib is able to show color images in the form of $M \times N \times 3$ RGB arrays (floating point or integer data types) or $M \times N \times 4$ RGBA arrays presenting an alpha channel. Bidimensional $M \times N$ arrays (as integer or floating point grayscale images) are exhibited using a colormap automatically fitted to the array range. The colorbar function can be used to plot a bar that illustrates the color/value mapping employed.

Color image visualization

In [2]:
lenna = cv2.cvtColor(cv2.imread('data/lenna.tiff'), cv2.COLOR_BGR2RGB)
<matplotlib.image.AxesImage at 0x7fe1c82c9b50>

Grayscale or single channel images

Bilinear interpolation (default), cm.jet colormap (default):

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