matlab - 凹凸线未到达图像边缘

标签 matlab hough-transform houghlines

我正在尝试检测下图中的所有线条:

enter image description here

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

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

这给出了以下图像

enter image description here

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

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

提前致谢

最佳答案

该问题源于 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 

enter image description here

关于matlab - 凹凸线未到达图像边缘,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71368930/

相关文章:

python - 改进 HoughLines 以进行水平线检测(Python、OpenCV)

c++ - 如何通过opencv计算图像中两个圆的距离

python - 如何防止 HoughLines 多次检测某些行

python - 通过霍夫变换检测框

matlab - 如何更改 HeatMap MATLAB 中 yticklabels 的字体大小?

matlab - `im2col`和 `col2im`的高效实现

arrays - 对包含奇数大小数组的结构中的每个元素求和 [MATLAB]

python - 如何找到图像中物体的中心和角度?

Python OpenCV : Hough Transform does not detect obvious lines

matlab - 有人知道语法 `pi()` 在 matlab 中的含义吗?