MATLAB编程做图像处理类,新手,求大神帮助,不胜感激。

2025-03-11 15:40:21
推荐回答(1个)
回答1:

function z = PSNR(x,y)% calculate PSNR of image
[M N] = size(x);
dx = im2double(x);
dy = im2double(y);
err = dx - dy;
MSE = sum(sum(err.^2)) / (M * N); %mean square error
z = 10*log10(255^2 / MSE);
end

clear all;
close all;
clc;
pic=imread('4.jpg');
data=rgb2gray(pic);
P1=imnoise(data,'gaussian',0.02) ;
P2=imnoise(data,'salt & pepper',0.02) ;
figure ;
subplot(1,3,1);imshow(data);title('原图');
subplot(1,3,2);imshow(P1);title('高斯加噪');
subplot(1,3,3);imshow(P2);title('椒盐加噪');

figure;
k1=medfilt2(data,[3 3]);
k2=medfilt2(P1,[3 3]);
k3=medfilt2(P2,[3 3]);
r1=PSNR(data, k1);
r2=PSNR(data, k2);
r3=PSNR(data, k3);
subplot(1,3,1);imshow(k1);title(['原图中值滤波' 'psnr:' num2str(r1)]);
subplot(1,3,2);imshow(k2);title(['高斯加噪中值滤波' 'psnr:' num2str(r2)]);
subplot(1,3,3);imshow(k3);title(['椒盐加噪中值滤波' 'psnr:' num2str(r3)]);

figure;
d1= filter2(fspecial('average',3),data)/255;
d2= filter2(fspecial('average',3),P1)/255;
d3= filter2(fspecial('average',3),P2)/255;
r1=PSNR(data, d1);
r2=PSNR(data, d2);
r3=PSNR(data, d3);
subplot(1,3,1);imshow(d1);title(['原图均值滤波' 'psnr:' num2str(r1)]);
subplot(1,3,2);imshow(d2);title(['高斯加噪均值滤波' 'psnr:' num2str(r2)]);
subplot(1,3,3);imshow(d3);title(['椒盐加噪均值滤波' 'psnr:' num2str(r3)]);