图像处理PSNR及其计算(OpenCV和matlab实现)

图像PSNR及其计算(OpenCV和matlab实现)

引言:在图像处理中,要对图像进行客观的评价,常常需要计算PSNR,本文将简单介绍PSNR的定义,并给出相关的源代码。

PSNR的概念

PSNR (Peak Signal to Noise Ratio)
峰值信噪比PSNR衡量图像失真或是噪声水平的客观标准。2个图像之间PSNR值越大,则越相似。普遍基准为30dB,30dB以下的图像劣化较为明显。定义为,

PSNR=10log10MAX2MSE

这里MAX表示图像颜色的最大数值,8bit图像取值为255。我们还要介绍MSE(均方差),即m×n单色图像 IK(原图像与处理图像)之间均方误差,定义为:

MSE=1mni=1nj=1mK(i,j)I(i,j)2

PSNR编程实现

matlab实现

第一种实现方法:
peaksnr = psnr(A,ref)
%peaksnr = psnr(A,ref) calculates the peak signal-to-noise ratio for the image A, with the image ref as the reference. A and ref must be of the same size and class.
第二种直观方法
  • 在matlab中新建m文件,下面这个实现简单明了
function [PSNR, MSE]=psnr(I,K)
[M,N,D] = size(I);
Diff = double(I)-double(K);
MSE = sum(Diff(:).^2)/numel(I);
PSNR=10*log10(255^2/MSE);
end
第三种实现方法:
  • 在matlab中新建m文件,复制进去保存就可以调用了。
function PSNR(A,B)

% PURPOSE: To find the PSNR (peak signal-to-noise ratio) between two
%          intensity images A and B, each having values in the interval
%          [0,1]. The answer is in decibels (dB).
%
%          There is also a provision, in EXAMPLE 3 below, for images 
%          stored in the interval [0,255], i.e. 256 gray levels. 
%
% SYNOPSIS: PSNR(A,B)
%
% DESCRIPTION: The following is quoted from "Fractal Image Compression",
%              by Yuval Fisher et al.,(Springer Verlag, 1995),
%              section 2.4, "Pixelized Data".
%
%              "...PSNR is used to measure the difference between two
%              images. It is defined as
%
%                           PSNR = 20 * log10(b/rms)
%
%              where b is the largest possible value of the signal
%              (typically 255 or 1), and rms is the root mean square
%              difference between two images. The PSNR is given in
%              decibel units (dB), which measure the ratio of the peak 
%              signal and the difference between two images. An increase
%              of 20 dB corresponds to a ten-fold decrease in the rms
%              difference between two images.
%              
%              There are many versions of signal-to-noise ratios, but
%              the PSNR is very common in image processing, probably
%              because it gives better-sounding numbers than other
%              measures."
%
% EXAMPLE 1: load clown
%            A = ind2gray(X,map); % Convert to an intensity image in [0,1].
%            B = 0.95 * A;        % Make B close to, but different from, A.
%            PSNR(A,B)            % ---> "PSNR = +33.49 dB"
%
% EXAMPLE 2: A = rand(256); % A is a random 256 X 256 matrix in [0,1].
%            B = 0.9 * A;   % Make B close to, but different from, A.
%            PSNR(A,B)      % ---> "PSNR = +24.76 dB (approx)"
%
% EXAMPLE 3: For images with 256 gray levels: this function PSNR was 
%            originally written for matrix-values between 0 and 1,
%            because of MATLAB's preference for that interval.
%
%            However, suppose the matrix has values in [0,255]. Taking
%            Example 1 above, we could change the image to 256 gray levels.
%         
%            load clown
%            A = ind2gray(X,map); % Convert to intensity image in [0,1]
%            AA = uint8(255*A);   % Change to integers in [0,255]
%            BB = 0.95*AA;        % Make BB close to AA.
%
%            Now we must alter the code for this new case. Comment out the
%            existing program (using %) and uncomment the alternative 
%            underneath it.
%
%            PSNR(AA,BB)          % ---> "PSNR = +33.56 dB"
%
%            Note the slightly different result from Example 1, because
%            decimal values were rounded into integers.

