要求:拍攝樹葉照片,正反兩張,提取葉脈,測量葉脈的分支,以及分支的傾斜角。
霍夫變換以及canny運算元理論百度。。。。
程式:
clc,close BW=imread('leaf.jpg'); BW=rgb2gray(BW); thresh=[0.13,0.3]; %閾值設定根據映像自訂範圍 sigma=1;%定義高斯參數,1-3 f = edge(double(BW),'canny',thresh,sigma); figure(1),imshow(f,[]); title('canny 邊緣檢測');%canny邊緣運算元 [H, theta, rho]= hough(f,'RhoResolution', 0.5,'ThetaResolution',0.75); %imshow(theta,rho,H,[],'notruesize'),axis on,axis normal%霍夫變換空間圖%xlabel('\theta'),ylabel('rho'); peak=houghpeaks(H,8,'threshold', ceil(0.3*max(H(:)))); %求出顯示霍夫變換矩陣中的8個極值點hold on lines=houghlines(f,theta,rho,peak);% 找原圖中的線 figure,imshow(f,[]),title('Hough Transform Detect Result'),hold onmax_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);%求直線斜率 plot(xy(:,1),xy(:,2),'LineWidth',4,'Color',[.6 .6 .6]); %繪製各條直線 end ang = atan(xielv)*180/pi%求各條直線角度
原始映像:
canny運算元邊緣檢測結果
霍夫變換取直線結果:
霍夫變換直線結果還是不明顯,對圖片識別度不高。