Reputation: 1
I'm trying to draw lines obtained from a Hough transform. The lines from the Hough transform have no length or end points; they are infinite.
To draw the lines, I need the end points. I can't find a good way to determine the length of the line so I multiply by 1000. I tried to come up with something better, but ChatGPT can't help me.
Do you know a better way?
def hough_lines(edges: np.ndarray, threshold: float, min_line_length: float = 50, rho: float = 1, theta: float = np.pi/180) -> np.ndarray:
height, width = edges.shape
diagonal = np.sqrt(height ** 2 + width ** 2)
rho_values = np.arange(-diagonal, diagonal, rho)
theta_values = np.linspace(-np.pi / 2, np.pi / 2, int(np.pi / theta))
cos_theta = np.cos(theta_values)
sin_theta = np.sin(theta_values)
accumulator = np.zeros((len(rho_values), len(theta_values)), dtype=int)
ys, xs = np.nonzero(edges)
for y, x in zip(ys, xs):
rho_vals = x * cos_theta + y * sin_theta
rho_indices = ((rho_vals + diagonal) / rho).astype(int)
accumulator[rho_indices, np.arange(len(theta_values))] += 1
line_indices, theta_indices = np.where(accumulator >= threshold)
lines = []
for rho_idx, theta_idx in zip(line_indices, theta_indices):
rho_val = rho_values[rho_idx]
theta_val = theta_values[theta_idx]
a = np.cos(theta_val)
b = np.sin(theta_val)
x0 = a * rho_val
y0 = b * rho_val
x1 = int(x0 + 1000 * (-b))
y1 = int(y0 + 1000 * (a))
x2 = int(x0 - 1000 * (-b))
y2 = int(y0 - 1000 * (a))
lines.append([x1, y1, x2, y2])
return np.array(lines)
Upvotes: 0
Views: 79