clc;
clear all;
II=imread('9.jpg');
figure,imshow(II);
I=im2bw(II);
b=size(I,1);
I(1:0+350,:)=0;
I(550:b,:)=0;
BW=edge(I,'sobel');
[H,T,R]=hough(BW);
figure,imshow(H,[],'XData',T,'YData',R,'InitialMagnification','fit');
xlabel('\theta'),ylabel('\rho');
axis on,axis normal,hold on;
P=houghpeaks(H,10,'threshold',ceil(0.7*max(H(:))));
x=T(P(:,2));
y=R(P(:,1));
plot(x,y,'s','LineWidth',2 ,'color','red');
lines=houghlines(BW,T,R,P,'FillGap',5,'MinLength',7);
figure,imshow(I);
figure,imshow(II),hold on;
max_len=0;
for k=1:length(lines)
xy=[lines(k).point1;lines(k).point2];
xielv(k)=(lines(k).point2(1)-lines(k).point1(1))/(lines(k).point2(2)-lines(k).point1(2)+0.0001);
xielv(k)=round(xielv(k));
len=norm(lines(k).point1-lines(k).point2);
plot(xy(:,1),xy(:,2),'LineWidth',8,'color','green');
% plot(xy(1,1),xy(1,2),'x','LineWidth',2,'color','yellow');
% plot(xy(2,1),xy(2,2),'x','LineWidth',2,'color','red');
if(len>max_len)
max_len=len;
xy_long=xy;
end
%plot(lines(k));
end
for k=1:length(lines)
for j=k+1:length(lines)
if(xielv(k)==xielv(j))
tt=(lines(k).point2(1)-lines(j).point1(1))/(lines(k).point2(2)-lines(j).point1(2)+0.0001);
ttt=(lines(k).point2(1)-lines(j).point2(1))/(lines(k).point2(2)-lines(j).point2(2)+0.0001);
if(round(tt)==round(ttt))
tt=round(tt);
if(tt==xielv(k))
xxyy=[lines(k).point1;lines(j).point2];
plot(xxyy(:,1),xxyy(:,2),'LineWidth',8,'color','green');
end
end
end
end
end
%plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'color','cyan');