if A == B
   error('Images are identical: PSNR has infinite value')
end

max2_A = max(max(A));
max2_B = max(max(B));
min2_A = min(min(A));
min2_B = min(min(B));

if max2_A > 1 || max2_B > 1 || min2_A < 0 || min2_B < 0
   error('input matrices must have values in the interval [0,1]')
end

error_diff = A - B;
decibels = 20*log10(1/(sqrt(mean(mean(error_diff.^2)))));
disp(sprintf('PSNR = +%5.2f dB',decibels))

% if A == B
%    error('Images are identical: PSNR has infinite value')
% end

% max2_A = max(max(A));
% max2_B = max(max(B));
% min2_A = min(min(A));
% min2_B = min(min(B));
%
% if max2_A > 255 || max2_B > 255 || min2_A < 0 || min2_B < 0
%   error('input matrices must have values in the interval [0,255]')
% end

% error_diff = A - B;
% decibels = 20*log10(255/(sqrt(mean(mean(error_diff.^2)))));
% disp(sprintf('PSNR = +%5.2f dB',decibels))

OpenCV实现

  • 使用的是opencv 2.0版本,核心程序如下,很简单的程序,详细的注释可以帮助你理解getPSNR函数:
//输入格式是Mat类型,I1,I2代表是输入的两幅图像
double getPSNR(const Mat& I1, const Mat& I2)
{
    Mat s1;
    absdiff(I1, I2, s1);       // |I1 - I2|AbsDiff函数是 OpenCV 中计算两个数组差的绝对值的函数
    s1.convertTo(s1, CV_32F);  // 这里我们使用的CV_32F来计算,因为8位无符号char是不能进行平方计算
    s1 = s1.mul(s1);           // |I1 - I2|^2

    Scalar s = sum(s1);         //对每一个通道进行加和

    double sse = s.val[0] + s.val[1] + s.val[2]; // sum channels

    if( sse <= 1e-10) // 对于非常小的值我们将约等于0
        return 0;
    else
    {
        double  mse =sse /(double)(I1.channels() * I1.total());//计算MSE
        double psnr = 10.0*log10((255*255)/mse);
        return psnr;//返回PSNR
    }
}

可能需要添加的头文件是:

#include <opencv2/core/core.hpp>        // Basic OpenCV structures (cv::Mat, Scalar)
#include <opencv2/highgui/highgui.hpp>  // OpenCV window I/O

使用说明:

//先定义PSNR
double psnr;
//然后读取输入的两幅图像
Mat img1=imread('1.jpg');
Mat img2=imread('2.jpg');
//调用函数
psnr = getPSNR(img1,img2);  

参考资料

