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-
and got after performming operations-
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:
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:
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);
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);
Comments
Post a Comment