matlab中中图像PSNR和SSIM的计算

图像处理结果的度量 —— SNR、PSNR、SSIM


网上找了很多关于PSNR和SSIM的计算,很多结果算出来都不一样,公式都是普遍的,如下:

现在总结下造成结果差异的原因。


PSNR的差异:

1.灰度图像:灰度图像比较好计算,只有一个灰度值。


2.彩色图像:

(a)可以将分别计算R,G,B三个通道总和,最后MSE直接在原公式上多除以3就行(opencv官方代是这么做的,与matlab直接计算结果是一样的)。

(b)将R,G,B格式转换为YCbCr,只计算Y分量(亮度分量),结果会比直接计算要高几个dB。


贴代码,这里是将图片格式转成YCbCr(只计算Y分量):


[cpp]  view plain  copy
  1. function [PSNR, MSE] = psnr(X, Y)  
  2. %%%%%%%%%%%%%%%%%%%%%%%%%%%  
  3. %  
  4. % 计算峰值信噪比PSNR  
  5. % 将RGB转成YCbCr格式进行计算  
  6. % 如果直接计算会比转后计算值要小2dB左右(当然是个别测试)  
  7. %  
  8. %%%%%%%%%%%%%%%%%%%%%%%%%%%  
  9.  if size(X,3)~=1   %判断图像时不是彩色图,如果是,结果为3,否则为1  
  10.    org=rgb2ycbcr(X);  
  11.    test=rgb2ycbcr(Y);  
  12.    Y1=org(:,:,1);  
  13.    Y2=test(:,:,1);  
  14.    Y1=double(Y1);  %计算平方时候需要转成double类型,否则uchar类型会丢失数据  
  15.    Y2=double(Y2);  
  16.  else              %灰度图像,不用转换  
  17.      Y1=double(X);  
  18.      Y2=double(Y);  
  19.  end  
  20.    
  21. if nargin<2      
  22.    D = Y1;  
  23. else  
  24.   if any(size(Y1)~=size(Y2))  
  25.     error('The input size is not equal to each other!');  
  26.   end  
  27.  D = Y1 - Y2;   
  28. end  
  29. MSE = sum(D(:).*D(:)) / numel(Y1);   
  30. PSNR = 10*log10(255^2 / MSE);  

控制台输入下面三条语句:


[cpp]  view plain  copy
  1. >> X= imread('C:\Users\Administrator\Desktop\noise_image.jpg');  
  2. >> Y= imread('C:\Users\Administrator\Desktop\actruel_image.jpg');  
  3. >> psnr(X, Y)  

SSIM的差异:同上,如果直接不转换成YCbCr格式,结果会偏高很多( matlab中,SSIM提出者【1】,代码 )。opencv里面是分别计算了R,G,B三个分量的SSIM值( 官方代码 )。最后我将3个值取了个平均(这个值比matlab里面低很多)。


以下代码主要是参考原作者修改的,源代码是直接没有进行格式转换,直接RGB格式,下面我是将他转换成YCbCr计算图片的SSIM


