上一篇
Python图像处理教程:使用skimage提取图像特征 | 实用指南
- Python
- 2025-07-27
- 1328
Python图像处理教程:使用skimage提取图像特征
从基础到实战,掌握图像特征提取的核心技术
什么是skimage?
scikit-image(简称skimage)是一个基于Python的开源图像处理库,它构建在SciPy之上,提供了一系列高效的图像处理算法。skimage特别适合用于:
- 图像预处理和增强
- 特征检测与提取
- 图像分割
- 图像恢复与去噪
- 几何变换
与其他图像处理库相比,skimage具有API简洁、算法丰富、文档完善等优势,是计算机视觉和图像分析领域的理想工具。
安装与环境配置
使用pip可以轻松安装skimage及其依赖:
pip install scikit-image numpy matplotlib
安装完成后,可以通过以下方式导入主要模块:
from skimage import io, color, feature, filters, transform, morphology import matplotlib.pyplot as plt import numpy as np
图像基础操作
1. 读取和显示图像
# 读取图像
image = io.imread('image.jpg')
# 显示图像
plt.figure(figsize=(8, 6))
plt.imshow(image)
plt.axis('off')
plt.title('原始图像')
plt.show()
2. 转换为灰度图像
许多图像处理算法需要先将彩色图像转换为灰度图像:
gray_image = color.rgb2gray(image)
plt.figure(figsize=(8, 6))
plt.imshow(gray_image, cmap='gray')
plt.axis('off')
plt.title('灰度图像')
plt.show()
图像特征提取技术
1. 边缘检测
边缘检测是识别图像中物体边界的基本技术:
# 使用Sobel算子
edges_sobel = filters.sobel(gray_image)
# 使用Canny算法(更高级)
edges_canny = feature.canny(gray_image, sigma=1.0)
# 显示结果
fig, axes = plt.subplots(1, 2, figsize=(12, 6))
axes[0].imshow(edges_sobel, cmap='gray')
axes[0].set_title('Sobel边缘检测')
axes[0].axis('off')
axes[1].imshow(edges_canny, cmap='gray')
axes[1].set_title('Canny边缘检测')
axes[1].axis('off')
plt.show()
2. 角点检测
角点是图像中两个边缘相交的点,是重要的图像特征:
from skimage.feature import corner_harris, corner_peaks
# 计算Harris角点响应
coords = corner_peaks(corner_harris(gray_image), min_distance=5, threshold_rel=0.02)
# 在原始图像上绘制角点
plt.figure(figsize=(8, 6))
plt.imshow(image)
plt.plot(coords[:, 1], coords[:, 0], 'r+', markersize=10)
plt.axis('off')
plt.title('检测到的角点')
plt.show()
3. 特征点检测(ORB)
ORB(Oriented FAST and Rotated BRIEF)是一种高效的特征检测和描述算法:
from skimage.feature import ORB, match_descriptors
# 初始化ORB检测器
orb = ORB(n_keypoints=100)
# 检测和描述特征
orb.detect_and_extract(gray_image)
keypoints = orb.keypoints
descriptors = orb.descriptors
# 在图像上绘制关键点
plt.figure(figsize=(8, 6))
plt.imshow(image)
plt.scatter(keypoints[:, 1], keypoints[:, 0], s=30, facecolors='none', edgecolors='r')
plt.axis('off')
plt.title('ORB特征点')
plt.show()
高级图像分析
1. 图像分割
使用分水岭算法进行图像分割:
from skimage.segmentation import watershed
from skimage.feature import peak_local_max
from scipy import ndimage as ndi
# 计算距离变换
distance = ndi.distance_transform_edt(gray_image)
# 寻找峰值
coords = peak_local_max(distance, footprint=np.ones((3, 3)), labels=gray_image)
mask = np.zeros(distance.shape, dtype=bool)
mask[tuple(coords.T)] = True
markers, _ = ndi.label(mask)
# 应用分水岭算法
labels = watershed(-distance, markers, mask=gray_image)
# 显示结果
plt.figure(figsize=(8, 6))
plt.imshow(color.label2rgb(labels, image=image, alpha=0.5))
plt.axis('off')
plt.title('分水岭分割结果')
plt.show()
2. 纹理特征提取
使用局部二值模式(LBP)提取纹理特征:
from skimage.feature import local_binary_pattern
# 计算LBP
radius = 3
n_points = 8 * radius
lbp = local_binary_pattern(gray_image, n_points, radius, method='uniform')
# 显示结果
plt.figure(figsize=(10, 8))
plt.subplot(121)
plt.imshow(gray_image, cmap='gray')
plt.title('原始图像')
plt.axis('off')
plt.subplot(122)
plt.imshow(lbp, cmap='viridis')
plt.title('局部二值模式')
plt.axis('off')
plt.show()
实际应用案例
物体识别流程
结合多种技术实现简单物体识别:
def object_recognition(image_path):
# 1. 读取图像
image = io.imread(image_path)
gray = color.rgb2gray(image)
# 2. 预处理 - 高斯模糊去噪
blurred = filters.gaussian(gray, sigma=1)
# 3. 边缘检测
edges = feature.canny(blurred, sigma=1.5)
# 4. 形态学操作 - 填充孔洞
filled = ndi.binary_fill_holes(edges)
# 5. 标记连通区域
labeled = morphology.label(filled)
# 6. 提取区域属性
regions = measure.regionprops(labeled)
# 7. 过滤小区域
min_area = 500
large_regions = [r for r in regions if r.area > min_area]
# 在原始图像上绘制边界框
fig, ax = plt.subplots(figsize=(10, 8))
ax.imshow(image)
for region in large_regions:
# 绘制矩形框
minr, minc, maxr, maxc = region.bbox
rect = plt.Rectangle((minc, minr), maxc - minc, maxr - minr,
fill=False, edgecolor='red', linewidth=2)
ax.add_patch(rect)
plt.axis('off')
plt.title(f'检测到 {len(large_regions)} 个物体')
plt.show()
# 使用示例
object_recognition('objects.jpg')
总结
skimage是Python生态中功能强大的图像处理库,本教程涵盖了图像处理的核心技术:
图像读取与显示
灰度转换
边缘检测
角点检测
特征点提取
图像分割
纹理分析
掌握这些技术将为计算机视觉、图像分析和机器学习应用奠定坚实基础。
学习资源
- 官方文档:scikit-image.org
- 示例库:官方示例库
- GitHub仓库:scikit-image/scikit-image
- 实用教程:skimage教程合集
本文由ChengJieTui于2025-07-27发表在吾爱品聚,如有疑问,请联系我们。
本文链接:http://pjw.521pj.cn/20256614.html
发表评论