gpt4 book ai didi

image - 图像中的线检测

转载 作者:太空宇宙 更新时间:2023-11-03 19:18:22 25 4
gpt4 key购买 nike

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

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

这是我的形象-

enter image description here

这是我执行操作后得到的- enter image description here

所以我的问题是为什么我会得到这个输出?如果垂直双键被算作 2 条不同的垂直线,则有 10 条垂直线。另外,如果我想获得水平、垂直、45 和 -45 的所有线怎么办,我怎样才能使用所有 4 个掩码来获得一个输出?

最佳答案

我有一个简单的建议是检测梯度并确定边缘点的方向。请记住,方向是与边缘垂直的方向。所以,如果要找竖线,垂直于竖线的方向就是水平方向,相对于笛卡尔平面要么是180度,要么是-180度。因此,对于检测到的边缘点的每个方向,如果方向为 -180 度或 180 度,则将此位置的输出设置为 true,否则为 false。要检测梯度方向,请使用 imgradient来自图像处理工具箱。我假设这是可用的,因为您同时使用了 imreadim2bw 并且它们都是该工具箱的一部分:

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

该代码使用一个名为 tol 的变量来定义您要检测的角度的公差,以考虑看起来垂直的噪声或边缘,但在计算角度时,它可能看起来不是.基本上,我们正在寻找角度在 180 度或 -180 度范围内的任何点。

这是我们得到的:

enter image description here

作为后处理的一种方式,您可以使用 bwareaopen过滤掉面积低于一定数量的像素区域。利用垂直线的面积比其他像素大这一事实,您可以这样做:

out_filter = bwareaopen(out, 50);

我们得到:

enter image description here


现在如果你想检测水平线,你应该找到 -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);

enter image description here

右倾线:

还必须在这里增加容忍度:

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

关于image - 图像中的线检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30934611/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com