图像中的线检测 [英] Line detection in image

查看:227
本文介绍了图像中的线检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是图像处理的新手,我试图使用此代码检测垂直线 -

  image = imread(' benzene.jpg'); 
BW = im2bw(图片);
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);

这是我的形象 -



< img src =https://i.stack.imgur.com/bdNOt.pngalt =在此输入图像说明>



这就是我得到的执行操作后 -



所以我的问题是为什么我得到这个输出?如果垂直双键被计为2条不同的垂直线,那么有10条垂直线。如果我想得到水平,垂直,45和-45所有线条怎么办? ,我如何使用所有4个掩码来获得单个输出?

解决方案

我有一个简单的建议是检测渐变并确定边缘点的方向。请记住,方向位于与边缘垂直的方向上。因此,如果要查找垂直线,垂直于垂直线的方向是水平的,相对于笛卡尔平面为180度或-180度。因此,对于检测到的边缘点的每个方向,如果方向为-180度或180度,则将此位置的输出设置为 true ,否则 false 。要检测渐变方向,请使用过滤掉区域低于一定数量的像素区域。利用垂直线比其他像素具有更大面积的事实,你可以这样做:

  out_filter = bwareaopen(out,50); 

我们得到:








现在,如果要检测水平线,则应找到-90度或90度的渐变方向。这是有道理的,因为那些水平线,垂直于水平线的方向确实是垂直的,并且是-90度或90度。如果你想要斜线,如果你想要左倾斜线,寻找45度或-135度和右倾斜线的角度,-45度或135度。我会告诉你为什么这些角度确实代表了那些线条。



你提供的图像中没有任何水平线,所以我只需寻找斜线:



左倾斜线



注意:由于量化错误,我不得不增加容差。

  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);



右倾斜线:



此处还必须增加容差以及:

  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);


I am new to image processing and I was trying to detect vertical lines using this 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 was my image-

And this is what I got after performming the operations-

So my question is why am I getting this output?There are 10 vertical lines if vertical double bonds are counted as 2 distinct vertical lines.Also what if I want to get horizontal,vertical,45 and -45 all the lines,how can I use all the 4 masks to get one single output?

解决方案

One simple suggestion I have is to detect the gradient and determine the orientation of an edge point. Bear in mind that the orientation is in the direction that is perpendicular to the edge. Therefore, if you want to find vertical lines, the direction that is perpendicular to a vertical line is horizontal, which is either 180 degrees or -180 degrees with respect to the Cartesian plane. As such, for each orientation of the edge points that are detected, if the orientation is either -180 degrees or 180 degrees, then set the output of this location to be true, else false. To detect the gradient orientations, use imgradient from the image processing toolbox for that. I'm assuming this is available as you have used both imread and im2bw and they are both part of that 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 a variable called tol 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.

This is what we get:

As a means of post-processing, you could use bwareaopen 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:

out_filter = bwareaopen(out, 50);

We get:


Now if you want to detect horizontal lines, you should find gradient orientations that are either -90 or 90 degrees. This makes sense because those lines that are horizontal, the direction perpendicular to a horizontal line is indeed vertical, and that's either -90 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. I'll let you work out why these angles are indeed representative of those kinds of lines.

You don't have any horizontal lines in the image you provided, so I'll just look for slanted lines:

Left leaning lines

Note: I had to increase the tolerance due to 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 to increase the tolerance here as 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);

这篇关于图像中的线检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