【发布时间】: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