Почему некоторые строки игнорируются функцией hough line? - PullRequest
0 голосов
/ 11 июля 2020

Я немного пытаюсь понять

, как убедиться, что все строки распознаются с помощью Line Hough Transform, взятого из библиотеки sckit-image.

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

Ниже распознаны все строки: enter image description here

But if I apply the same script on similar image, one line will get ignored after applying the Hough transform,

I have read the documentation which says:

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

enter image description here

So my conclusion is the line got removed since it hadn't got enough "votes", (I have tested it with different precisions (0.05, 0.5, 0.1) degree, but still got the same issue). Here is the code:

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 ()

Как мне «поймать» и эту строчку, любое предложение?

1 Ответ

1 голос
/ 13 июля 2020

Более короткие строки имеют более низкие значения аккумулятора в преобразовании Хафа, поэтому вам необходимо соответствующим образом настроить порог. Если вы знаете, сколько сегментов линии вы ищете, вы можете установить довольно низкий порог, а затем ограничить количество обнаруженных пиков.

Вот сокращенная версия приведенного выше кода с измененным порогом для справки:

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

...