matlab achieve PSNR

@

1.PSNR principle

The PSNR, PSNR is usually used to evaluate the image quality as compared to the original, and after an image compression quality, of course, bound to the difference image of the original image quality after compression, so use of such a predetermined evaluation standard to a. The higher the PSNR, the smaller the distortion after compression. Here mainly defines two values, one is the MSE mean square error, and the other is the PSNR PSNR, the following equation:
Here Insert Picture Description
MAX here is generally grayscale image, a general is 255.

2.PSNR implementation code of matlab

The image reduction reamplified comparison, the following is the code:

close all;
clear all;
clc;

img=imread('lena.jpg');
[h w]=size(img);
imgn=imresize(img,[floor(h/2) floor(w/2)]);
imgn=imresize(imgn,[h w]);
img=double(img);
imgn=double(imgn);

B=8;                %编码一个像素用多少二进制位
MAX=2^B-1;          %图像有多少灰度级
MES=sum(sum((img-imgn).^2))/(h*w);     %均方差
PSNR=20*log10(MAX/sqrt(MES));           %峰值信噪比

The higher the PSNR, images and artwork closer.

3. matlab codes for color images of the PSNR

(a) may calculate the sum of the three channels R, G, B, and finally plurality MSE directly on the original formula dividing line 3 (OpenCV official code do so, the calculation result is the same as directly matlab).
(b) converting R, G, B format is the YCbCr, counting only the Y component (luminance component), the result is higher than the direct calculation of a few dB.
Paste codes, where the image is converted into the YCbCr format (only the Y component is calculated):

function [PSNR, MSE] = psnr(X, Y)
%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% 计算峰值信噪比PSNR
% 将RGB转成YCbCr格式进行计算
% 如果直接计算会比转后计算值要小2dB左右(当然是个别测试)
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%
 if size(X,3)~=1   %判断图像时不是彩色图,如果是,结果为3,否则为1
   org=rgb2ycbcr(X);
   test=rgb2ycbcr(Y);
   Y1=org(:,:,1);
   Y2=test(:,:,1);
   Y1=double(Y1);  %计算平方时候需要转成double类型,否则uchar类型会丢失数据
   Y2=double(Y2);
 else              %灰度图像,不用转换
     Y1=double(X);
     Y2=double(Y);
 end
 
if nargin<2    
   D = Y1;
else
  if any(size(Y1)~=size(Y2))
    error('The input size is not equal to each other!');
  end
 D = Y1 - Y2; 
end
MSE = sum(D(:).*D(:)) / numel(Y1); 
PSNR = 10*log10(255^2 / MSE);

Then write the main function

 X= imread('C:\Users\Administrator\Desktop\noise_image.jpg');
 Y= imread('C:\Users\Administrator\Desktop\actruel_image.jpg');
 psnr(X, Y)

Guess you like

Origin www.cnblogs.com/xiegaosen/p/12004629.html