图片大小为1024*1280,这里先定位上边缘,再定位到下边缘
上边缘(行数1:512)
img = imread('./G_A01_L1_2_R.bmp');
% Convert to intensity.
I =imbinarize(img);
figure;
subplot(2,2,1);
imshow(I);
title('binary image');
% Extract edges.
BW1 = edge(I,'Prewitt');
subplot(2,2,2);
imshow(BW1);
title('edge');
[H1,T1,R1] = hough(BW1);
%显示变换域
subplot(2,2,3);
imshow(imadjust(rescale(H1)),'XData',T1,'YData',R1,'InitialMagnification','fit');
xlabel('\theta');
ylabel('\rho');
title('change domain');
axis on,axis normal,hold on
%计算变换域峰值
P1 = houghpeaks(H1,'threshold',ceil(0.3*max(H1(:))));
x=T1(P1(:,2));
y=R1(P1(:,1));
plot(x,y,'s','color','red');
%标记直线
L1 = houghlines(BW1,T1,R1,P1,'FillGap',200,'MinLength',100);
subplot(2,2,4);
imshow(RGB);
title('result')
hold on
max_len=0;
for k=1:length(L1)
xy=[L1(k).point1;L1(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
%Plot beginning and ends of lines
plot(xy(1,1),xy(1,2),'xw','LineWidth',2);
plot(xy(2,1),xy(2,2),'xw','LineWidth',2);
%Determine the endpoints of the longest line segment
len=norm(L1(k).point1-L1(k).point2);
if(len>max_len)
max_len=len;
xy_long=xy;
end
end
下边缘(513:1024)只需要改行数
效果图: