【发布时间】:2016-04-12 23:50:53
【问题描述】:
我正在尝试编写一些代码来查找图像中的线条并在找到的线条上绘制一条红线。我已经设法使用霍夫变换来做到这一点,但我的问题是我需要它只找到水平和垂直线并忽略所有其他斜坡的线。
我想我可以通过找到代码找到的线的斜率来解决这个问题,并且只使用 if 语句在水平和垂直线上显示红线,但我无法弄清楚如何提取 x和我找到的点的 y 值。
有人对如何解决这个问题有任何建议吗?
下面是我的代码:
function findlineshv(I)
% Read Image
img = imread(I);
% Convert to black and white because
% edge function only works with BW imgs
bwImage = rgb2gray(img);
% figure(1),imshow(bwImage);
% find edges using edge function
b=edge(bwImage,'sobel');
% show edges
% figure(1),imshow(b);
% compute the Hough transform of the edges found
% by the edge function
[hou,theta,rho] = hough(b);
% define peaks, x and y
peaks = houghpeaks(hou,5,'threshold',ceil(0.3*max(hou(:))));
x = theta(peaks(:,2));
y = rho(peaks(:,1));
lines = houghlines(bwImage,theta,rho,peaks,'FillGap',5,'MinLength',7);
figure, imshow(bwImage), hold on
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',3,'Color','red');
end
【问题讨论】:
-
提示:如果您也分享您的图片,您可能会得到更快的回复。