%灰度圖像的最大像素值是255, 而不是512。
%另外計算MSE也不用那麽麻煩
%下面是計算 MSE和PSNR的程序,我拿壹張圖片試了壹下,結果肯定是不壹樣的。
%還有統計像素值出現的次數,用直方圖imhist會更簡單點,這個程序我沒寫。
I=imread('boat.bmp');
J=imnoise(I,'gaussian',0,0.01);
Jg=double(J);
%均值濾波
k1=filter2(fspecial('average',3),Jg);
k2=filter2(fspecial('average',5),Jg);
figure,subplot(221),imshow(I);title('原圖');
subplot(222),imshow(J);title('加入高斯白噪聲以後的圖像');
subplot(223),imshow(uint8(k1));title('3*3模板均值濾波');
subplot(224),imshow(uint8(k2));title('5*5模板均值濾波');
%中值濾波
k3=medfilt2(Jg,[3 3]);
k4=medfilt2(Jg,[5 5]);
figure,subplot(221),imshow(I);title('原圖');
subplot(222),imshow(J);title('加入高斯白噪聲以後的圖像');
subplot(223),imshow(uint8(k3));title('3*3模板中值濾波');
subplot(224),imshow(uint8(k4));title('5*5模板中值濾波');
%計算均值濾波後圖像的PMSE與PSNR
diff=(double(I)-k1).^2;
mse=mean(diff(:));
pmse_avg=mse;
max_value=max(k1(:));
psnr=double(10*log(max_value*max_value/mse)/log(10));
%計算中值濾波後的各項指標
diff=(double(I)-k3).^2;
mse=mean(diff(:));
max_value=max(k3(:));
pmse_mid=mse;
psnr_mid=double(10*log(max_value*max_value/mse)/log(10));