求M文件,直接求图像的峰值信噪比(无论灰度和彩色)

2024-11-18 12:34:04
推荐回答(3个)
回答1:

>> close all
>> clear all
>> I=imread('lena.bmp');
>> J=imnoise(I,'gaussian',0.01,0.005);
>> A=imread('lena.bmp');
[M,N]=size(A);
B = imread('J.bmp');
a=double(A);
b=double(B);
sum=0;
for i=1:M;
for j=1:N;
sum=sum+(a(i,j)-b(i,j))^2;
end;
end;
mseValue=sum/(M*N);
psnrValue=10*log10(255^2/mseValue);
disp(['输入数据的MSE为:',num2str(mseValue)]);
disp(['输入数据的PSNR为:',num2str(psnrValue)]);

输入数据的MSE为:7915.4387
输入数据的PSNR为:9.1461

以上是MATLAB程序 及其输出结果 M文件可为
function PSNR = PSNR(A,B)
[M,N]=size(A);
x=double(A);
y=double(B);
sum=0;
for i=1:M;
for j=1:N;
sum=sum+(x(i,j)-y(i,j))^2;
end;
end;
mseValue=sum/(M*N);
psnrValue=10*log10(255^2/mseValue);
disp(['输入数据的MSE为:',num2str(mseValue)]);
disp(['输入数据的PSNR为:',num2str(psnrValue)]);

回答2:

[a,b,c]=size(I);%i为所要求的图片
snr=0;
if c==1;%灰度图像
Ps=sum(sum((I-mean(mean(I))).^2));%原始信号
Pn=sum(sum((I-In).^2))%;in为噪声信号
snr=10*log10(Ps/Pn);
elseif c==3;%彩色图像for i=1:3
Ps=sum(sum((I(:,:,i)-mean(mean(I(:,:,i)))).^2));%原始信号
Pn=sum(sum((I(:,:,i)-In(:,:,i)).^2));;%in为噪声信号
snr=snr+10*log10(Ps/Pn);
end
snr=snr/3;
end

这个代码涉及到的信噪比是新的图像与原始图像的比较

回答3:

function [ PSNR,MSE ] = Psnr( im1,im2 )

%------------------------计算峰值信噪比程序———————————————-----
% im1 : the original image matrix
% im2 : the modified image matrix

if (size(im1))~=(size(im2))
error('错误:两个输入图象的大小不一致');
end

[m,n] = size(im1);
A = double(im1);
B = double(im2);
D = sum( sum( (A-B).^2 ) );%||A-B||^2
MSE = D / (m * n);
if D == 0
error('两幅图像完全一样');
PSNR = 200;
else
PSNR = 10*log10( (255^2) / MSE );
end