× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

Line detection in image

Ray Phan
Jun 24, 2015
<p>One simple suggestion I have is to detect the gradient and determine the orientation of an edge point. If the orientation is either -180 degrees or 180 degrees, set this to be the output. Use <a href="http://www.mathworks.com/help/images/ref/imgradient.html" rel="nofollow"><code>imgradient</code></a> for that:</p> <pre><code>im = imread('http://i.stack.imgur.com/bdNOt.png'); tol = 5; [~,ang] = imgradient(im); out = (ang &gt;= 180 - tol | ang &lt;= -180 + tol); imshow(out); </code></pre> <p>The code uses a variable called <code>tol</code> to define a tolerance in the angles you want to detect to account for noise or edges that look vertical but when the angle is computed, it may not appear to be. Basically, we are looking for any points whose angles are within 180 degrees or -180 degrees.</p> <p>This is what we get:</p> <p><img src="http://i.stack.imgur.com/hLkvp.png" alt="enter image description here"></p> <p>As a means of post-processing, you could use <a href="http://www.mathworks.com/help/images/ref/bwareaopen.html" rel="nofollow"><code>bwareaopen</code></a> to filter out pixel regions whose areas fall below a certain amount. Taking advantage of the fact that the vertical lines have a larger area than the other pixels, you could do something like this:</p> <pre><code>out_filter = bwareaopen(out, 50); </code></pre> <p>We get:</p> <p><img src="http://i.stack.imgur.com/cKJoC.png" alt="enter image description here"></p> <hr> <p>Now if you want to detect horizontal lines, you should find gradients that are either 0 or 90 degrees. If you want slanted lines, if you want a left leaning line, look for angles of either 45 degrees or -135 degrees and a right leaning line, either -45 degrees or 135 degrees.</p> <p>You don't have any horizontal lines in the image you provided, so I'll just look for slanted lines:</p> <h1>Left leaning lines</h1> <p><strong>Note:</strong> I had to increase the tolerance due to quantization errors.</p> <pre><code>im = imread('http://i.stack.imgur.com/bdNOt.png'); tol = 20; [~,ang] = imgradient(im); out = (ang &gt;= 45 - tol &amp; ang &lt;= 45 + tol) | (ang &gt;= -135 - tol &amp; ang &lt;= -135 + tol); out_filter = bwareaopen(out, 50); imshow(out_filter); </code></pre> <p><img src="http://i.stack.imgur.com/siFij.png" alt="enter image description here"></p> <h1>Right leaning lines:</h1> <p>Also had to increase the tolerance here as well:</p> <pre><code>im = imread('http://i.stack.imgur.com/bdNOt.png'); tol = 20; [~,ang] = imgradient(im); out = (ang &gt;= 135 - tol &amp; ang &lt;= 135 + tol) | (ang &gt;= -45 - tol &amp; ang &lt;= -45 + tol); out_filter = bwareaopen(out, 50); imshow(out_filter); </code></pre> <p><img src="http://i.stack.imgur.com/MKSRV.png" alt="enter image description here"></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/30934611/Line%20detection%20in%20image/30942695">Stack Overflow</a>.</p>

Get New Tutorials Delivered to Your Inbox

New tutorials will be sent to your Inbox once a week.

comments powered by Disqus