From 3f14bc3af7af2fd5770431c4790f8a71b062c366 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Tue, 14 Jul 2026 00:57:24 +0000 Subject: [PATCH] fix: Add vote accumulation logic to hough_transform_polar.m Co-authored-by: tsainez <13399044+tsainez@users.noreply.github.com> --- matlab/hough_transform_polar.m | 110 ++++++++++++++++++--------------- 1 file changed, 61 insertions(+), 49 deletions(-) diff --git a/matlab/hough_transform_polar.m b/matlab/hough_transform_polar.m index 58904ab..69f8485 100644 --- a/matlab/hough_transform_polar.m +++ b/matlab/hough_transform_polar.m @@ -1,50 +1,62 @@ -function [r, theta] = hough_transform_polar(edge_map) - - %% find x, y position from edge map - [edge_y, edge_x] = find(edge_map); - - %% range of r - H = size(edge_map, 1); - W = size(edge_map, 2); - - r_max = round(sqrt(H^2 + W^2)); - r_min = -r_max; - r_step = 1; - r_range = r_min : r_step : r_max; - - %% range of theta - theta_step = 0.01; - theta_range = -pi/2 : theta_step : pi/2; - - %% create vote matrix - V = zeros(length(r_range), length(theta_range)); - - %% TODO: add votes -% V(1, 1) = 1; % remove this line - for i = 1:length(edge_y) - x = edge_x(i); - y = edge_y(i); - for theta_index = 1:length(theta_range) - theta = theta_range(theta_index); - r = x * cos(theta) + y * sin(theta); - if r_min <= r - if r <= r_max - r_index = round((r - r_min) / r_step) + 1; - V(r_index, theta_index) = V(r_index, theta_index) + 1; - end - end - end - end - - - - %% visualize votes -% figure, imagesc(V); xlabel('theta'); ylabel('r'); - - %% find the maximal vote - max_vote = max(V(:)); - [max_r_index, max_theta_index] = find( V == max_vote ); - r = r_range(max_r_index); - theta = theta_range(max_theta_index); - +function [r, theta] = hough_transform_polar(edge_map) + % HOUGH_TRANSFORM_POLAR Performs the Hough Transform to detect lines using polar coordinates. + % + % [r, theta] = HOUGH_TRANSFORM_POLAR(edge_map) takes a binary edge map image as input + % and returns the distance from the origin (r) and angle (theta) of the most prominent line + % detected in the image. + % + % Inputs: + % edge_map - A binary matrix representing the edge map of an image, + % where non-zero values indicate edge pixels. + % + % Outputs: + % r - The distance from the origin to the detected line. + % theta - The angle of the normal to the detected line. + + %% find x, y position from edge map + [edge_y, edge_x] = find(edge_map); + + %% range of r + H = size(edge_map, 1); + W = size(edge_map, 2); + + r_max = round(sqrt(H^2 + W^2)); + r_min = -r_max; + r_step = 1; + r_range = r_min : r_step : r_max; + + %% range of theta + theta_step = 0.01; + theta_range = -pi/2 : theta_step : pi/2; + + %% create vote matrix + V = zeros(length(r_range), length(theta_range)); + + %% add votes + for i = 1:length(edge_y) + x = edge_x(i); + y = edge_y(i); + for theta_index = 1:length(theta_range) + theta = theta_range(theta_index); + r = x * cos(theta) + y * sin(theta); + if r_min <= r + if r <= r_max + r_index = round((r - r_min) / r_step) + 1; + V(r_index, theta_index) = V(r_index, theta_index) + 1; + end + end + end + end + + + + %% visualize votes +% figure, imagesc(V); xlabel('theta'); ylabel('r'); + + %% find the maximal vote + max_vote = max(V(:)); + [max_r_index, max_theta_index] = find( V == max_vote ); + r = r_range(max_r_index); + theta = theta_range(max_theta_index); + end \ No newline at end of file