当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python skimage.transform.hough_line用法及代码示例


用法:

skimage.transform.hough_line(image, theta=None)

执行直线霍夫变换。

参数

image(M, N) ndarray

具有表示边的非零值的输入图像。

theta1D ndarray of double,可选

计算变换的角度,以弧度为单位。默认为在 [-pi/2, pi/2) 范围内均匀分布的 180 个角度的向量。

返回

hspaceuint64 的二维数组

霍夫变换累加器。

anglesndarray

计算变换的角度,以弧度为单位。

distancesndarray

距离值。

注意

原点是原始图像的左上角。 X 和 Y 轴分别是水平和垂直边。距离是从原点到检测线的最小代数距离。可以通过减小 theta 阵列中的步长来提高角度精度。

例子

生成测试图像:

>>> img = np.zeros((100, 150), dtype=bool)
>>> img[30, :] = 1
>>> img[:, 65] = 1
>>> img[35:45, 35:50] = 1
>>> for i in range(90):
...     img[i, i] = 1
>>> rng = np.random.default_rng()
>>> img += rng.random(img.shape) > 0.95

应用霍夫变换:

>>> out, angles, d = hough_line(img)
import numpy as np
import matplotlib.pyplot as plt

from skimage.transform import hough_line
from skimage.draw import line

img = np.zeros((100, 150), dtype=bool)
img[30, :] = 1
img[:, 65] = 1
img[35:45, 35:50] = 1
rr, cc = line(60, 130, 80, 10)
img[rr, cc] = 1
rng = np.random.default_rng()
img += rng.random(img.shape) > 0.95

out, angles, d = hough_line(img)

fix, axes = plt.subplots(1, 2, figsize=(7, 4))

axes[0].imshow(img, cmap=plt.cm.gray)
axes[0].set_title('Input image')

angle_step = 0.5 * np.rad2deg(np.diff(angles).mean())
d_step = 0.5 * np.diff(d).mean()
bounds = (np.rad2deg(angles[0]) - angle_step,
          np.rad2deg(angles[-1]) + angle_step,
          d[-1] + d_step, d[0] - d_step)

axes[1].imshow(out, cmap=plt.cm.bone, extent=bounds)
axes[1].set_title('Hough transform')
axes[1].set_xlabel('Angle (degree)')
axes[1].set_ylabel('Distance (pixel)')

plt.tight_layout()
plt.show()

(Source codepngpdf)

hough_tf.png

相关用法


注:本文由纯净天空筛选整理自scikit-image.org大神的英文原创作品 skimage.transform.hough_line。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。