凹凸线未到达图像边缘
我试图检测下图中的所有行:
使用以下代码我能够检测到几乎所有行:
%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:
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
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该问题源于 houghpeaks 属性过于严格,无法找到较小的线段。我使用的方法是:
使用您当前的代码来获取检测到的线所在的角度(我刚刚做了
[lines.theta]
并发现theta将等于-84
)再次使用您的代码,但仅包含该角度,同时放宽
houghpeaks
属性以允许检测更多线条。您将检测到更多重叠的线条,我们将通过根据其系数设置相似性阈值来丢弃这些线条。
这是我们收集的实现,
其中哪条线有相似的线,使得 coef 距离低于阈值
d 是对称二元相关矩阵,我们找到对角线上方上三角中的所有点,这是线 id#在同一行重复,所以我们丢弃它们。
The issue stems from the houghpeaks properties being too restrictive to find the smaller lines segments. The approach I used is to:
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
)use again your code but only with that angle included, while relaxing the
houghpeaks
properties to allow many more lines to be detected.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
we collect for which line there is a similar one such that the coef distance is below a thershold
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.