计算matlab中图像的PSNR和SSIM

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

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


PSNR的差异:

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


2.彩色图像:

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

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


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



 
 
  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. if nargin< 2
  21. D = Y1;
  22. else
  23. if any(size(Y1)~=size(Y2))
  24. error( 'The input size is not equal to each other!');
  25. end
  26. D = Y1 - Y2;
  27. end
  28. MSE = sum(D(:).*D(:)) / numel(Y1);
  29. PSNR = 10* log10( 255^ 2 / MSE);

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



 
 
  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



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


控制台输入以下代码:



 
 
  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.

                                                 

  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值