五種實現matlab邊緣檢測算法:
方法一:
matlab自帶的edge函數:
將圖片保存為lena.jpg
I=imread('lena.jpg');%提取圖像 img=rgb2gray(I); [m,n]=size(img); BW1=edge(img,'sobel'); %用Sobel算子進行邊緣檢測 BW2=edge(img,'roberts');%用Roberts算子進行邊緣檢測 BW3=edge(img,'prewitt'); %用Prewitt算子進行邊緣檢測 BW4=edge(img,'log'); %用Log算子進行邊緣檢測 BW5=edge(img,'canny'); %用Canny算子進行邊緣檢測 h=fspecial('gaussian',5);%?高斯濾波 BW6=edge(img,'canny');%高斯濾波后使用Canny算子進行邊緣檢測 subplot(2,3,1), imshow(BW1); title('sobel edge check'); subplot(2,3,2), imshow(BW2); title('roberts edge check'); subplot(2,3,3), imshow(BW3); title('prewitt edge check'); subplot(2,3,4), imshow(BW4); title('log edge check'); subplot(2,3,5), imshow(BW5); title('canny edge check'); subplot(2,3,6), imshow(BW6); title('gasussian&canny edge check');
效果如下圖所示:
方法二:Laplacian算法
clear; sourcePic=imread('lena.jpg');%圖像讀入 grayPic=mat2gray(sourcePic);%實現圖像的矩陣歸一化操作 [m,n]=size(grayPic); newGrayPic=grayPic; LaplacianNum=0;%經Laplacian操作得到的每個像素的值 LaplacianThreshold=0.2;%設定閾值 for j=2:m-1 %進行邊界提取 for k=2:n-1 LaplacianNum=abs(4*grayPic(j,k)-grayPic(j-1,k)-grayPic(j+1,k)-grayPic(j,k+1)-grayPic(j,k-1)); if(LaplacianNum > LaplacianThreshold) newGrayPic(j,k)=255; else newGrayPic(j,k)=0; end end end figure,imshow(newGrayPic); title('Laplacian算子的處理結果')
效果圖如下:
方法三:Prewitt算法
%Prewitt 算子的實現: clear; sourcePic=imread('lena.jpg'); grayPic=mat2gray(sourcePic); [m,n]=size(grayPic); newGrayPic=grayPic; PrewittNum=0; PrewittThreshold=0.5;%設定閾值 for j=2:m-1 %進行邊界提取 for k=2:n-1 PrewittNum=abs(grayPic(j-1,k+1)-grayPic(j+1,k+1)+grayPic(j-1,k)-grayPic(j+1,k)+grayPic(j-1,k-1)-grayPic(j+1,k-1))+abs(grayPic(j-1,k+1)+grayPic(j,k+1)+grayPic(j+1,k+1)-grayPic(j-1,k-1)-grayPic(j,k-1)-grayPic(j+1,k-1)); if(PrewittNum > PrewittThreshold) newGrayPic(j,k)=255; else newGrayPic(j,k)=0; end end end figure,imshow(newGrayPic); title('Prewitt算子的處理結果')
效果圖如下:
方法四:Sobel算法
%Sobel 算子的實現: clear; sourcePic=imread('lena.jpg'); grayPic=mat2gray(sourcePic); [m,n]=size(grayPic); newGrayPic=grayPic; sobelNum=0; sobelThreshold=0.7; for j=2:m-1 for k=2:n-1 sobelNum=abs(grayPic(j-1,k+1)+2*grayPic(j,k+1)+grayPic(j+1,k+1)-grayPic(j-1,k-1)-2*grayPic(j,k-1)-grayPic(j+1,k-1))+abs(grayPic(j-1,k-1)+2*grayPic(j-1,k)+grayPic(j-1,k+1)-grayPic(j+1,k-1)-2*grayPic(j+1,k)-grayPic(j+1,k+1)); if(sobelNum > sobelThreshold) newGrayPic(j,k)=255; else newGrayPic(j,k)=0; end end end figure,imshow(newGrayPic); title('Sobel算子的處理結果')
效果如下:
方法五:Roberts 算子的實現
%Roberts 算子的實現: clear all; clc; sourcePic=imread('lena.jpg'); grayPic=mat2gray(sourcePic); [m,n]=size(grayPic); newGrayPic=grayPic; robertsNum=0; robertThreshold=0.2; for j=1:m-1 for k=1:n-1 robertsNum = abs(grayPic(j,k)-grayPic(j+1,k+1)) + abs(grayPic(j+1,k)-grayPic(j,k+1)); if(robertsNum > robertThreshold) newGrayPic(j,k)=255; else newGrayPic(j,k)=0; end end end figure,imshow(newGrayPic); title('roberts算子的處理結果')
效果圖:
參考:https://www.cnblogs.com/leegod/p/8109023.html