圖片大小爲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)只需要改行數
效果圖: