scipy.ndimage.
sobel#
- scipy.ndimage.sobel(input, axis=-1, output=None, mode='reflect', cval=0.0)[source]#
- 计算 Sobel 滤波器。 - 参数::
- inputarray_like
- 输入数组。 
- axisint, 可选
- input 计算所沿的轴。默认值为 -1。 
- output数组或 dtype,可选
- 放置输出的数组,或返回数组的 dtype。默认情况下将创建与输入具有相同 dtype 的数组。 
- modestr 或序列,可选
- mode 参数决定当过滤器与边界重叠时输入数组如何扩展。通过传递一个长度等于输入数组维数的模式序列,可以在每个轴上指定不同的模式。默认值为 'reflect'。有效值及其行为如下 - ‘reflect’ (d c b a | a b c d | d c b a)
- 通过关于最后一个像素的边缘反射来扩展输入。这种模式有时也被称为半样本对称。 
- ‘constant’ (k k k k | a b c d | k k k k)
- 通过用由 cval 参数定义的相同常数值填充边缘之外的所有值来扩展输入。 
- ‘nearest’ (a a a a | a b c d | d d d d)
- 通过复制最后一个像素来扩展输入。 
- ‘mirror’ (d c b | a b c d | c b a)
- 通过关于最后一个像素中心的反射来扩展输入。这种模式有时也被称为全样本对称。 
- ‘wrap’ (a b c d | a b c d | a b c d)
- 通过环绕到另一侧来扩展输入。 
 - 为了与插值函数保持一致,还可以使用以下模式名称 - ‘grid-constant’
- 这是 'constant' 的同义词。 
- ‘grid-mirror’
- 这是 'reflect' 的同义词。 
- ‘grid-wrap’
- 这是 'wrap' 的同义词。 
 
- cval标量,可选
- 如果 mode 为 'constant',则用于填充输入边缘之外的值。默认值为 0.0。 
 
- 返回值::
- sobelndarray
- 经过滤波的数组。与 input 具有相同的形状。 
 
 - 备注 - 此函数计算轴特定的 Sobel 梯度。水平边缘可以通过水平变换 (axis=0) 来强调,垂直边缘可以通过垂直变换 (axis=1) 来强调,依此类推,适用于更高维度。这些可以组合起来得到幅度。 - 示例 - >>> from scipy import ndimage, datasets >>> import matplotlib.pyplot as plt >>> import numpy as np >>> ascent = datasets.ascent().astype('int32') >>> sobel_h = ndimage.sobel(ascent, 0) # horizontal gradient >>> sobel_v = ndimage.sobel(ascent, 1) # vertical gradient >>> magnitude = np.sqrt(sobel_h**2 + sobel_v**2) >>> magnitude *= 255.0 / np.max(magnitude) # normalization >>> fig, axs = plt.subplots(2, 2, figsize=(8, 8)) >>> plt.gray() # show the filtered result in grayscale >>> axs[0, 0].imshow(ascent) >>> axs[0, 1].imshow(sobel_h) >>> axs[1, 0].imshow(sobel_v) >>> axs[1, 1].imshow(magnitude) >>> titles = ["original", "horizontal", "vertical", "magnitude"] >>> for i, ax in enumerate(axs.ravel()): ... ax.set_title(titles[i]) ... ax.axis("off") >>> plt.show() 