MATLAB代码:读取实验原理中的水平模板图像并提取直线度
% 读取水平模板图像 I = imread('horiz_template.png');
% 转换为灰度图像 Igray = rgb2gray(I);
% 二值化图像 thresh = graythresh(Igray); Ibw = imbinarize(Igray, thresh);
% 去除小的噪点 Iclean = bwareaopen(Ibw, 50);
% 提取水平直线 [H,theta,rho] = hough(Iclean,'Theta',-90:0.1:90); P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:)))); lines = houghlines(Iclean,theta,rho,P,'FillGap',5,'MinLength',7);
% 绘制直线 figure, imshow(Igray), hold on max_len = 0; for k = 1:length(lines) xy = [lines(k).point1; lines(k).point2];
% 计算直线长度 len = norm(lines(k).point1 - lines(k).point2);
% 仅绘制长度较长的直线 if ( len > max_len) max_len = len; xy_long = xy; end end plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','red');
% 计算直线的角度 angle = atan2(abs(xy_long(1,2)-xy_long(2,2)),abs(xy_long(1,1)-xy_long(2,1)))*180/pi; fprintf('水平直线的角度为:%f ', angle);
原文地址: http://www.cveoy.top/t/topic/nUVw 著作权归作者所有。请勿转载和采集!