title: Matlab Hough transform for line detection
body: <p>I am trying to detect the white straight lines on the image using the hough transform. </p>
  
  <p><a href="https://i.stack.imgur.com/yTE30.jpg" rel="nofollow noreferrer">Binary image with White = 1,  Black= 0 - Lines detected in green and longest line detected in red</a></p>
  
  <p>The following code only detects sections of a curved line as straight and is ignoring (failing to identify) the truly straight lines. How can I modify it to find only the proper straight lines among the white lines?</p>
  
  <pre><code>    load('image.mat')
  
      %Binarize image
  
      image(image== 0) = 1;
      image= image- ones(size(image),'int8');
      image= logical(image);
      % imshow(BW_active,[0 1])
  
  
      [H,theta,rho] = hough(image, 'RhoResolution',0.5);
      P = houghpeaks(H);
      lines = houghlines(image,theta,rho,P);      %,'FillGap',20,'MinLength',7);
  
      figure, imshow(image, [0 1]), hold on
      max_len = 0;
  
      % from Mathworks
  for k = 1:length(lines)
     xy = [lines(k).point1; lines(k).point2];
     plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
  
     % Plot beginnings and ends of lines
     plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
     plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
  
     % Determine the endpoints of the longest line segment
     len = norm(lines(k).point1 - lines(k).point2);
     if ( len &gt; max_len)image
        max_len = len;
        xy_long = xy;
     end
  end
      % highlight the longest line segment
      plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
  </code></pre>
  
  <p>Here there is the .mat file.
  <a href="https://drive.google.com/file/d/0BxCvoDCdJJYoOFZLeklCX2dzc2c/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/0BxCvoDCdJJYoOFZLeklCX2dzc2c/view?usp=sharing</a></p>