Python数字图像处理:边缘轮廓检测方法与实操

周末不加班
周末不加班
发布时间:2026-10-12| 2人浏览过
原创

本文介绍如何使用Python的scikit-image库进行数字图像的边缘轮廓检测,包括find_contours函数和多项式逼近算法,提供代码示例和结果分析。

轮廓检测基础

在数字图像处理中,边缘轮廓检测用于识别图像中物体的边界,常用于分割和特征提取。scikit-image的measure模块提供了多种工具,其中find_contours适用于二值图像,而approximate_polygon可对复杂轮廓进行简化。

第1步:使用find_contours检测二值图像轮廓

目的:从二值图像中提取所有闭合或开放轮廓。

入口:调用skimage.measure.find_contours()函数。

动作:将灰度图像转换为二值数组后,传入level参数设定阈值,函数返回轮廓坐标列表。

参数:array为输入的二值图像数组,level为轮廓检测阈值(通常设为0.5)。

完成状态:成功获取contours集合,每个元素包含轮廓的(x, y)坐标序列,可通过循环绘制验证。

以下示例演示基本用法:

import numpy as np
import matplotlib.pyplot as plt
from skimage import measure, draw

# 生成二值测试图像
img = np.zeros([100, 100])
img[20:40, 60:80] = 1  # 矩形
rr, cc = draw.circle(60, 60, 10)  # 小圆
rr1, cc1 = draw.circle(20, 30, 15)  # 大圆
img[rr, cc] = 1
img[rr1, cc1] = 1

# 检测所有图形的轮廓
contours = measure.find_contours(img, 0.5)

# 绘制轮廓
fig, (ax0, ax1) = plt.subplots(1, 2, figsize=(8, 8))
ax0.imshow(img, cmap='gray')
ax1.imshow(img, cmap='gray')
for n, contour in enumerate(contours):
    ax1.plot(contour[:, 1], contour[:, 0], linewidth=2)
ax1.axis('image')
ax1.set_xticks([])
ax1.set_yticks([])
plt.show()
使用find_contours检测不同图形轮廓的效果,轮廓以不同颜色区分
示例1结果:find_contours检测出的轮廓以彩色线条标示在原图上

第2步:通过approximate_polygon简化多边形曲线

目的:对提取的复杂轮廓进行降采样,减少冗余点,提高后续处理效率。

入口:使用skimage.measure.approximate_polygon()函数。

动作:基于Douglas-Peucker算法,输入原始坐标点和容忍值,返回简化后的多边形坐标序列。

参数:coords为轮廓坐标点数组,tolerance为近似容忍度(越小越精确)。

完成状态:获得更少的坐标点表示同一轮廓,便于存储和分析。

以下示例展示与subdivide_polygon配合使用的流程:

import numpy as np
import matplotlib.pyplot as plt
from skimage import measure

# 定义手形轮廓坐标
hand = np.array([[1.64516129, 1.16145833],
                 [1.64516129, 1.59375],
                 [1.35080645, 1.921875],
                 [1.375, 2.18229167],
                 [1.68548387, 1.9375],
                 [1.60887097, 2.55208333],
                 [1.68548387, 2.69791667],
                 [1.76209677, 2.56770833],
                 [1.83064516, 1.97395833],
                 [1.89516129, 2.75],
                 [1.9516129, 2.84895833],
                 [2.01209677, 2.76041667],
                 [1.99193548, 1.99479167],
                 [2.11290323, 2.63020833],
                 [2.2016129, 2.734375],
                 [2.25403226, 2.60416667],
                 [2.14919355, 1.953125],
                 [2.30645161, 2.36979167],
                 [2.39112903, 2.36979167],
                 [2.41532258, 2.1875],
                 [2.1733871, 1.703125],
                 [2.07782258, 1.16666667]])

# 细分多边形(模拟B样条平滑)
new_hand = hand.copy()
for _ in range(5):
    new_hand = measure.subdivide_polygon(new_hand, degree=2)

# 近似简化多边形
appr_hand = measure.approximate_polygon(new_hand, tolerance=0.02)
print("Number of coordinates:", len(hand), len(new_hand), len(appr_hand))

# 绘制对比
fig, axes = plt.subplots(2, 2, figsize=(9, 8))
ax0, ax1, ax2, ax3 = axes.ravel()
ax0.plot(hand[:, 0], hand[:, 1], 'r', label='original')
ax0.set_title('Original Hand')
ax1.plot(new_hand[:, 0], new_hand[:, 1], 'g', label='subdivided')
ax1.set_title('Subdivided Polygon')
ax2.plot(appr_hand[:, 0], appr_hand[:, 1], 'b', label='approximated')
ax2.set_title('Approximated Polygon')
ax3.plot(hand[:, 0], hand[:, 1], 'r', new_hand[:, 0], new_hand[:, 1], 'g', appr_hand[:, 0], appr_hand[:, 1], 'b')
ax3.legend()
ax3.set_title('All Contours Overlay')
plt.tight_layout()
plt.show()
马图像原始灰度图与find_contours提取的轮廓对比
示例2结果:原始马图像及其轮廓检测结果,清晰显示物体边界
多边形细分与逼近过程的对比图,展示坐标点减少效果
示例3结果:hand轮廓经过细分和近似简化后的坐标变化对比

以上就是Python数字图像处理中边缘轮廓检测的详细内容,更多关于Python图像轮廓提取的资料请关注本站其它相关文章!

相关标签:

免责声明:本文内容仅供参考,不代表本站观点。转载或引用请注明出处。

Python 相关产品和应用

全部下载
Selenium

Selenium

4.51.0 · Windows

用于浏览器自动化和 Web 测试的 Python 工具。

scikit-learn

scikit-learn

1.9.1 · Windows

常用 Python 机器学习算法与模型评估工具库。

Black

Black

26.10.1 · Windows

强调一致性的 Python 自动代码格式化工具。

Ruff

Ruff

0.17.1 · macOS

由 Rust 编写的高速 Python 代码检查和格式化工具。

pytest

pytest

9.1.1 · Windows

简洁而强大的 Python 自动化测试框架。

FastAPI

FastAPI

0.146.0 · Windows

高性能 Python API 框架,支持类型提示和自动接口文档。

Flask

Flask

3.1.3 · Windows

轻量灵活的 Python Web 应用框架。

Django

Django

6.1.2 · Windows

成熟的 Python Web 框架,适合构建内容站点和业务系统。

Spyder

Spyder

6.1.7 · Windows

面向科学计算与数据分析的 Python 集成开发环境。

SciPy

SciPy

1.18.1 · Windows

面向科学、工程和技术计算的 Python 算法库。