【问题标题】:How come some of the lines get ignored with hough line function?为什么有些线会被霍夫线函数忽略?
【发布时间】:2020-11-01 05:08:58
【问题描述】:

我有点想弄清楚

如何确保所有线条都能被从 sckit-image 库中提取的 Line Hough 变换识别。

https://scikit-image.org/docs/dev/auto_examples/edges/plot_line_hough_transform.html#id3

以下所有行都被识别:

但是如果我在相似的图像上应用相同的脚本, 应用霍夫变换后,一行将被忽略,

我已经阅读了文档,上面写着:

The Hough transform constructs a histogram array representing the parameter
space (i.e., an :math:`M \\times N` matrix, for :math:`M` different values of
the radius and :math:`N` different values of :math:`\\theta`).  For each
parameter combination, :math:`r` and :math:`\\theta`, we then find the number
of non-zero pixels in the input image that would fall close to the
corresponding line, and increment the array at position :math:`(r, \\theta)`
appropriately. 
We can think of each non-zero pixel "voting" for potential line candidates. The
local maxima in the resulting histogram indicates the parameters of the most
probably lines

所以我的结论是这条线被删除了,因为它没有得到足够的“投票”, (我已经用不同的精度(0.05、0.5、0.1)度测试了它,但仍然遇到同样的问题)。 代码如下:

import numpy as np
from skimage.transform import hough_line, hough_line_peaks
from skimage.feature import canny
from skimage import data,io
import matplotlib.pyplot as plt
from matplotlib import cm


# Constructing test image
image = io.imread("my_image.png")

# Classic straight-line Hough transform
# Set a precision of 0.05 degree.
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 3600)
h, theta, d = hough_line(image, theta=tested_angles)

# Generating figure 1
fig, axes = plt.subplots(1, 3, figsize=(15, 6))
ax = axes.ravel()

ax[0].imshow(image, cmap=cm.gray)
ax[0].set_title('Input image')
ax[0].set_axis_off()

ax[1].imshow(np.log(1 + h),
             extent=[np.rad2deg(theta[-1]), np.rad2deg(theta[0]), d[-1], d[0]],
             cmap=cm.gray, aspect=1/1.5)
ax[1].set_title('Hough transform')
ax[1].set_xlabel('Angles (degrees)')
ax[1].set_ylabel('Distance (pixels)')
ax[1].axis('image')

ax[2].imshow(image, cmap=cm.gray)
origin = np.array((0, image.shape[1]))
for _, angle, dist in zip(*hough_line_peaks(h, theta, d)):
    y0, y1 = (dist - origin * np.cos(angle)) / np.sin(angle)
    ax[2].plot(origin, (y0, y1), '-r')
ax[2].set_xlim(origin)
ax[2].set_ylim((image.shape[0], 0))
ax[2].set_axis_off()
ax[2].set_title('Detected lines')

plt.tight_layout() plt.show()

我也应该如何“抓住”这条线, 有什么建议吗?

【问题讨论】:

  • 请编辑您的问题并显示您运行的实际代码。如果您生成了霍夫累加器空间图像,还可以显示它。谢谢。
  • @MarkSetchell 没问题我已经更新了问题,谢谢!
  • 能否尝试将阈值参数修改为hough_line_peaks?您缺少的那条线比其他线短,因此可能低于阈值。
  • 太好了,作为答案发布在下面。
  • @JammingThebBits 请考虑在下面接受(并支持)Stefan 的回答,以便其他人知道它是正确的,以便 Stefan 获得适当的声誉。它不会花费您任何费用,也可以提高您作为 StackOverflow 优秀公民的声誉。单击投票计数旁边的勾号/复选标记以接受。谢谢。

标签: scikit-image hough-transform


【解决方案1】:

较短的线在霍夫变换中具有较低的累加器值,因此您必须适当调整阈值。如果您知道要查找的线段数量,则可以将阈值设置得相当低,然后限制检测到的峰值数量。

这是上面的代码的浓缩版本,具有修改的阈值,供参考:

import numpy as np
from skimage.transform import hough_line, hough_line_peaks
from skimage import io
import matplotlib.pyplot as plt
from matplotlib import cm
from skimage import color


# Constructing test image
image = color.rgb2gray(io.imread("my_image.png"))

# Classic straight-line Hough transform
# Set a precision of 0.05 degree.
tested_angles = np.linspace(-np.pi / 2, np.pi / 2, 3600)

h, theta, d = hough_line(image, theta=tested_angles)
hpeaks = hough_line_peaks(h, theta, d, threshold=0.2 * h.max())

fig, ax = plt.subplots()
ax.imshow(image, cmap=cm.gray)

for _, angle, dist in zip(*hpeaks):
    (x0, y0) = dist * np.array([np.cos(angle), np.sin(angle)])
    ax.axline((x0, y0), slope=np.tan(angle + np.pi/2))

plt.show()

(注意:axline需要matplotlib 3.3。)

【讨论】:

    猜你喜欢
    • 2023-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-07
    • 2013-03-15
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多