1. 程式人生 > >計算matlab中影象的PSNR和SSIM

計算matlab中影象的PSNR和SSIM

網上找了很多關於PSNR和SSIM的計算,很多結果算出來都不一樣,公式都是普遍的,如下:

現在總結下造成結果差異的原因。

PSNR的差異:

1.灰度影象:灰度影象比較好計算,只有一個灰度值。

2.彩色影象:

(a)可以將分別計算R,G,B三個通道總和,最後MSE直接在原公式上多除以3就行(opencv官方代

(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 [email protected]
  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.