Clear Filters
Clear Filters

Hough Transform voting matrix problem

5 views (last 30 days)
Daniel Silvério Almeida
Daniel Silvério Almeida on 12 Mar 2021
Edited: Sai Pavan on 10 Apr 2024
Hello, i tryng to develop a Hough transform function i have come this far but i am having a problem in the for cicle when putting the correct values in the voting matrix. Can someone help me?

Answers (1)

Sai Pavan
Sai Pavan on 10 Apr 2024
Edited: Sai Pavan on 10 Apr 2024
Hello Daniel,
I understand that you need help in filling the voting matrix or accumulator matrix while writing hough transform code from scratch.
There are several issues and improvements to be made in the attached "HoughTransfrom1" function, especially in the loop where you're trying to populate the Hough accumulator matrix H. The issues include:
  1. Incorrect use of "thetaangles" in inner loop: The loop is iterated over "thetaangles" but not used correctly to index into H. We need to calculate the "rho" value for each "theta" and then find the corresponding indices in "rhoscale" and "thetascale" to update the accumulator.
  2. handling of negative "rho" values: The code snippet for handling negative rho values is commented out. In the Hough Transform, "rho" can be negative, representing lines more than half a rotation from the reference angle. We need to accommodate these values correctly.
  3. Accumulator indexing: We need to map "rho" and "theta" values to their nearest bins in "rhoscale" and "thetascale". This requires a bit of manipulation since "rho" can be negative, and "theta" has a range of [-pi,pi].
Please refer to the below code snippet that is modified accomodating the above mentioned changes:
function [H, rhoscale, thetascale] = HoughTransform1(img1, thresholdold, rhoRes, thetaRes)
rhomax = sqrt(size(img1,1)^2 + size(img1,2)^2); % Calculate dimensions for rho
rhoscale = -rhomax:rhoRes:rhomax; % Include negative values for rho
thetascale = -pi:thetaRes:pi;
H = zeros(length(rhoscale), length(thetascale)); % Initialize the Hough accumulator matrix
[y, x] = find(img1 >= thresholdold); % Find x, y positions of thresholded image
% Loop through each edge pixel
for i = 1:length(x)
for thetaIdx = 1:length(thetascale)
rho = x(i) * cos(thetascale(thetaIdx)) + y(i) * sin(thetascale(thetaIdx)); % Calculate rho for each theta
[~, rhoIdx] = min(abs(rhoscale - rho)); % Find the nearest rho index in rhoscale
H(rhoIdx, thetaIdx) = H(rhoIdx, thetaIdx) + 1; % Increment the accumulator
end
end
end
Hope it helps!

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!