Controller
Controller

Reputation: 529

Difference between MATLAB hough and my implementation

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;
                end
            end
        end    
    end
end

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?

Upvotes: 3

Views: 217

Answers (1)

Controller
Controller

Reputation: 529

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.

Upvotes: 2

Related Questions