2
$\begingroup$

I am new to Computer Vision, I am writing my own code for Hough Transform I have found the following algorithm:

Algorithm:

• The cell (i,j) corresponds to the square associated
with parameter values (θj, ρi).

• Initialize all cells with value 0.

• For each foreground point (xk,yk) in the thresholded
edge image
– Let θj equal all the possible θ-values

• Solve for ρ using ρ=x cos θj +ysin θj

• Round ρ to the closest cell value, ρq

• Increment A(i,q) if the θj results in ρq

• After this procedure, A(i,j)=P means that P points in
the xy-space lie on the line ρj=x cos θj +ysin θj

• Find line candiates where A(i,j) is above a suitable
threshold

I have written the code to increment the indices of hough accumulator array.

My Code:

%%
% Hough Transform

[rows,cols] = size(E); % size of edged image

rhomax = ceil (sqrt(rows^2 + cols^2)); % maximum value of rho

rhomax = rhomax * 2; % multuplied with 2 to cater both +rho and -rho values

accu = zeros(rhomax, 101); % initialization of accumulaor array with rhomax and 101 possible theta values

for i =1:1:rows % for all rows

    for j =1:1:cols % for all columns

        if(E(i,j) ~= 0) % if the pixel is ONE (part of the edge) then process it

            for theta = 85.0:0.1:95.0 % for the theta values 85.0 - 95.0

                % computing the theta index for hough accumulator
                theta_index = 0;
                for k =85.0:0.1:theta
                    theta_index = theta_index + 1;
                end
                %---

                rho = round (i*cos(theta) + j*sin(theta)); % computing the value of rho

                % computing the rho index for hough accumulator
                rho_index = 1;
                if(rho < 0)
                    rho_index = rho_index + abs(rho);
                elseif(rho > 0)
                    rho_index = rho_index + (rhomax/2) + rho;
                elseif(rho == 0)
                    rho_index = rho_index + (rhomax/2);
                end
                %---

                accu(rho_index,theta_index) = accu(rho_index,theta_index)+1; % incrementing the corresponding index in accumulator

            end

        end

    end

end

Now I want to proceed further with the threshold value. I will be very grateful if someone tell me how should I select my threshold value ?

$\endgroup$
3
$\begingroup$

Well, it really depends on what you expect to find in the image. The matlab function uses an example threshold of half the largest peak.

What sort of images are you using this on?


So, based on just finding vertical lines in your image, let's make some assumptions.

  • Your rectangle's minimum vertical side length is $L_{\rm min}$ pixels.
  • Your edge detection only finds 80% of each edge.

Then the minimum threshold will be $$ 0.8 L_{\rm min} $$

You many need to modify this if, for example, your edges tend to be thicker or thinner than the 80% figure.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.