I am trying to recreate MATLAB's hough function with mine. My code follows

function [H,T,R] = my_hough(x,dr,dtheta)
    rows = size(x,1);
    cols = size(x,2);
    D = sqrt((rows - 1)^2 + (cols - 1)^2);
    Nr = 2*(ceil(D/dr)) + 1;
    diagonal = dr*ceil(D/dr);
    R = -diagonal:dr:diagonal;
    T = -90:dtheta:90-dtheta;
    Ntheta = length(T);
    H = zeros(Nr,Ntheta);
    for i = 1:Ntheta
        for n1 = 1:rows
            for n2 = 1:cols
                if x(n1,n2)==1
                    r = n2*cos(T(i)*pi/180) + n1*sin(T(i)*pi/180);
                    [~,j] = min(abs(R-ones(1,Nr)*r));
                    H(j,i) = H(j,i) + 1;

where dr and dtheta are distance and angle resolution. Printing the difference between my Hough table and MATLAB's there are many zeros, but there are also some non-zero elements. Any idea why this is happening?

  • $\begingroup$ Since your θ is initially in degrees, you can use sind and cosd, instead of sin and cos, and avoid having to convert to radians. $\endgroup$ – Dima Jun 4 '15 at 17:16

I don't have the rep to comment on Dima's answer, but the code in the question does convert the $\theta$ to radians already.

This is my implementation of the routine for detecting lines:

[ theta,rho,height,width,Accumulator] = SHTanalyse( m,delta_theta,delta_rho )
%% Get dimensions of input image
[height, width] = size(m);

% Create array for theta 
theta = -90:delta_theta:90;
costheta = cosd(theta); % Create lookup tables to optimize computation of cos theta and sin theta
sintheta = sind(theta);

% Find bounds on rho
rhomax = round(sqrt(height^2 + width^2));

% Create array for rho
rho = -rhomax:delta_rho:rhomax;

% Get edgel coordinates
[yEdges,xEdges] =  find(m);

% Initialize the accumulator
Accumulator = zeros(numel(rho),numel(theta));

% Voting process
for k = 1:numel(yEdges) % Loop through edge pixels
    for t = 1:numel(theta)
        % Compute the corresponding rho
        rhvalue = round(xEdges(k).*costheta(t) + yEdges(k).*sintheta(t));
        % Increment accumulator
        Accumulator(rhvalue + round(0.5*numel(rho)) + 1,theta(t) + 90 + 1) = Accumulator(rhvalue + round(0.5*numel(rho)) + 1,theta(t) + 90 + 1) + 1;

You can compare its performance with the MATLAB's own Hough function to figure out the discrepancy.

| improve this answer | |
  • $\begingroup$ Your code is giving me an error about trying to access out of bounds elements of the Accumulator $\endgroup$ – Controller Jun 4 '15 at 17:04
  • $\begingroup$ In this case, the last line inside the 'for' loops (i.e. the line in which we vote in the accumulator) needs to be changed. Perhaps the issue lies in the 'round' part. Try to see what happens when 'numel(rho)' is odd and even. $\endgroup$ – AshivD Jun 6 '15 at 9:13

Well, actually it was a very silly mistake...

r = n2*cos(T(i)*pi/180) + n1*sin(T(i)*pi/180);

must be

r = (n2-1)*cos(T(i)*pi/180) + (n1-1)*sin(T(i)*pi/180); thanks to this weird 

MATLAB indexing...

| improve this answer | |

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.