Azzera filtri
Azzera filtri

Hough Transform voting matrix problem

8 visualizzazioni (ultimi 30 giorni)
Daniel Silvério Almeida
Daniel Silvério Almeida il 12 Mar 2021
Modificato: Sai Pavan il 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?

Risposte (1)

Sai Pavan
Sai Pavan il 10 Apr 2024
Modificato: Sai Pavan il 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
Hope it helps!

Community Treasure Hunt

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

Start Hunting!

Translated by