Canny边缘检测器#

Canny滤波器是一个多阶段的边缘检测器。它使用基于高斯导数的滤波器来计算梯度的强度。高斯滤波器减少了图像中噪声的影响。然后,通过移除非最大梯度幅度的像素,将潜在的边缘细化到1像素曲线。最后,通过在梯度幅度上使用滞后阈值处理来保留或移除边缘像素。

Canny 有三个可调参数:高斯宽度(图像噪声越大,宽度越大),以及滞后阈值的低阈值和高阈值。

noisy image, Canny filter, $\sigma=1$, Canny filter, $\sigma=3$
import numpy as np
import matplotlib.pyplot as plt
from scipy import ndimage as ndi
from skimage.util import random_noise
from skimage import feature


# Generate noisy image of a square
image = np.zeros((128, 128), dtype=float)
image[32:-32, 32:-32] = 1

image = ndi.rotate(image, 15, mode='constant')
image = ndi.gaussian_filter(image, 4)
image = random_noise(image, mode='speckle', mean=0.1)

# Compute the Canny filter for two values of sigma
edges1 = feature.canny(image)
edges2 = feature.canny(image, sigma=3)

# display results
fig, ax = plt.subplots(nrows=1, ncols=3, figsize=(8, 3))

ax[0].imshow(image, cmap='gray')
ax[0].set_title('noisy image', fontsize=20)

ax[1].imshow(edges1, cmap='gray')
ax[1].set_title(r'Canny filter, $\sigma=1$', fontsize=20)

ax[2].imshow(edges2, cmap='gray')
ax[2].set_title(r'Canny filter, $\sigma=3$', fontsize=20)

for a in ax:
    a.axis('off')

fig.tight_layout()
plt.show()

脚本总运行时间: (0 分钟 0.089 秒)

由 Sphinx-Gallery 生成的图库