0

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)
             if(BW(i,j)==1)
                  for ii=1:size(theta,2)                  
                      rho1 = i*cosd(theta(1,ii)) + j*sind(theta(1,ii));
                      rhotemp = rho1 - floor(rho1);
                      if(rhotemp>(0.5+0.25))
                          rho1 = ceil(rho1);
                      elseif(rhotemp>0.25)
                          rho1 = floor(rho1) + 0.5;
                      else
                          rho1 = floor(rho1);
                      end
                      position = (rho1 - rho(1,1))/0.5;
                      houghMatrix(position+1,ii) = houghMatrix(position+1,ii) + 1;
                  end
             end
         end
     end

 [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).

Vivek
  • 1
  • 1
  • 1
  • 1
    I worked with the Hough Transform a few years ago, this questions/answer will be useful to you: http://dsp.stackexchange.com/questions/1958/help-understanding-hough-transform/2006#2006 – CyberMen Apr 22 '15 at 15:29

1 Answers1

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.

Tolga Birdal
  • 5,465
  • 1
  • 16
  • 40