clear all;
im=imread('E:\研究生用\高压线图片\1.jpg');
figure,imshow(im),hold on;%show the original image
title('The original imaage.');
im=rgb2gray(im);
imEdge=edge(im,'Sobel','horizontal');%提取边缘
figure;imshow(imEdge,[]);
[H,T,R] = hough(imEdge,'RhoResolution',0.5,'ThetaResolution',0.5);
figure,imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit'),hold on;
xlabel('\theta'), ylabel('\rho');
axis on, axis normal,hold on;
P = houghpeaks(H,3,'threshold',ceil(0.2*max(H(:))));
x = T(P(:,2)); y = R(P(:,1));
plot(x,y,'s','color','white');
lines = houghlines(imEdge,T,R,P,'FillGap',111,'MinLength',200);
figure, imshow(im), hold on;
title('The lines that have been found');
max_len = 0;
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','red');
end
bw=im2bw(imEdge,0.6);
[m,n]=size(bw);
step_p=1;
step_angle=0.1;
size_angle=round(pi/step_angle);
%注意这里 最大角度是180度,不是360度
pMax=sqrt(m.^2+n.^2);
size_p=round(pMax/step_p)+1;
houghSpace=zeros(size_p,size_angle);
u=1;
[row,col]=find(bw);
count=size(row);
for i=1:count
for k=1:size_angle
p=row(i)*cos(k*step_angle)+col(i)*sin(k*step_angle);
if(p>0) %p有可能小于0的,实际上是同一条直线会对应2个角度,其中一个角度使得p小于0.
pIndex=round(p/step_p)+1;
houghSpace(pIndex,k)=houghSpace(pIndex,k)+1;
end
end
end
%把hough参数空间的最大的几个值求出来,对应的就是原图的直线
maxPara=max(houghSpace(:));
scale=0.6;
[parP,parA]=find(houghSpace>=maxPara*scale);
%houghLine是检测出的直线
clear houghLine;
houghLine=zeros(m,n);
len=length(parP);
for i=1:count
for k=1:len
p=parP(k)*step_p;
angle=parA(k)*step_angle;
if(abs(row(i)*cos(step_angle)+col(i)*sin(step_angle)-step_p)<2)
houghLine(row(i),col(i))=1;
end
end
end
for i=1:381
for j=1:249
if houghLine(i,j)==1
RGB(i,j,1)=255;
RGB(i,j,2)=0;
RGB(i,j,3)=0;
end
end
end
figure;imshow(houghLine,[]);
title('The houghLine that have been found');