边缘检测算子和小波变换提取图像边缘【matlab】
Roberts边缘检测算子:根据一对互相垂直方向上的差分可用来计算梯度的原理,采用对角线方向相邻两像素之差。
小波变换的方法比较适用于展现夹带在正常信号中的瞬间反常现象,具有方向敏感性。所以可以边缘检测。
Roberts边缘检测算子:
clear; I=imread('D:\文件及下载相关\图片\gray2.png'); I=rgb2gray(I); grayPic=mat2gray(I); [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')
小波变换检测边缘:
clear all; I = imread('D:\文件及下载相关\图片\gray2.png'); I = rgb2gray(I);imshow(I); I1 = imadjust(I,stretchlim(I),[0,1]);figure;imshow(I1); [N,M] = size(I); h = [0.125,0.375,0.375,0.125]; g = [0.5,-0.5]; delta = [1,0,0]; J = 3; a(1:N,1:M,1,1:J+1) = 0; dx(1:N,1:M,1,1:J+1) = 0; dy(1:N,1:M,1,1:J+1) = 0; d(1:N,1:M,1,1:J+1) = 0; a(:,:,1,1) = conv2(h,h,I,'same'); dx(:,:,1,1) = conv2(delta,g,I,'same'); dy(:,:,1,1) = conv2(g,delta,I,'same'); x = dx(:,:,1,1); y = dy(:,:,1,1); d(:,:,1,1) = sqrt(x.^2+y.^2); I1 = imadjust(d(:,:,1,1),stretchlim(d(:,:,1,1)),[0 1]);figure;imshow(I1); lh = length(h); lg = length(g); for j = 1:J+1 lhj = 2^j*(lh-1)+1; lgj = 2^j*(lg-1)+1; hj(1:lhj)=0; gj(1:lgj)=0; for n = 1:lh hj(2^j*(n-1)+1)=h(n); end for n = 1:lg gj(2^j*(n-1)+1)=g(n); end a(:,:,1,j+1) = conv2(hj,hj,a(:,:,1,j),'same'); dx(:,:,1,j+1) = conv2(delta,gj,a(:,:,1,j),'same'); dy(:,:,1,j+1) = conv2(gj,delta,a(:,:,1,j),'same'); x = dx(:,:,1,j+1); y = dy(:,:,1,j+1); dj(:,:,1,j+1) = sqrt(x.^2+y.^2); I1 = imadjust(dj(:,:,1,j+1),stretchlim(dj(:,:,1,j+1)),[0 1]); figure;imshow(I1); end
图像边缘检测经典算子及MATLAB实现 - CSDN博客 http://blog.csdn.net/u012808193/article/details/45722283
边缘检测是图像分析和模式识别的主要特征提取手段。所谓边缘是指其周围像素灰度后阶变化或屋顶状变化的那些像素的集合,它存在于目标与背景、目标与目标、区域与区域,基元与基元之间。 因此它是图象分割所依赖的重要的特征,也是纹理特征的重要信息源和形状特征的基础;而图象的纹理形状特征的提取又常常依赖于图象分割。 图象的边缘提取也是图象匹配的基础,因为它是位置的标志,对灰度的变化不敏感,它可作为匹配的特征点。
目前常用的边缘检测方法有:Roberts边缘检测算子、Sobel边缘检测算子、Prewitt边缘检测算子、Laplace边缘检测算子等等。
1、Roberts边缘检测算子
Roberts边缘检测算子根据任意一对互相垂直方向上的差分可用来计算梯度的原理,采用对角线方向相邻两像素之差。
MATLAB实验代码如下:
clear; sourcePic=imread('lena.jpg'); %读取原图像 grayPic=mat2gray(sourcePic); %实现图像矩阵的归一化操作 [m,n]=size(grayPic); newGrayPic=grayPic;%为保留图像的边缘一个像素 robertsNum=0; %经roberts算子计算得到的每个像素的值 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算子的处理结果')
实验结果:
2、Sobel边缘检测算子
对数字图像的每个像素,考察它上、下、左、右邻点灰度的加权差,与之接近的邻点的权大。
MATLAB实验代码如下:
clear; sourcePic=imread('lena.jpg');%读取原图像 grayPic=mat2gray(sourcePic);%实现图像矩阵的归一化操作 [m,n]=size(grayPic); newGrayPic=grayPic;%为保留图像的边缘一个像素 sobelNum=0;%经sobel算子计算得到的每个像素的值 sobelThreshold=0.8;%设定阈值 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算子的处理结果')
实验结果:
3、Prewitt边缘检测算子
Prewitt算子是一种边缘样板算子。这些算子样板由理想的边缘子图像构成。依次用边缘样板去检测图像,与被检测区域最为相似的样板给出最大值。
MATLAB实验代码
clear; sourcePic=imread('lena.jpg');%读取原图像 grayPic=mat2gray(sourcePic);%实现图像矩阵的归一化操作 [m,n]=size(grayPic); newGrayPic=grayPic;%为保留图像的边缘一个像素 PrewittNum=0;%经Prewitt算子计算得到的每个像素的值 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算子的处理结果')
实验结果
4、Laplace边缘检测算子
Laplacian 算子是n维欧几里德空间中的一个二阶微分算子。
MATLAB实验代码
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算子的处理结果')
实验结果