J0=imnoise(I,'gaussian')
J1=imnoise(I,'salt &pepper')
J0J3=imfilter(J0,fspecial('average'))
J0J5=imfilter(J0,fspecial('average',[5 5]))
J0Z3=medfilt2(J0)
J0Z5=medfilt2(J0,[5 5])
J1J3=imfilter(J1,fspecial('average'))
J1J5=imfilter(J1,fspecial('average',[5 5]))
J1Z3=medfilt2(J1)
J1Z5=medfilt2(J1,[5 5])
subplot(2,2,1),imshow(I)
title('原始图像')
subplot(2,2,2),imshow(J0)
title('加入零均值高斯噪声')
subplot(2,2,3),imshow(J1)
title('加入椒盐噪声')
figure,
subplot(2,2,1),imshow(J0J3)
title('对高斯噪声,采用3x3均值滤波')
subplot(2,2,2),imshow(J0J5)
title('对高斯噪声,采用5x5均值滤波')
subplot(2,2,3),imshow(J0Z3)
title('对高斯噪声,采用3x3中值滤波')
subplot(2,2,4),imshow(J0Z5)
title('对高斯噪声,宽拆采用5x5中慎纳枣值滤波')
figure,
subplot(2,2,1),imshow(J1J3)
title('对椒盐噪声,采用3x3均值茄败滤波')
subplot(2,2,2),imshow(J1J5)
title('对椒盐噪声,采用5x5均值滤波')
subplot(2,2,3),imshow(J1Z3)
title('对椒盐噪声,采用3x3中值滤波')
subplot(2,2,4),imshow(J1Z5)
title('对椒盐噪声,采用5x5中值滤波')
%之前写的一个程序,这里面用Sobel算子和Prewitt算子的部分就是对图像锐化得到边缘的了。也可以直接用matlab自带的函数S = edge(I, 'sobel')进行锐化。
clcclose all
clear all
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%读入图像
f=imread('Pictures/4_m03.tif')
%f=imread('Pictures/5_m01.tif')
%f=imread('Pictures/10_m02.tif')
%f=imread('Pictures/22_m03.tif')
f=imresize(f,0.25)
f=im2double(f)
[m,n]=size(f)
subplot(2,3,1),imshow(f)
title('原始图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%生成高斯平滑滤波模板
hg=zeros(3,3) %设定模板大小3*3
delta=0.5
for x=1:1:3
for y=1:1:3
u=x-2
v=y-2
谨带 hg(x,y)=exp(-(u^2+v^2)/(2*pi*delta^2))
end
end
h=hg/sum(hg(:))
%高斯滤波
ftemp=zeros(m,n)
rowhigh=m-1
colhigh=n-1
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1) f(x,y-1) f(x,y) f(x,y+1)f(x+1,y-1) f(x+1,y) f(x+1,y+1)]
竖迹 A=h.*mod
ftemp(x,y)=sum(A(:))
end
end
f=ftemp
subplot(2,3,4),imshow(f)
title('高斯滤波后的图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用Sobel算子进行边缘检测
sx=[-1 0 1-2 0 2-1 0 1]
sy=[-1 -2 -10 0 01 2 1]
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1) 祥纤芦f(x,y-1) f(x,y) f(x,y+1)f(x+1,y-1) f(x+1,y) f(x+1,y+1)]
fsx=sx.*mod
fsy=sy.*mod
ftemp(x,y)=max((abs(sum(fsx(:)))),(abs(sum(fsy(:)))))
end
end
fs=im2uint8(ftemp)
subplot(2,3,2),imshow(fs)
title('Sobel算子进行边缘检测的原始图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%阈值分割
TH1=140 %设定阈值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH1)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200
elseif(fs(x,y)>=TH1)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200
else fs(x,y)=50
end
end
end
subplot(2,3,5),imshow(fs)
title('Sobel算子边缘检测并细化后的图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%利用Prewitt算子进行边缘检测
sx=[1 0 -11 0 -11 0 -1]
sy=[-1 -1 -10 0 01 1 1]
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
mod=[f(x-1,y-1) f(x-1,y) f(x-1,y+1) f(x,y-1) f(x,y) f(x,y+1)f(x+1,y-1) f(x+1,y) f(x+1,y+1)]
fsx=sx.*mod
fsy=sy.*mod
ftemp(x,y)=max((abs(sum(fsx(:)))),(abs(sum(fsy(:)))))
end
end
fs=im2uint8(ftemp)
subplot(2,3,3),imshow(fs)
title('Prewitt算子进行边缘检测的原始图像')
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%阈值分割
TH1=100 %设定阈值
for x=2:1:rowhigh-1
for y=2:1:colhigh-1
if (fs(x,y)>=TH1)&((fs(x,y-1) <= fs(x,y)) & (fs(x,y) > fs(x,y+1)) )
fs(x,y)=200
elseif(fs(x,y)>=TH1)&( (fs(x-1,y) <=fs(x,y)) & (fs(x,y) >fs(x+1,y)))
fs(x,y)=200
else fs(x,y)=50
end
end
end
subplot(2,3,6),imshow(fs)
title('Prewitt算子边缘检测并细化后的图像')
clc,cleara
=
1:1:6
%横坐标
b
=
[8.0
9.0
10.0
15.0
35.0
40.0]
%纵坐标
plot(a,
b,
'b')
%自物漏然状态的画图效果
hold
on
%第一种带举,画平滑曲蠢蚂碧线的方法
c
=
polyfit(a,
b,
2)
%进行拟合,c为2次拟合后的系数
d
=
polyval(c,
a,
1)
%拟合后,每一个横坐标对应的值即为d
plot(a,
d,
'r')
%拟合后的曲线
plot(a,
b,
'*')
%将每个点
用*画出来
hold
on
%第二种,画平滑曲线的方法
values
=
spcrv([[a(1)
a
a(end)][b(1)
b
b(end)]],3)
plot(values(1,:),values(2,:),
'g')
建议学会使用搜索引擎,百度“matlab曲线平滑“出来一堆方法
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)