PSNR(Peak Signal to Noise Ratio,峰值信噪比)是一个表示信号最大功率和对其产生影响的噪声功率之间比值的术语,常用对数分贝来表示。PSNR常用作图像压缩领域中信号重建测量,它通过均方差(Mean Square Error,MSE)进行定义。两个M×N单色图像I和K,如果噪声近似,那么它们的MSE定义为:
PSNR的MATLAB代码如下:
% in1 Original image
% in2 reconstructed image
% mse Mean Square Error
% psnr Peak Signal to Noise Ratio
function result=psnr(in1,in2)
in1=imread('Original image');
in2=imread('reconstructed image');
%in1=rgb2gray(in1);
%in2=rgb2gray(in2);
z=mse(in1,in2);
result=10*log10(255.^2/z);
function z=mse(x,y)
x=double(x);
y=double(y);
[m,n]=size(x);
z=0;
for i=1:m
for j=1:n
z=z+(x(i,j)-y(i,j)).^2;
end
end
z=z/(m*n);
SSIM的MATLAB代码如下:
% im1 Original image
% im2 Reconstructed image
% ssim Structural Similarity Index
function [mssim, ssim_map]=ssim(img1, img2, K, window, L)
img1=imread('');
img2=imread('');
if (size(img1) ~=size(img2))
ssim_index=-Inf;
ssim_map=-Inf;
return;
end
[M N]=size(img1);
if (nargin==2)
if ((M<11) | (N<11))
ssim_index=-Inf;
ssim_map=-Inf;
return
end
window=fspecial('gaussian', 11, 1.5);
K(1)=0.01;% default settings
K(2)=0.03;
L=255;
end
if (nargin==3)
if ((M<11) | (N<11))
ssim_index=-Inf;
ssim_map=-Inf;
return
end
window=fspecial('gaussian', 11, 1.5);
L=255;
if (length(K)==2)
if (K(1)<0 | K(2)<0)
ssim_index=-Inf;
ssim_map=-Inf;
return;
end
else
ssim_index=-Inf;
ssim_map=-Inf;
return;
end
end
ffmpeg i myfile.png psnr target out.jpg
cwebp.exe tool to encode an image file (e.g. JPEG or PNG) into WebP
可以将测试用PNG图片转换成JPEG和Webp格式,经验证转换工具的测试结果与编程测试结果基本相同。