MATLAB:连一条简单的线都检测不出来吗?



我正在学习houghhoughlines在MATLAB中的工作原理。

这是我正在使用的代码:

clear all; clc; close all
I = imread('lines2.png');
BW = im2bw(I);
[H,T,R] = hough(BW,'Theta',-90:0.1:89.99,'RhoResolution',1);
P = houghpeaks(H,2);
lines = houghlines(H,T,R,P,'FillGap',10,'MinLength',1);
figure, imshow(I), hold on
for k = 1:length(lines)
   xy = [lines(k).point1; lines(k).point2];
   plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
   % Plot beginnings and ends of lines
   plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
   plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
end

我得到了这个结果(线的末端位于图像本身之外)。其他人可以重现它吗?

我尝试修改hough的参数,但我的测试都没有给出预期的结果。实际上,将'Theta'参数的步骤设置为1会导致未在右侧获得线路。我还尝试使用BW=~BW;颠倒图像,但后来我只得到一行很远的线...

对于houghlines,第一个输入是bw而不是h。在代码中使用〜bw,这使行白色和背景为黑色。通过这些更改,您的代码应正常工作。

lines = houghlines(BW,T,R,P,'FillGap',10,'MinLength',1);

最新更新