图像处理结果的度量 —— 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 plaincopy
  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);

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

[cpp] view plaincopy
  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 plaincopy
  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.5,11*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

控制台输入以下代码:

[cpp] view plaincopy
  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.

matlab中中图像PSNR和SSIM的计算相关推荐

  1. matlab中psnr多了50,matlab中中图像PSNR和SSIM的计算

    网上找了很多关于PSNR和SSIM的计算,很多结果算出来都不一样,公式都是普遍的,如下: 现在总结下造成结果差异的原因. PSNR的差异:1.灰度图像:灰度图像比较好计算只有一个值. 2.彩色图像:a ...

  2. 计算两个文件夹中图片的PSNR和SSIM

    注释:两个文件夹中图片的文件名要一致,格式.后缀都要一直,如果其他格式可以将.jpg改成.bmp 写来只是自己看,自己能用,所以跑不通您也别怪我菜 ```bash```bash```python im ...

  3. MATLAB【八】———— matlab 读取单个(多个)文件夹中所有图像

    0.matlab 移动(复制)文件到另一个文件夹 sourcePath = '.\Square_train'; targetPath = '.\Square_test'; fileList = dir ...

  4. matlab中给图像加几个矩形框_在图像中画矩形框(matlab)

    参考代码:https://github.com/cuijiaxun/FaceRecognitionByMatlab 中的LabelDetectWindow.m 在目标检测的时候,一般都需要用矩形框圈出 ...

  5. matlab中的图像类型

    MATLAB中的图像类型 在MATLAB中数组是最基本的数据结构,大部分图像用二维 数组即矩阵表示,矩阵中的一个元素对应一个像素.例如,一个由500行600列不同颜色点组成的图像可以用500*600的 ...

  6. MATLAB使用radon函数和iradon函数计算图像的投影并从投影中重构图像

    %使用radon函数和iradon函数计算图像的投影并从投影中重构图像 P = phantom(256); %应用在X线断层摄影术里广泛使用一个测试图像::Shepp-Logan Head影像 ims ...

  7. MATLAB#183;提取图像中多个目标

    基于matlab工具箱提取图像中的多目标特征(代码如下): 代码前面部分为提取图像的边界信息,调用了后面的遍历函数Pixel_Search,函数实现方法见后~ %%ROI Testing close ...

  8. 【计算机视觉基础】MATLAB程序实现图像中两个像素点的8-邻域、欧几里得距离与出租车距离、互换两像素点的罗森菲尔德8-邻域像素值

    调用Matlab函数指令实现以下内容: 提取图像上某两点(x1,y1).(x2,y2)的8-邻域罗森菲尔德邻域并显示: 显示(x1,y1)与(x2,y2)两点间的图像像素变化情况: 比较并显示RGB图 ...

  9. 【MATLAB图像处理】计算机视觉基础MATLAB实现读取磁盘中的图像,实现图像转化显示、像素点8-邻域标记、显示两个像素点的像素变化情况

    通过调用Matlab函数完成以下内容 创建n×m大小的灰度图像与彩色图像: 结合subplot.imshow.imtool等函数显示灰度.彩色.伪彩色图像.HSV图像等图像不同显示类型: 提取图像上某 ...

最新文章

  1. Android JNI入门第四篇——jni头文件分析
  2. php引入类的位置,php如何在一个类中引入另外一个类
  3. Vue.js 表单输入绑定
  4. Android基础_数据存储
  5. 让人郁闷的“DesktopCompatible”
  6. 虚拟网络的组建和应用课后习题答案
  7. 第六篇:python基础之文件处理
  8. string类的各种函数用法
  9. Linux下XPath对xml解析
  10. 使用udp协议实现服务器端程序时,用VisualC#实现UDP协议(二)
  11. ICBU可控文本生成技术详解
  12. sea 配置资料收集
  13. 走进我的交易室07_资金管理公式
  14. Borland Enterprise Core Object II (ECO II)第一次接觸
  15. 11.重载示例(下)
  16. C++轻量级微服务_『高级篇』docker容器来说什么是微服务(三)
  17. 二叉树 知道度 求节点数
  18. 描述性统计分析案例题_SPSS问卷数据统计分析之项目分析
  19. 微信小程序熊猫抽奖盒子panda_luckybox3.3.1多开版
  20. 数字电视复用器中的PCR矫正技术

热门文章

  1. php数据库缓存类,常见php数据文件缓存类汇总
  2. 【c语言】测量字符串长度
  3. 计算机科学概论各章总结,计算机科学概论(原书第5版)读书笔记
  4. 计算机系统的基本功能,计算机系统的主要功能是什么
  5. python 修改字符串中的某个单词_python Pandas替换字符串中的单词
  6. python生产和消费模型_python queue和生产者和消费者模型
  7. Android 金融类项目模块化架构
  8. SpringBoot集成FreeMarker
  9. Codeforces 895C - Square Subsets
  10. VC++设置Release模式下允许调试代码