MATLAB 读取图像模板中的水平线条 - 直线度分析
以下是一个可能的解决方案,使用MATLAB的图像处理工具箱中的函数:
- 读取图像并转换为灰度图像:
img = imread('template.jpg');
gray_img = rgb2gray(img);
- 找到水平线条:
使用边缘检测函数edge找到图像中的边缘,然后使用霍夫变换函数hough找到水平线条的位置。
BW = edge(gray_img, 'canny');
[H,theta,rho] = hough(BW);
P = houghpeaks(H,10,'threshold',ceil(0.3*max(H(:))));
lines = houghlines(BW,theta,rho,P,'FillGap',10,'MinLength',20);
% 找到水平线条
horizontal_lines = [];
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
if abs(lines(k).theta) < 5
horizontal_lines = [horizontal_lines; xy];
end
end
- 可视化结果:
imshow(img)
hold on;
for k = 1:length(horizontal_lines)
plot(horizontal_lines(k,[1,2]),horizontal_lines(k,[3,4]),'LineWidth',2,'Color','green');
end
这样,水平线条就会以绿色线条的形式显示在原始图像上。
原文地址: https://www.cveoy.top/t/topic/nUVV 著作权归作者所有。请勿转载和采集!