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.193 秒)

Sphinx-Gallery 生成的图库