[cpp]  view plain  copy
  1. function [mssim, ssim_map] = ssim(img1, img2, K, window, L)  
  2.   
  3. %========================================================================  
  4. %SSIM Index, Version 1.0  
  5. %Copyright(c) 2003 Zhou Wang  
  6. %All Rights Reserved.  
  7. %  
  8. %The author is with Howard Hughes Medical Institute, and Laboratory  
  9. %for Computational Vision at Center for Neural Science and Courant  
  10. %Institute of Mathematical Sciences, New York University.  
  11. %  
  12. %----------------------------------------------------------------------  
  13. %Permission to use, copy, or modify this software and its documentation  
  14. %for educational and research purposes only and without fee is hereby  
  15. %granted, provided that this copyright notice and the original authors'  
  16. %names ap pearon all copies and supporting documentation. This program  
  17. %shall not be used, rewritten, or adapted as the basis of a commercial  
  18. %software or hardware product without first obtaining permission of the  
  19. %authors. The authors make no representations about the suitability of  
  20. %this software for any purpose. It is provided "as is" without express  
  21. %or implied warranty.  
  22. %----------------------------------------------------------------------  
  23. %  
  24. %This is an implementation of the algorithm for calculating the  
  25. %Structural SIMilarity (SSIM) index between two images. Please refer  
  26. %to the following paper:  
  27. %  
  28. %Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image  
  29. %quality assessment: From error visibility to structural similarity"  
  30. %IEEE Transactios on Image Processing, vol. 13, no. 4, pp.600-612,  
  31. %Apr. 2004.  
  32. %  
  33. %Kindly report any suggestions or corrections to zhouwang@ieee.org  
  34. %  
  35. %----------------------------------------------------------------------  
  36. %  
  37. %Input : (1) img1: the first image being compared  
  38. %        (2) img2: the second image being compared  
  39. %        (3) K: constants in the SSIM index formula (see the above  
  40. %            reference). defualt value: K = [0.01 0.03]  
  41. %        (4) window: local window for statistics (see the above  
  42. %            reference). default widnow is Gaussian given by  
  43. %            window = fspecial('gaussian', 11, 1.5);  
  44. %        (5) L: dynamic range of the images. default: L = 255  
  45. %  
  46. %Output: (1) mssim: the mean SSIM index value between 2 images.  
  47. %            If one of the images being compared is regarded as   
  48. %            perfect quality, then mssim can be considered as the  
  49. %            quality measure of the other image.  
  50. %            If img1 = img2, then mssim = 1.  
  51. %        (2) ssim_map: the SSIM index map of the test image. The map  
  52. %            has a smaller size than the input images. The actual size:  
  53. %            size(img1) - size(window) + 1.  
  54. %  
  55. %Default Usage:  
  56. %   Given 2 test images img1 and img2, whose dynamic range is 0-255  
  57. %  
  58. %   [mssim ssim_map] = ssim_index(img1, img2);  
  59. %  
  60. %Advanced Usage:  
  61. %   User defined parameters. For example  
  62. %  
  63. %   K = [0.05 0.05];  
  64. %   window = ones(8);  
  65. %   L = 100;  
  66. %   [mssim ssim_map] = ssim_index(img1, img2, K, window, L);  
  67. %  
  68. %See the results:  
  69. %  
  70. %   mssim                        %Gives the mssim value  
  71. %   imshow(max(0, ssim_map).^4)  %Shows the SSIM index map  
  72. %  
  73. %========================================================================  
  74.   
  75.   
  76. if (nargin < 2 | nargin > 5)  
  77.    ssim_index = -Inf;  
  78.    ssim_map = -Inf;  
  79.    return;  
  80. end  
  81.   
  82. if (size(img1) ~= size(img2))  
  83.    ssim_index = -Inf;  
  84.    ssim_map = -Inf;  
  85.    return;  
  86. end  
  87.   
  88. [M N] = size(img1);  
  89.   
  90. if (nargin == 2)  
  91.    if ((M < 11) | (N < 11))   % 图像大小过小,则没有意义。  
  92.            ssim_index = -Inf;  
  93.            ssim_map = -Inf;  
  94.       return  
  95.    end  
  96.    window = fspecial('gaussian', 11, 1.5);        % 参数一个标准偏差1.5,11*11的高斯低通滤波。  
  97.    K(1) = 0.01;                                   % default settings  
  98.    K(2) = 0.03;                                      
  99.    L = 255;                                    
  100. end  
  101.   
  102. if (nargin == 3)  
  103.    if ((M < 11) | (N < 11))  
  104.            ssim_index = -Inf;  
  105.            ssim_map = -Inf;  
  106.       return  
  107.    end  
  108.    window = fspecial('gaussian', 11, 1.5);  
  109.    L = 255;  
  110.    if (length(K) == 2)  
  111.       if (K(1) < 0 | K(2) < 0)  
  112.                    ssim_index = -Inf;  
  113.                    ssim_map = -Inf;  
  114.                    return;  
  115.       end  
  116.    else  
  117.            ssim_index = -Inf;  
  118.            ssim_map = -Inf;  
  119.            return;  
  120.    end  
  121. end  
  122.   
  123. if (nargin == 4)  
  124.    [H W] = size(window);  
  125.    if ((H*W) < 4 | (H > M) | (W > N))  
  126.            ssim_index = -Inf;  
  127.            ssim_map = -Inf;  
  128.       return  
  129.    end  
  130.    L = 255;  
  131.    if (length(K) == 2)  
  132.       if (K(1) < 0 | K(2) < 0)  
  133.                    ssim_index = -Inf;  
  134.                    ssim_map = -Inf;  
  135.                    return;  
  136.       end  
  137.    else  
  138.            ssim_index = -Inf;  
  139.            ssim_map = -Inf;  
  140.            return;  
  141.    end  
  142. end  
  143.   
  144. if (nargin == 5)  
  145.    [H W] = size(window);  
  146.    if ((H*W) < 4 | (H > M) | (W > N))  
  147.            ssim_index = -Inf;  
  148.            ssim_map = -Inf;  
  149.       return  
  150.    end  
  151.    if (length(K) == 2)  
  152.       if (K(1) < 0 | K(2) < 0)  
  153.                    ssim_index = -Inf;  
  154.                    ssim_map = -Inf;  
  155.                    return;  
  156.       end  
  157.    else  
  158.            ssim_index = -Inf;  
  159.            ssim_map = -Inf;  
  160.            return;  
  161.    end  
  162. end  
  163.   
  164. if size(img1,3)~=1   %判断图像时不是彩色图,如果是,结果为3,否则为1  
  165.    org=rgb2ycbcr(img1);  
  166.    test=rgb2ycbcr(img2);  
  167.    y1=org(:,:,1);  
  168.    y2=test(:,:,1);  
  169.    y1=double(y1);  
  170.    y2=double(y2);  
  171.  else   
  172.      y1=double(img1);  
  173.      y2=double(img2);  
  174.  end  
  175. img1 = double(y1);   
  176. img2 = double(y2);  
  177. % automatic downsampling  
  178. %f = max(1,round(min(M,N)/256));  
  179. %downsampling by f  
  180. %use a simple low-pass filter  
  181. if(f>1)  
  182. %     lpf = ones(f,f);  
  183. %     lpf = lpf/sum(lpf(:));  
  184. %     img1 = imfilter(img1,lpf,'symmetric','same');  
  185. %     img2 = imfilter(img2,lpf,'symmetric','same');  
  186. %     img1 = img1(1:f:end,1:f:end);  
  187. %     img2 = img2(1:f:end,1:f:end);  
  188. % end  
  189.   
  190. C1 = (K(1)*L)^2;    % 计算C1参数,给亮度L(x,y)用。    C1=6.502500  
  191. C2 = (K(2)*L)^2;    % 计算C2参数,给对比度C(x,y)用。  C2=58.522500   
  192. window = window/sum(sum(window)); %滤波器归一化操作。  
  193.   
  194.   
  195. mu1   = filter2(window, img1, 'valid');  % 对图像进行滤波因子加权  valid改成same结果会低一丢丢  
  196. mu2   = filter2(window, img2, 'valid');  % 对图像进行滤波因子加权  
  197.   
  198. mu1_sq = mu1.*mu1;     % 计算出Ux平方值。  
  199. mu2_sq = mu2.*mu2;     % 计算出Uy平方值。  
  200. mu1_mu2 = mu1.*mu2;    % 计算Ux*Uy值。  
  201.   
  202. sigma1_sq = filter2(window, img1.*img1, 'valid') - mu1_sq;  % 计算sigmax (标准差)  
  203. sigma2_sq = filter2(window, img2.*img2, 'valid') - mu2_sq;  % 计算sigmay (标准差)  
  204. sigma12 = filter2(window, img1.*img2, 'valid') - mu1_mu2;   % 计算sigmaxy(标准差)  
  205.   
  206. if (C1 > 0 & C2 > 0)  
  207.    ssim_map = ((2*mu1_mu2 + C1).*(2*sigma12 + C2))./((mu1_sq + mu2_sq + C1).*(sigma1_sq + sigma2_sq + C2));  
  208. else  
  209.    numerator1 = 2*mu1_mu2 + C1;  
  210.    numerator2 = 2*sigma12 + C2;  
  211.    denominator1 = mu1_sq + mu2_sq + C1;  
  212.    denominator2 = sigma1_sq + sigma2_sq + C2;  
  213.    ssim_map = ones(size(mu1));  
  214.    index = (denominator1.*denominator2 > 0);  
  215.    ssim_map(index) = (numerator1(index).*numerator2(index))./(denominator1(index).*denominator2(index));  
  216.    index = (denominator1 ~= 0) & (denominator2 == 0);  
  217.    ssim_map(index) = numerator1(index)./denominator1(index);  
  218. end  
  219. mssim = mean2(ssim_map);  
  220.   
  221. return  


