matlab - Line detection in image -


i new image processing , trying detect vertical lines using code-

image=imread('benzene.jpg');   bw = im2bw(image); w1=[-1 2 -1 ; -1 2 -1 ; -1 2 -1]; g=(imfilter(double(bw),w1)); g=abs(g); t=max(g(:)); g=g>=t; imshow(g); 

this image-

enter image description here

and got after performming operations- enter image description here

so question why getting output?there 10 vertical lines if vertical double bonds counted 2 distinct vertical lines.also if want horizontal,vertical,45 , -45 lines,how can use 4 masks 1 single output?

one simple suggestion have detect gradient , determine orientation of edge point. bear in mind orientation in direction perpendicular edge. therefore, if want find vertical lines, direction perpendicular vertical line horizontal, either 180 degrees or -180 degrees respect cartesian plane. such, each orientation of edge points detected, if orientation either -180 degrees or 180 degrees, set output of location true, else false. detect gradient orientations, use imgradient image processing toolbox that. i'm assuming available have used both imread , im2bw , both part of toolbox:

im = imread('http://i.stack.imgur.com/bdnot.png'); tol = 5; [~,ang] = imgradient(im); out = (ang >= 180 - tol | ang <= -180 + tol); imshow(out); 

the code uses variable called tol define tolerance in angles want detect account noise or edges vertical when angle computed, may not appear be. basically, looking points angles within 180 degrees or -180 degrees.

this get:

enter image description here

as means of post-processing, use bwareaopen filter out pixel regions areas fall below amount. taking advantage of fact vertical lines have larger area other pixels, this:

out_filter = bwareaopen(out, 50); 

we get:

enter image description here


now if want detect horizontal lines, should find gradient orientations either -90 or 90 degrees. makes sense because lines horizontal, direction perpendicular horizontal line indeed vertical, , that's either -90 or 90 degrees. if want slanted lines, if want left leaning line, angles of either 45 degrees or -135 degrees , right leaning line, either -45 degrees or 135 degrees. i'll let work out why these angles indeed representative of kinds of lines.

you don't have horizontal lines in image provided, i'll slanted lines:

left leaning lines

note: had increase tolerance due quantization errors.

im = imread('http://i.stack.imgur.com/bdnot.png'); tol = 20; [~,ang] = imgradient(im); out = (ang >= 45 - tol & ang <= 45 + tol) | (ang >= -135 - tol & ang <= -135 + tol); out_filter = bwareaopen(out, 50); imshow(out_filter); 

enter image description here

right leaning lines:

also had increase tolerance here well:

im = imread('http://i.stack.imgur.com/bdnot.png'); tol = 20; [~,ang] = imgradient(im); out = (ang >= 135 - tol & ang <= 135 + tol) | (ang >= -45 - tol & ang <= -45 + tol); out_filter = bwareaopen(out, 50); imshow(out_filter); 

enter image description here


Comments

Popular posts from this blog

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -