I want to implement Hough transform on image without using inbuilt function. Some papers say that the image is first flipped before applying Hough transform. I do not understand how Matlab is doing it.

I have written the code below, but the H matrix by Matlab and houghMatrix generated by me are not same.

I have used default values of rhoResolution as 0.5 and thetaResolution as 0.5 also. I tried to do it without rotating image but then also not getting same matrices.

RGB = imread('gantrycrane.png');
     I  = rgb2gray(RGB); % convert to intensity
     BW = edge(I,'canny'); % extract edges
     BW = BW(end:-1:1,1:1:end);
     theta = -90:0.5:(90-0.5);
     D = sqrt((size(BW,1) - 1)^2 + (size(BW,2) - 1)^2);
     rho = -ceil(D):0.5:ceil(D);
     houghMatrix = zeros(size(rho,2),size(theta,2));
     for i=1:size(BW,1)
         for j=1:size(BW,2)
                  for ii=1:size(theta,2)                  
                      rho1 = i*cosd(theta(1,ii)) + j*sind(theta(1,ii));
                      rhotemp = rho1 - floor(rho1);
                          rho1 = ceil(rho1);
                          rho1 = floor(rho1) + 0.5;
                          rho1 = floor(rho1);
                      position = (rho1 - rho(1,1))/0.5;
                      houghMatrix(position+1,ii) = houghMatrix(position+1,ii) + 1;

 [H,T,R] = hough(BW,'RhoResolution',0.5,'Theta',-90:0.5:89.5);

I need to implement this in C code after that, and the functionality should be same as Matlab code. So I need to have a similar matrix like Matlab (if error of +-5 then it is acceptable but this is two totally different matrices).


1 Answer 1


Check this out. It is an easy to follow implementation. You can compare your implementation against this. Here is a no loop version.

Another advice is to add comments in the code above so that it could be understood better.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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