五种实现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
最新评论