Introduction

In the vast field of data science, image processing has carved out a significant niche, providing tools and techniques to analyze and manipulate visual data. Python, with its rich ecosystem of libraries, offers several options for working with images. Among these, scikit-image stands out as a powerful, accessible tool dedicated to image processing, using NumPy arrays as image objects for easy integration with other scientific Python libraries.

In this post, we’ll explore how to use scikit-image to read images into NumPy arrays and visualize them using Matplotlib, illustrating the basics with a simple example.

Getting Started with scikit-image

Scikit-image is built on NumPy and Matplotlib, making it a great choice for tasks that require manipulation and analysis of pixels. Let’s start by reading and displaying an image.

Reading and Displaying an Image

Here’s how you can read and display an image with scikit-image:

1
2
3
4
5
6
7
8
9
from skimage import data
import matplotlib.pyplot as plt

# Load an example image from scikit-image's data module
img = data.astronaut()

# Display the image
plt.imshow(img)
plt.show()

This code snippet loads an example image (an astronaut) from scikit-image’s extensive collection of demo images and displays it using Matplotlib’s imshow function.

Exploring Image Properties

Understanding an image’s properties is essential for processing tasks. Here’s how you can examine basic properties like size and color channels:

1
2
3
4
5
6
7
8
9
10
# Dimension of image: pixels in (rows, columns)
img_size = img.shape
print('Size of image: \n{} \n'.format(img_size))

# Extract dimensions
dim1, dim2 = img.shape[0], img.shape[1]
num_channels = img.shape[2]

# RGB Colour image has three channels: Red, Green, Blue
print('No. of channels: \n{}'.format(num_channels))

This will output the dimensions of the image and the number of color channels, which is crucial for further manipulation, such as filtering, resizing, or color adjustments.

Additional Tips

While we used a built-in image for simplicity, scikit-image can load images from any URL using the io module, as shown below:

1
2
3
4
from skimage import io

# Importing an image from a URL
img = io.imread('url_here')

This flexibility makes scikit-image a handy tool for working with a vast range of image data sources.

Conclusion

Scikit-image paired with Matplotlib provides a robust set of tools for image processing tasks in Python. Whether you’re a beginner looking to understand the basics of image data or an advanced user working on complex image analysis projects, scikit-image offers the functions and simplicity required to accomplish these tasks efficiently.

By learning how to manipulate images using these libraries, you can unlock a deeper understanding of image analysis techniques and their applications in real-world scenarios.


🍀Afterword🍀
The blog focuses on programming, algorithms, robotics, artificial intelligence, mathematics, etc., with a continuous output of high quality.
🌸Chat QQ Group: Rabbit’s Magic Workshop (942848525)
⭐Bilibili Account: 白拾ShiroX (Active in the knowledge and animation zones)
✨GitHub Page: YangSierCode000 (Engineering files)
⛳Discord Community: AierLab (Artificial Intelligence community)