数字图像处理|Matlab-空域增强实验-彩色图像的去噪
程序员文章站
2022-05-21 13:49:05
...
Matlab-空域增强实验-彩色图像的去噪
代码链接:https://download.csdn.net/download/qq_43571150/12033259
问题1:
对一副彩色图像分别添加高斯噪声和椒盐噪声,输出结果图像;
I=imread('05.jpg');
[width,height,z]=size(I);
if(z>1)
I1=rgb2gray(I);
end
%高斯噪声
av=0;
std=0.1;
u1=rand(width,height);
u2=rand(width,height);
x=std*sqrt(-2*log(u1)).*cos(2*pi*u2)+av;
result1=double(I1)/255+x;
result1=uint8(255*result1);
%椒盐噪声
result2=I1;
k1=0.1;
k2=0.3;
a1=rand(width,height)<k1;
a2=rand(width,height)<k2;
result2(a1&a2)=0;
result2(a1& ~a2)=255;
subplot(2,2,1);imshow(I);title('原图');
subplot(2,2,2);imshow(I1);title('灰度图');
subplot(2,2,3);imshow(result1);title('加高斯噪声后');
subplot(2,2,4);imshow(result2);title('加椒盐噪声后');
imwrite(result1,'05 高斯.jpg');
imwrite(result2,'05 椒盐.jpg');
问题2:
采用K-NN均值滤波对二种噪声图像进行去噪,测试3组不同的参数,输出结果图像,并分析实验效果;
I=imread('01.jpg');%读取图像
G=imread('01 高斯.jpg');%高斯噪声图
J=imread('01 椒盐.jpg');%椒盐噪声图
figure('name','对高斯噪声进行均值滤波处理');
subplot(3,3,1);
imshow(I);
title('原图像');
%高斯图像
subplot(3,3,2);
imshow(G);
title('高斯噪声图像');
subplot(3,3,4);
C1=avg_filter(G,3); %尺寸3
imshow(C1,[]);
title('高斯噪声图均值滤波处理 尺寸3');
imwrite(C1,'01 高斯噪声图均值滤波处理 尺寸3.jpg');
subplot(3,3,5);
C2=avg_filter(G,5); %尺寸6
imshow(C2,[]);
title('高斯噪声图均值滤波处理 尺寸5');
imwrite(C2,'01 高斯噪声图均值滤波处理 尺寸5.jpg');
subplot(3,3,6);
C3=avg_filter(G,9); %尺寸9
imshow(C3,[]);
title('高斯噪声图均值滤波处理 尺寸9');
imwrite(C3,'01 高斯噪声图均值滤波处理 尺寸9.jpg');
%椒盐图像
subplot(3,3,3);
imshow(J);
title('椒盐噪声图像');
subplot(3,3,7);
C4=avg_filter(J,3); %尺寸3
imshow(C4,[]);
title('椒盐噪声图均值滤波处理 尺寸3');
imwrite(C4,'01 椒盐噪声图均值滤波处理 尺寸3.jpg');
subplot(3,3,8);
C5=avg_filter(J,5); %尺寸5
imshow(C5,[]);
title('椒盐噪声图均值滤波处理 尺寸5');
imwrite(C5,'01 椒盐噪声图均值滤波处理 尺寸5.jpg');
subplot(3,3,9);
C6=avg_filter(J,9); %尺寸9
imshow(C6,[]);
title('椒盐噪声图均值滤波处理 尺寸9');
imwrite(C6,'01 椒盐噪声图均值滤波处理 尺寸9.jpg');
%均值滤波函数
function d=avg_filter(x,n)
a(1:n,1:n)=1; %a即n×n模板,元素全是1
[height, width]=size(x); %输入图像是hight x width的,且hight>n,width>n
x1=double(x);
x2=x1;
for i=1:height-n+1
for j=1:width-n+1
c=x1(i:i+(n-1),j:j+(n-1)).*a; %取出x1中从(i,j)开始的n行n列元素与模板相乘
s=sum(sum(c)); %求c矩阵中各元素之和
x2(i+(n-1)/2,j+(n-1)/2)=s/(n*n); %将与模板运算后的各元素的均值赋给模板中心位置的元素
end
end
%未被赋值的元素取原值
d=uint8(x2);
end
问题3:
采用K-NN中值滤波对二种噪声图像进行去噪,测试3组不同的参数,输出结果图像,并分析实验效果。
I=imread('05.jpg');%读取图像
G=imread('05 高斯.jpg');%高斯噪声图
J=imread('05 椒盐.jpg');%椒盐噪声图
figure('name','对高斯噪声进行中值滤波处理');
subplot(3,3,1);
imshow(I);
title('原图像');
%高斯图像
subplot(3,3,2);
imshow(G);
title('高斯噪声图像');
subplot(3,3,4);
C1=median_filter(G,3); %尺寸3
imshow(C1,[]);
title('高斯噪声图中值滤波处理 尺寸3');
imwrite(C1,'05 高斯噪声图中值滤波处理 尺寸3.jpg');
subplot(3,3,5);
C2=median_filter(G,5); %尺寸6
imshow(C2,[]);
title('高斯噪声图中值滤波处理 尺寸5');
imwrite(C2,'05 高斯噪声图中值滤波处理 尺寸5.jpg');
subplot(3,3,6);
C3=median_filter(G,9); %尺寸9
imshow(C3,[]);
title('高斯噪声图中值滤波处理 尺寸9');
imwrite(C3,'05 高斯噪声图中值滤波处理 尺寸9.jpg');
%椒盐图像
subplot(3,3,3);
imshow(J);
title('椒盐噪声图像');
subplot(3,3,7);
C4=median_filter(J,3); %尺寸3
imshow(C4,[]);
title('椒盐噪声图中值滤波处理 尺寸3');
imwrite(C4,'05 椒盐噪声图中值滤波处理 尺寸3.jpg');
subplot(3,3,8);
C5=median_filter(J,5); %尺寸6
imshow(C5,[]);
title('椒盐噪声图中值滤波处理 尺寸5');
imwrite(C5,'05 椒盐噪声图中值滤波处理 尺寸5.jpg');
subplot(3,3,9);
C6=median_filter(J,9); %尺寸9
imshow(C6,[]);
title('椒盐噪声图中值滤波处理 尺寸9');
imwrite(C6,'05 椒盐噪声图中值滤波处理 尺寸9.jpg');
function d=median_filter(x,n)
d=x;
[width,height]=size(x);%得到图像的长和宽
for ii=1:width-(n-1)
for jj=1:height-(n-1)%height表示的个数为可完整滤波的格子数
tmp1=d(ii:ii+(n-1),jj:jj+(n-1));%取出要滤波的n*n的方阵
tmp2=tmp1(1,:);
for kk=2:n
tmp2=[tmp2,tmp1(kk,:)];%把所有的行排成一行方便后面求中值
end
y= median(tmp2);
d(ii+(n-1)/2,jj+(n-1)/2)=y;
end
end
end
上一篇: mysql的排序问题
下一篇: php导入csv文件以及碰到乱码解决方法