[1]http://www.opencv.org.cn/opencvdoc/2.3.2/html/doc/tutorials/highgui/video-input-psnr-ssim/video-input-psnr-ssim.html
[2]http://www.mathworks.com/matlabcentral/fileexchange/135-psnr

  • 22
    点赞
  • 147
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: PSNR(Peak Signal-to-Noise Ratio)和SSIM(Structural Similarity Index)是用来衡量两幅图像之间相似度的指标。 PSNR使用峰值信噪比来度量图像质量的一种方法。它使用均方误差(MSE)来衡量两幅图像之间的差异,MSE越小代表图像之间的差异越小,相似度越高。PSNR计算公式为: PSNR = 10 * log10((255^2) / MSE) 其中255是像素值的最大可能值,MSE为两幅图像像素之间差值的平方和的平均值。 SSIM是一种结构相似性指数,它不仅考虑了亮度的差异,还考虑了对比度和结构的差异。SSIM计算公式为: SSIM = (2 * μx * μy + c1) * (2 * σxy + c2) / ((μx^2 + μy^2 + c1) * (σx^2 + σy^2 + c2)) 其中μx和μy分别是两幅图像的平均值,σx^2和σy^2分别是两幅图像的方差,σxy是两幅图像的协方差,c1和c2是预定义的常数,用来避免分母为零的情况。 在MATLAB中,可以使用相关函数来计算PSNR和SSIM。比如,使用函数`imread`读取两幅图像,然后使用函数`imresize`来确保图像的大小一致,再使用函数`psnr`来计算PSNR使用函数`ssim`来计算SSIM。 示例代码如下: ```matlab image1 = imread('image1.jpg'); image2 = imread('image2.jpg'); image1 = imresize(image1, size(image2)); % 确保图像大小一致 psnrValue = psnr(image1, image2); ssimValue = ssim(image1, image2); ``` 以上代码读取了两幅图像,并将它们的大小调整为一致,然后分别计算PSNR和SSIM的值。 希望以上内容对您有所帮助。 ### 回答2: 图像相似度PSNR(Peak Signal-to-Noise Ratio)和SSIM(Structural Similarity Index)是常用于评估图像质量的指标,用于度量两幅图像之间的相似程度。以下是使用Matlab实现这两种相似度指标的方法: 1. PSNR实现PSNR是通过比较原始图像和重建图像的均方误差来评估图像质量的。在Matlab中,可以使用以下代码计算PSNR: ```matlab % 读取原始图像和重建图像 originalImage = imread('原始图像路径'); reconstructedImage = imread('重建图像路径'); % 计算MSE(均方误差) mse = sum(sum((double(originalImage) - double(reconstructedImage)).^2)) / (numel(originalImage)); % 计算PSNR psnr = 10 * log10((255^2) / mse); ``` 2. SSIM实现: SSIM通过比较原始图像和重建图像的亮度、对比度和结构相似性来评估图像质量的。在Matlab中,可以使用以下代码计算SSIM: ```matlab % 读取原始图像和重建图像 originalImage = imread('原始图像路径'); reconstructedImage = imread('重建图像路径'); % 计算SSIM [ssimValue, ~] = ssim(originalImage, reconstructedImage); ``` 需要注意的是,上述代码中的图像路径需要根据实际情况进行修改,同时保证原始图像和重建图像具有相同的大小。 这样就可以使用Matlab实现图像相似度PSNR和SSIM的计算了。 ### 回答3: 图像相似度是用来衡量两幅图像之间的相似程度的一种指标。PSNR(Peak Signal-to-Noise Ratio)和SSIM(Structural Similarity Index)是常用的图像相似度评价方法。在Matlab中,可以通过以下步骤实现这两种方法的计算。 首先,对于PSNR,可以通过以下几个步骤实现: 1. 使用imread函数读取待比较的两幅图像,分别命名为img1和img2。 2. 使用im2double函数将图像转换为double型的数据,并将像素值缩放到0-1范围内。 3. 计算两幅图像的均方误差(MSE),可以通过以下代码实现: mse = sum(sum((img1 - img2).^2)) / numel(img1); 其中,.^2表示对每个元素进行平方运算,numel函数用于计算图像的像素总数。 4. 计算PSNR值,使用以下代码实现psnr = 10 * log10(1^2 / mse); 其中,1表示图像的最大像素值。 其次,对于SSIM,可以通过以下几个步骤实现: 1. 使用ssim函数计算两幅图像的SSIM指数,可以通过以下代码实现: ssim_val = ssim(img1, img2); 其中,img1和img2为待比较的两幅图像。 2. 处理ssim_val的输出结果。 通过以上步骤,我们可以得到两幅图像之间的PSNR和SSIM值。这些值越高,表示两幅图像的相似度越高。需要注意的是,这两种方法都只能用于评价图像在感知上的相似度,并不能完全代替人眼对图像的主观判断。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值