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