Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 61 additions & 49 deletions matlab/hough_transform_polar.m
Original file line number Diff line number Diff line change
@@ -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