title: How to detect hand drawn lines using hough transform in matlab?
body: <p>I'm working on a matlab image processing project which basically extracts components and connections from an image of hand-drawn circuit diagram.</p>
  
  <p>After preproccessing and obtaining skeleton image, I tried to use Hough transform to detect the lines so that i can identify corners and connection paths.</p>
  
  <p>Here is the code: </p>
  
  <pre><code>[H,T,R] = hough(im);
  peaks = houghpeaks(H,50,'Threshold',ceil(0.3*max(H(:))));
  lines = houghlines(im, T,R,peaks, 'Fillgap', 20, 'MinLength', 20);
  
  figure; imshow(im);
  title('Lines detected');
  hold on;
  for l=1:length(lines)
      xy = [lines(l).point1; lines(l).point2];
      if ((lines(l).theta == 0)||(lines(l).theta &gt;= 355 &amp;&amp; lines(l).theta &lt; 5)) || (lines(l).theta &lt; 95 &amp;&amp; lines(l).theta &gt; 85) % detect only approx. horizontal and vertical lines 
          plot(xy(:,1),xy(:,2), 'LineWidth', 2);
      end
  end
  </code></pre>
  
  <p>This is the input and output that i got when executing: </p>
  
  <p><a href="https://i.stack.imgur.com/Vs8xq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Vs8xq.png" alt="enter image description here"></a></p>
  
  <p>I need to detect all the line segments, that are almost horizontal or vertical, having minimum length, with some irregularities due to hand drawn nature.</p>
  
  <p>In given screenshot, the output image shows only few detected lines, and some of lines are partially detected. It should actually detect all the wires used to connect components</p>
  
  <p>How can i tune Hough transform functions or use any other methods to achieve this requirement ?</p>