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)

简介

在数据科学的广阔领域中,图像处理占据了重要的一席之地,为分析和处理视觉数据提供了各种工具和技术。Python 拥有丰富的库生态系统,为图像处理提供了多种选择,其中,scikit-image 凭借其强大且易用的功能脱颖而出。它使用 NumPy 数组作为图像对象,方便与其他科学 Python 库集成。

在这篇文章中,我们将探索如何使用 scikit-image 将图像读取到 NumPy 数组并使用 Matplotlib 进行可视化,通过一个简单的例子来说明基本原理。

scikit-image 入门

Scikit-image 基于 NumPy 和 Matplotlib 构建,非常适合需要操作和分析像素的任务。让我们从读取和显示图像开始。

读取和显示图像

以下是使用 scikit-image 读取和显示图像的方法:

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

# 从 scikit-image 的数据模块加载一个示例图像
img = data.astronaut()

# 显示图像
plt.imshow(img)
plt.show()

这个代码段加载了 scikit-image 的示例图像集中的一个宇航员图像,并使用 Matplotlib 的 imshow 函数显示出来。

探索图像属性

了解图像的属性对于处理任务至关重要。以下是检查图像基本属性(如大小和颜色通道)的方法:

1
2
3
4
5
6
7
8
9
10
# 图像的尺寸:像素(行数,列数)
img_size = img.shape
print('图像大小: \n{} \n'.format(img_size))

# 提取尺寸
dim1, dim2 = img.shape[0], img.shape[1]
num_channels = img.shape[2]

# RGB 彩色图像有三个通道:红色、绿色、蓝色
print('通道数: \n{}'.format(num_channels))

这将输出图像的维度和颜色通道数,对于后续操作(如滤波、调整大小或颜色调整)非常重要。

额外提示

虽然我们为了简便使用了内置图像,scikit-image 还可以通过 io 模块从任何 URL 加载图像,如下所示:

1
2
3
4
from skimage import io

# 从 URL 导入图像
img = io.imread('url_here')

这种灵活性使 scikit-image 成为处理各种图像数据源的得力工具。

结论

Scikit-image 与 Matplotlib 搭配,为 Python 中的图像处理任务提供了一套强大的工具。无论你是希望理解图像数据基础的新手,还是从事复杂图像分析项目的高级用户,scikit-image 都提供了实现这些任务所需的功能和简洁性。

通过学习如何使用这些库操作图像,你可以更深入地理解图像分析技术及其在现实世界中的应用。


🍀后记🍀
博客的关键词集中在编程、算法、机器人、人工智能、数学等等,持续高质量输出中。
🌸唠嗑QQ群兔叽の魔术工房 (942848525)
⭐B站账号白拾ShiroX(活跃于知识区和动画区)
✨GitHub主页YangSierCode000(工程文件)
⛳Discord社区AierLab(人工智能社区)