title: Hough transform in MATLAB without using hough function
body: <p>I found an implementation of the Hough transform in MATLAB at <a href="http://rosettacode.org/wiki/Hough_transform#MATLAB" rel="nofollow">Rosetta Code</a>, but I'm having trouble understanding it. Also I would like to modify it to show the original image and the reconstructed lines (de-Houghing).</p>
  
  <p>Any help in understanding it and de-Houghing is appreciated. Thanks</p>
  
  <ol>
  <li><p>Why is the image flipped?</p>
  
  <p><code>theImage = flipud(theImage);</code></p></li>
  <li><p>I can't wrap my head around the norm function. What is its purpose, and can it be avoided?</p></li>
  </ol>
  
  <p><strong>EDIT:</strong> norm is just a synonym for euclidean distance: sqrt(width^2 + height^2)</p>
  
  <p><code>rhoLimit = norm([width height]);</code></p>
  
  <ol>
  <li><p>Can someone provide an explanation of how/why rho, theta, and houghSpace is calculated?</p>
  
  <pre><code>rho = (-rhoLimit:1:rhoLimit);          
  theta = (0:thetaSampleFrequency:pi);
  
  numThetas = numel(theta);
  houghSpace = zeros(numel(rho),numThetas);
  </code></pre></li>
  <li><p>How would I de-Hough the Hough space to recreate the lines?</p></li>
  </ol>
  
  <p>Calling the function using a 10x10 image of a diagonal line created using the identity (eye) function</p>
  
  <pre><code>theImage = eye(10)
  thetaSampleFrequency = 0.1
  [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)
  </code></pre>
  
  <p>The actual function</p>
  
  <pre><code>function [rho,theta,houghSpace] = houghTransform(theImage,thetaSampleFrequency)
  
      %Define the hough space
      theImage = flipud(theImage);
      [width,height] = size(theImage);
  
      rhoLimit = norm([width height]);
      rho = (-rhoLimit:1:rhoLimit);          
      theta = (0:thetaSampleFrequency:pi);
  
      numThetas = numel(theta);
      houghSpace = zeros(numel(rho),numThetas);
  
      %Find the "edge" pixels
      [xIndicies,yIndicies] = find(theImage);
  
      %Preallocate space for the accumulator array
      numEdgePixels = numel(xIndicies);
      accumulator = zeros(numEdgePixels,numThetas);
  
      %Preallocate cosine and sine calculations to increase speed. In
      %addition to precallculating sine and cosine we are also multiplying
      %them by the proper pixel weights such that the rows will be indexed by 
      %the pixel number and the columns will be indexed by the thetas.
      %Example: cosine(3,:) is 2*cosine(0 to pi)
      %         cosine(:,1) is (0 to width of image)*cosine(0)
      cosine = (0:width-1)'*cos(theta); %Matrix Outerproduct  
      sine = (0:height-1)'*sin(theta); %Matrix Outerproduct
  
      accumulator((1:numEdgePixels),:) = cosine(xIndicies,:) + sine(yIndicies,:);
  
      %Scan over the thetas and bin the rhos 
      for i = (1:numThetas)
          houghSpace(:,i) = hist(accumulator(:,i),rho);
      end
  
      pcolor(theta,rho,houghSpace);
      shading flat;
      title('Hough Transform');
      xlabel('Theta (radians)');
      ylabel('Rho (pixels)');
      colormap('gray');
  
  end
  </code></pre>