Я нашел репо в Github, которое его вычисляет, и адаптировал его, чтобы отображать только небольшое пространство.
Ссылка на Github: https://github.com/alyssaq/hough_transform
Адаптированный ниже код:
import numpy as np
import imageio
import math
import matplotlib.pyplot as plt
def rgb2gray(rgb):
return np.dot(rgb[..., :3], [0.299, 0.587, 0.114]).astype(np.uint8)
def hough_line(img, angle_step=1, lines_are_white=True, value_threshold=5):
"""
Hough transform for lines
Input:
img - 2D binary image with nonzeros representing edges
angle_step - Spacing between angles to use every n-th angle
between -90 and 90 degrees. Default step is 1.
lines_are_white - boolean indicating whether lines to be detected are white
value_threshold - Pixel values above or below the value_threshold are edges
Returns:
accumulator - 2D array of the hough transform accumulator
theta - array of angles used in computation, in radians.
rhos - array of rho values. Max size is 2 times the diagonal
distance of the input image.
"""
# Rho and Theta ranges
thetas = np.deg2rad(np.arange(-90.0, 90.0, angle_step))
width, height = img.shape
diag_len = int(round(math.sqrt(width * width + height * height)))
rhos = np.linspace(-diag_len, diag_len, diag_len * 2)
# Cache some resuable values
cos_t = np.cos(thetas)
sin_t = np.sin(thetas)
num_thetas = len(thetas)
# Hough accumulator array of theta vs rho
accumulator = np.zeros((2 * diag_len, num_thetas), dtype=np.uint8)
# (row, col) indexes to edges
are_edges = img > value_threshold if lines_are_white else img < value_threshold
y_idxs, x_idxs = np.nonzero(are_edges)
# Vote in the hough accumulator
for i in range(len(x_idxs)):
x = x_idxs[i]
y = y_idxs[i]
for t_idx in range(num_thetas):
# Calculate rho. diag_len is added for a positive index
rho = diag_len + int(round(x * cos_t[t_idx] + y * sin_t[t_idx]))
accumulator[rho, t_idx] += 1
return accumulator, thetas, rhos
def show_hough_line(img, accumulator, thetas, rhos, save_path=None):
plt.imshow(accumulator, aspect='auto', cmap='jet', extent=[np.rad2deg(thetas[-1]), np.rad2deg(thetas[0]), rhos[-1], rhos[0]])
if save_path is not None:
plt.savefig(save_path, bbox_inches='tight')
plt.show()
if __name__ == '__main__':
imgpath = 'path_img.tif'
img = imageio.imread(imgpath)
if img.ndim == 3:
img = rgb2gray(img)
accumulator, thetas, rhos = hough_line(img)
show_hough_line(img,
accumulator,
thetas, rhos,
save_path='output.png')
Получаю правильный результат: ![введите описание изображения здесь](https://i.stack.imgur.com /GzFPU.png )
Для этого изображения:
![введите описание изображения здесь](https://i.stack.imgur.com/XZTAM.png)
person
natielle
schedule
06.12.2020