Matplotlib is a widely used plotting library in Python that offers a wide range of functions for creating different types of plots and visualizations. It is a powerful library that enables the creation of static, animated, and interactive visualizations in Python programming language.
Scikit Image with Matplotlib
When it comes to visualizing images in the context of image processing, Matplotlib can be combined with the scikit-image library to achieve various visualization tasks. Generally, the Scikit-image library provides functions like io.imshow() and io.show() to display images, However, we can use Matplotlib”s imshow() function to display images with additional options such as annotations, color maps, axis configuration, and more.
Also, it can be helpful for creating multiple plots in a single figure (nothing but subplots) for comparing different versions of an image or displaying intermediate steps in the image processing workflow.
To set up Matplotlib for use with scikit-image, you need to ensure that both libraries are installed and properly configured. It is recommended to use a package manager such as pip or conda to install Matplotlib. pip is the default package manager for Python, while Conda is a popular choice for managing packages in Anaconda environments.
Installing Matplotlib using pip
To install Matplotlib using pip, just run the below command in your command prompt −
pip install Matplotlib
This will download the Matplotlib package.
Installing Matplotlib using Conda
If you”re using the Anaconda distribution already in your system then you can directly use the conda package manager to install Matplotlib. Following is the command −
conda install matplotlib
After the successful installation, you can directly import the matplotlib.pyplot, and skimage libraries to access the required functionality In your Python script or notebook to perform the image processing tasks.
Below are a few basic Python programs that demonstrate how to use the Matplotlib library along with scikit-image to perform data visualization tasks effectively.
Example 1
The following example demonstrates how to use Matplotlib to display the scikit-image loaded image.
from skimage import io import matplotlib.pyplot as plt # Read an image image_data = io.imread(''Images/logo-w.png'') # Display the image using Matplotlib plt.imshow(image_data) plt.title(''Logo'', fontsize=18)
Output
On executing the above program, you will get the following output −
Example 2
The following example demonstrates how to apply a circular mask to an image using scikit-image and display the original image and the masked image side by side using Matplotlib.
import matplotlib.pyplot as plt from skimage import io import numpy as np # Load the image image_path = ''Images_/Zoo.jpg'' image = io.imread(image_path) image_copy = np.copy(image) # Create circular mask rows, cols, _ = image.shape row, col = np.ogrid[:rows, :cols] center_row, center_col = rows / 2, cols / 2 radius = min(rows, cols) / 2 outer_disk_mask = ((row - center_row)**2 + (col - center_col)**2 > radius**2) # Apply mask to image image[outer_disk_mask] = 0 # Display the original and masked images using Matplotlib fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(10, 5)) axes[0].imshow(image_copy) axes[0].set_title(''Original Image'') axes[0].axis(''off'') axes[1].imshow(image) axes[1].set_title(''Masked Image'') axes[1].axis(''off'') plt.tight_layout() plt.show()
Output
On executing the above program, you will get the following output −