MATLAB数字图像处理(三) |
您所在的位置:网站首页 › matlab抠图步骤 › MATLAB数字图像处理(三) |
文章目录
二值图像轮廓提取灰度图像边缘检测含噪图像边缘检测均值滤波函数
二值图像轮廓提取
根据掏空内部点算法,运用Matlab编程实现二值图像的轮廓提取。(以二值图像circles为例) I = imread('circles.png'); subplot(1,2,1),imshow(I); [M,N]=size(I); Ic=I; for i = 2:M-1 for j=2:N-1 if(I(i,j)==255&I(i+1,j)==255&I(i-1,j)==255&I(i,j+1)==255&I(i,j-1)==255&I(i+1,j+1)==255&I(i+1,j-1)==255&I(i-1,j+1)==255&I(i-1,j-1)==255) Ic(i,j)=0; end end end subplot(1,2,2),imshow(Ic);
以灰度图像rice为例,利用Matlab图像处理工具箱中的edge函数,分别使用Roberts 算子、Sobel算子、Prewitt 算子对其进行边缘检测。 I=imread('rice.png'); subplot(2,2,1),imshow(I); BW1=edge(I,'roberts'); BW2=edge(I,'prewitt'); BW3=edge(I,'sobel'); subplot(2,2,2),imshow(BW1),title('roberts算子'); subplot(2,2,3),imshow(BW2),title('prewitt算子'); subplot(2,2,4),imshow(BW3),title('sobel算子');
若向原始图像rice中加入高斯噪声,之后再对噪声图像分别运用Roberts 算子、Sobel算子、Prewitt 算子、Log算子(高斯-拉普拉斯算子)进行边缘检测,观察检测结果,试比较4种边缘检测算子的抗噪声干扰能力。 I=imread('rice.png'); subplot(2,3,1),imshow(I); G=imnoise(I,'gaussian'); subplot(2,3,2),imshow(G); BW1=edge(G,'roberts'); BW2=edge(G,'prewitt'); BW3=edge(G,'sobel'); BW4=edge(G,'log'); subplot(2,3,3),imshow(BW1),title('roberts算子'); subplot(2,3,4),imshow(BW2),title('prewitt算子'); subplot(2,3,5),imshow(BW3),title('sobel算子'); subplot(2,3,6),imshow(BW4),title('log算子');
根据均值滤波的基本原理自定义均值滤波函数d=avefilt(x,n),x为输入图像矩阵,n为模板大小(即n×n),d为输出图像矩阵。并调用该函数对含有高斯噪声的图像进行均值滤波处理。 % avefilt.m function d=avefilt(x,n) a=ones(n);%生成n*n的全1矩阵 [M,N]=size(x); x1=double(x); x2=x1; for i=1:M-n+1 for j=1:N-n+1 c=x1(i:i+n-1,j:j+n-1).*a; s=sum(sum(c));%求c矩阵所有元素的和 x2(i+fix((n-1)/2),fix(j+(n-1)/2))=s/(n*n);%使中心元素=均值,fix()向下取整 end end d=uint8(x2);%四舍五入取整映射到0~255范围 % ex3_4.m ori=imread('eight.tif'); Gao=imnoise(ori,'gaussian'); subplot(1,3,1),imshow(ori); subplot(1,3,2),imshow(Gao); after=avefilt(Gao,3); subplot(1,3,3),imshow(after);
|
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |