Mar*_*bov 3 matlab image-processing
我正在尝试编写一些代码来查找图像中的线条并在找到的线条上绘制一条红线。我已经使用 Hough 变换设法做到了这一点,但我的问题是我需要它只找到水平线和垂直线,而忽略所有其他斜坡的线。
我想我可以通过找到代码找到的线的斜率来解决这个问题,并且只使用 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
Run Code Online (Sandbox Code Playgroud)
您只需在 Hough 函数中设置所需的 theta 值即可完成此操作。
start_angle = 80;
end_angle = 100;
theta_resolution = 0.5:
[H,T,R] = hough(b, 'Theta', start_angle:theta_resolution:end_angle);
Run Code Online (Sandbox Code Playgroud)