clear
close all
clc
I=imread('111.jpg');% 读取图像
f =rgb2gray(I);%二值化
f =im2bw(f,0.35);
f =255-f;
f =edge(f,'canny');
figure;imshow(f);%获取霍夫空间
%'RhoResolution', Hough 变换 bin 沿 rho 轴的间距
% 指定为介于 0 和 norm(size(BW))之间(不包含两者)的正数。没明白啥意思
%'Theta',角度值 取值范围为-90~89[H,T,R]=hough(f,'RhoResolution',0.01,'Theta',-1:0.01:1);%R 与原点之间的距离值
%T 角度值
%H 霍夫值
figure;imagesc(R,T,H);colormap(hot);colorbar
xlabel('\theta T'),ylabel('\rho R');
axis on, axis normal, hold on;% 对h值进行筛选
[m,n]=find(H<95&H>70);%获取筛选后在霍夫空间的分布
t =T(n(:,1));
r =R(m(:,1));% 创建空白图用于绘制
figure;[m1,n1]=size(f);
z =zeros(m1,n1);%imshow(z,[]);%显示原图,将直线绘在原图上
imshow(f,[]);% 霍夫空间逆变换
for i=1:size(t,2)y(i,1)=(r(i)-1*cos((t(i)+360)*(pi/180)))/sin((t(i)+360)*(pi/180));y(i,2)=(r(i)-size(f(1,:),2)*cos((t(i)+360)*(pi/180)))/sin((t(i)+360)*(pi/180));xy(1,1)=1;xy(2,1)=y(i,1);xy(1,2)=size(f(1,:),2);xy(2,2)=y(i,2);
hold on
plot(xy(1,:),xy(2,:),'LineWidth',1,'Color','red');
end