Skip to content

Instantly share code, notes, and snippets.

@palmerc
Created December 11, 2015 14:05
Show Gist options
  • Select an option

  • Save palmerc/a124548f985df5d3cfdc to your computer and use it in GitHub Desktop.

Select an option

Save palmerc/a124548f985df5d3cfdc to your computer and use it in GitHub Desktop.
Naive Hough Transform for Matlab
function [ Hough, theta_range, rho_range ] = naiveHough(I)
%NAIVEHOUGH Peforms the Hough transform in a straightforward way.
%
[rows, cols] = size(I);
theta_maximum = 90;
rho_maximum = floor(sqrt(rows^2 + cols^2)) - 1;
theta_range = -theta_maximum:theta_maximum - 1;
rho_range = -rho_maximum:rho_maximum;
Hough = zeros(length(rho_range), length(theta_range));
wb = waitbar(0, 'Naive Hough Transform');
for row = 1:rows
waitbar(row/rows, wb);
for col = 1:cols
if I(row, col) > 0
x = col - 1;
y = row - 1;
for theta = theta_range
rho = round((x * cosd(theta)) + (y * sind(theta)));
rho_index = rho + rho_maximum + 1;
theta_index = theta + theta_maximum + 1;
Hough(rho_index, theta_index) = Hough(rho_index, theta_index) + 1;
end
end
end
end
close(wb);
end
@palmerc
Copy link
Author

palmerc commented Dec 11, 2015

A naive implementation of the Hough Transform using the normal line representation. You need to supply a thresholded gradient magnitude image as input. The resulting rho-theta Hough matrix can then be opened and view to get an idea of where lines in the image exist.

@antneh
Copy link

antneh commented Dec 7, 2018

sir am completely new for how to implement this function on matlab. can you please send me the completed code(which give output when i give an image). [email protected]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment