JammingThebBits
JammingThebBits

Reputation: 752

How come some of the lines get ignored with hough line function?

I'm struggling a bit to figure out

how to make sure all lines get recognized with Line Hough Transform taken from sckit-image library.

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

Here below all lines got recognized: 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()

How should I "catch" this line too, any suggestion?

Upvotes: 0

Views: 1641

Answers (1)

Stefan van der Walt
Stefan van der Walt

Reputation: 7253

Shorter lines have lower accumulator values in the Hough transform, so you have to adjust the threshold appropriately. If you know how many line segments you are looking for, you can set the threshold fairly low and then limit the number of peaks detected.

Here's a condensed version of the code above, with modified threshold, for reference:

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

(Note: axline requires matplotlib 3.3.)

Upvotes: 2

Related Questions