凹凸线未到达图像边缘

发布于 2025-01-11 21:47:29 字数 1299 浏览 0 评论 0原文

我试图检测下图中的所有行:

在此处输入图像描述

使用以下代码我能够检测到几乎所有行:

%Find the line detected
I = imread('img3.png');
J = histeq(I);
BW = edge(J,'canny');
[H,T,R] = hough(BW);
P = houghpeaks(H,30,'Threshold',0.7*max(H(:)),'nhoodsize',[5 5]);
lines = houghlines(BW,T,R,P );
figure, imshow(I, []), hold on, max_len = 0;
for k = 1:length(lines)
 xy = [lines(k).point1; lines(k).point2];
 plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
 plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
 plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
 len = norm(lines(k).point1 - lines(k).point2);
 if ( len > max_len)
 max_len = len;
 xy_long = xy;
 end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');

这给出了以下图像

在此处输入图像描述

但是,正如您所看到的,线条并没有一直延伸到图像的边缘。我尝试缩小 nhood 大小,但这意味着对某些线条进行双重检测,并且不会将线条延伸到边缘。 还有可能检测到最顶线和最底线吗?我知道这些线由于较短而不会获得那么多选票,我修改了阈值,但最终出现了虚假的对角线,并且仍然没有检测到顶线和底线。

另外,我如何在未知图像上设置 Houghlines 参数。这里非常简单,能够(粗略地)估计我期望看到的行数,然后围绕该数字进行调节。

提前致谢

I am trying to detect all the lines in the following image:

enter image description here

Using the following code I am able to detect almost all lines:

%Find the line detected
I = imread('img3.png');
J = histeq(I);
BW = edge(J,'canny');
[H,T,R] = hough(BW);
P = houghpeaks(H,30,'Threshold',0.7*max(H(:)),'nhoodsize',[5 5]);
lines = houghlines(BW,T,R,P );
figure, imshow(I, []), hold on, max_len = 0;
for k = 1:length(lines)
 xy = [lines(k).point1; lines(k).point2];
 plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
 plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
 plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
 len = norm(lines(k).point1 - lines(k).point2);
 if ( len > max_len)
 max_len = len;
 xy_long = xy;
 end
end
% highlight the longest line segment
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');

This gives the following image

enter image description here

However as you can see the lines do not go all the way to the edge of the image. I have tried shrinking the nhood size but this meant double detection of some lines and no extension of lines to the edge.
Also is it possible to detect the very top and bottom lines? I understand that these lines won't have as many votes due to them being shorter, I've modified the threshold but end up with spurious diagonal lines and still no detection of the top and bottom lines.

Also how would I set the Houghlines parameter on an unknown image. Here it's quite simple, being able to estimate (roughly) the number of lines I'd expect to see and then moderate around that number.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

有深☉意 2025-01-18 21:47:29

该问题源于 houghpeaks 属性过于严格,无法找到较小的线段。我使用的方法是:

  1. 使用您当前的代码来获取检测到的线所在的角度(我刚刚做了[lines.theta]并发现theta将等于-84)

  2. 再次使用您的代码,但仅包含该角度,同时放宽 houghpeaks 属性以允许检测更多线条。

  3. 您将检测到更多重叠的线条,我们将通过根据其系数设置相似性阈值来丢弃这些线条。

这是我们收集的实现,

angleList = -84; 
[H,T,R] = hough(BW, 'Theta', angleList);
P = houghpeaks(H,80,'Threshold', 0.1*max(H(:)),'nhoodsize',[1 1]);
lines = houghlines(BW,T,R,P,'FillGap',500,'MinLength',5 );

% just get the eq of the line coef a,b using polyfit (y=a*x+b)
line_coef= @(x) polyfit([lines(x).point1(1) lines(x).point2(1)],...
                        [lines(x).point1(2) lines(x).point2(2)],1);

for n=1:numel(lines)
    p(:,n)=line_coef(n);
end

其中哪条线有相似的线,使得 coef 距离低于阈值

for n=1:numel(lines)-1
     d(:,n) = sum(abs(p(:,n)-p)) <2 ;
end

d 是对称二元相关矩阵,我们找到对角线上方上三角中的所有点,这是线 id#在同一行重复,所以我们丢弃它们。

id=find(sum(triu(d,1)));
lines(id)=[];

%... plot the lines using your code 

输入图片此处描述

The issue stems from the houghpeaks properties being too restrictive to find the smaller lines segments. The approach I used is to:

  1. use your current code to obtain the angle that the detected lines are at (I just did [lines.theta] and found that theta will be equal to -84)

  2. use again your code but only with that angle included, while relaxing the houghpeaks properties to allow many more lines to be detected.

  3. you'll get many more lines detected that overlap, and we'll discard those by setting a similarity threshold based on their coefficients.

here's the implementation

angleList = -84; 
[H,T,R] = hough(BW, 'Theta', angleList);
P = houghpeaks(H,80,'Threshold', 0.1*max(H(:)),'nhoodsize',[1 1]);
lines = houghlines(BW,T,R,P,'FillGap',500,'MinLength',5 );

% just get the eq of the line coef a,b using polyfit (y=a*x+b)
line_coef= @(x) polyfit([lines(x).point1(1) lines(x).point2(1)],...
                        [lines(x).point1(2) lines(x).point2(2)],1);

for n=1:numel(lines)
    p(:,n)=line_coef(n);
end

we collect for which line there is a similar one such that the coef distance is below a thershold

for n=1:numel(lines)-1
     d(:,n) = sum(abs(p(:,n)-p)) <2 ;
end

d is a symmetric binary correlation matrix, we find all the points in the upper triangular above the diagonal, this are the lines id# that repeat on the same line so we discard them.

id=find(sum(triu(d,1)));
lines(id)=[];

%... plot the lines using your code 

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文