控制台输入以下代码:


[cpp]  view plain  copy
  1. >> img1= imread('C:\Users\Administrator\Desktop\noise_image.jpg');  
  2. >> img2= imread('C:\Users\Administrator\Desktop\actruel_image.jpg');  
  3. >> ssim(img1,img2)  


最后说一句,不管是结果如何,只要对比实验用的同一种评价代码工具,无所谓结果和原论文一不一样,问题是很多论文实验都搞不出来滴


参考文献

【1】Wang Z, Bovik A C, Sheikh H R, et al. Image quality assessment: from error visibility to structural similarity[J]. IEEE Transactions on Image Processing, 2004, 13(4):600-612.

                                                 

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MATLAB提供了计算图像质量评价指标PSNRSSIM的函数,可以通过以下步骤使用MATLAB GUI来计算: 1. 在MATLAB中创建一个GUI界面,在界面上创建两个按钮,一个用于选择参考图像,另一个用于选择待评价的图像。 2. 为每个按钮添加回调函数,使其分别调用MATLAB的`uigetfile`函数用于选择参考图像和待评价图像。 3. 在GUI界面上添加一个用于显示结果的文本框。 4. 编写一个计算PSNRSSIM的函数,该函数的输入为参考图像和待评价图像的路径,输出为PSNRSSIM的值。 5. 在每个按钮的回调函数中,调用计算函数并将结果显示在文本框中。 下面是一个简单的示例代码来实现上述步骤: ```matlab function psnr_ssim_gui % 创建GUI界面 fig = uifigure('Name', 'PSNR and SSIM Calculation'); % 创建显示结果的文本框 resultTextBox = uitextarea(fig, 'Position', [50, 100, 200, 100]); % 创建选择参考图像的按钮 refButton = uibutton(fig, 'Position', [50, 250, 200, 30], 'Text', 'Select Reference Image', 'ButtonPushedFcn', @(src, event) selectImage(src, event, 'reference')); % 创建选择待评价图像的按钮 evalButton = uibutton(fig, 'Position', [50, 300, 200, 30], 'Text', 'Select Evaluation Image', 'ButtonPushedFcn', @(src, event) selectImage(src, event, 'evaluation')); function selectImage(src, event, imageType) % 弹出文件选择对话框 [filename, pathname] = uigetfile({'*.jpg;*.png', 'Image Files (*.jpg, *.png)'}, 'Select an image'); if ~isequal(filename, 0) % 获取图像的完整路径 imagepath = fullfile(pathname, filename); % 根据图像类型,更新参考图像或待评价图像的路径 if isequal(imageType, 'reference') refImagePath = imagepath; else evalImagePath = imagepath; end end end function calculatePSNR_SSIM % 计算PSNRSSIM [psnrValue, ssimValue] = calculate(refImagePath, evalImagePath); % 将结果显示在文本框中 resultText = sprintf('PSNR: %.2f\nSSIM: %.2f', psnrValue, ssimValue); resultTextBox.Value = resultText; end function [psnrValue, ssimValue] = calculate(refImagePath, evalImagePath) % 读取图像 refImage = imread(refImagePath); evalImage = imread(evalImagePath); % 在此编写计算PSNRSSIM的代码 % 这里只是举例计算图像的差异,你需要根据实际需要进行计算 diff = abs(evalImage - refImage); mse = mean(diff(:).^2); maxval = 255; psnrValue = 10 * log10(maxval^2 / mse); % 这里的ssimValue也只是个示例,你需要根据实际需要进行计算 ssimValue = ssim(evalImage, refImage); end end ``` 以上代码创建了一个简单的GUI界面,包括选择参考图像和待评价图像的按钮,并显示计算结果的文本框。当用户点击按钮时,会调用相应的回调函数来选择图像,并调用计算函数来计算PSNRSSIM的值,最后将结果显示在文本框中。请注意,这只是一个示例代码,你需要根据实际需要进行适当的修改和完善。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值