Advertisement
Guest User

psnrhma.m

a guest
Mar 27th, 2019
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 9.62 KB | None | 0 0
  1. function [p_hvs_m, p_hvs] = psnrhma(img1, img2, wstep)
  2.  
  3. %========================================================================
  4. %
  5. % Calculation of PSNR-HMA and PSNR-HA image quality measures
  6. %
  7. % PSNR-HMA is Peak Signal to Noise Ratio taking into account
  8. % Contrast Sensitivity Function (CSF), between-coefficient  
  9. % contrast masking of DCT basis functions as well as mean shift and
  10. % contrast changing
  11. % PSNR-HA is the same metric without taking into account constrast masking
  12. %
  13. % Copyright(c) 2011 Nikolay Ponomarenko
  14. % All Rights Reserved
  15. %
  16. % Homepage: www.ponomarenko.info , E-mail: nikolay@ponomarenko.info
  17. %
  18. %----------------------------------------------------------------------
  19. %
  20. % Permission to use, copy, or modify this software and its documentation
  21. % for educational and research purposes only and without fee is hereby
  22. % granted, provided that this copyright notice and the original authors
  23. % names appear on all copies and supporting documentation. This program
  24. % shall not be used, rewritten, or adapted as the basis of a commercial
  25. % software or hardware product without first obtaining permission of the
  26. % authors. The authors make no representations about the suitability of
  27. % this software for any purpose. It is provided "as is" without express
  28. % or implied warranty.
  29. %
  30. %----------------------------------------------------------------------
  31. %
  32. % This is an implementation of the algorithm for calculating the PSNR-HA
  33. % or PSNR-HMA between two images. Please refer to the following paper:
  34. %
  35. % N. Ponomarenko, O. Ieremeiev, V. Lukin, K. Egiazarian, M. Carli,
  36. % Modified Image Visual Quality Metrics for Contrast Change and Mean Shift
  37. % Accounting, Proceedings of CADSM, February 2011, Ukraine, pp. 305 - 311.
  38. %
  39. % Kindly report any suggestions or corrections to nikolay@ponomarenko.info
  40. %
  41. %----------------------------------------------------------------------
  42. %
  43. % Input : (1) img1: the first image being compared
  44. %         (2) img2: the second image being compared
  45. %         (3) wstep: step of 8x8 window to calculate DCT
  46. %             coefficients. Default value is 8.
  47. %
  48. % Output: (1) p_hvs_m: the PSNR-HMA value between 2 images.
  49. %             If one of the images being compared is regarded as
  50. %             perfect quality, then PSNR-HMA can be considered as the
  51. %             quality measure of the other image.
  52. %             If compared images are visually undistingwished,
  53. %             then PSNR-HMA = 100000.
  54. %         (2) p_hvs: the PSNR-HA value between 2 images.
  55. %
  56. % Default Usage:
  57. %   Given 2 test images img1 and img2, whose dynamic range is 0-255
  58. %
  59. %   [p_hvs_m, p_hvs] = psnrhvsm(img1, img2);
  60. %
  61. % See the results:
  62. %
  63. %   p_hvs_m  % Gives the PSNR-HMA value
  64. %   p_hvs    % Gives the PSNR-HA value
  65. %
  66. %========================================================================
  67.  
  68.   if nargin < 2
  69.     p_hvs_m = -Inf;
  70.     p_hvs = -Inf;
  71.     return;
  72.   end
  73.  
  74.   if size(img1) ~= size(img2)
  75.     p_hvs_m = -Inf;
  76.     p_hvs = -Inf;
  77.     return;
  78.   end
  79.  
  80.   if nargin > 2
  81.     step = wstep;
  82.   else
  83.     step = 8; % Default value is 8;
  84.   end
  85.  
  86.   CSFCof_y  = [1.6193873005, 2.2901594831, 2.08509755623, 1.48366094411, 1.00227514334, 0.678296995242, 0.466224900598, 0.3265091542;      
  87.            2.2901594831, 1.94321815382, 2.04793073064, 1.68731108984, 1.2305666963, 0.868920337363, 0.61280991668, 0.436405793551;
  88.            2.08509755623, 2.04793073064, 1.34329019223, 1.09205635862, 0.875748795257, 0.670882927016, 0.501731932449, 0.372504254596;
  89.            1.48366094411, 1.68731108984, 1.09205635862, 0.772819797575, 0.605636379554, 0.48309405692, 0.380429446972, 0.295774038565;
  90.            1.00227514334, 1.2305666963, 0.875748795257, 0.605636379554, 0.448996256676, 0.352889268808, 0.283006984131, 0.226951348204;
  91.            0.678296995242, 0.868920337363, 0.670882927016, 0.48309405692, 0.352889268808, 0.27032073436, 0.215017739696, 0.17408067321;
  92.            0.466224900598, 0.61280991668, 0.501731932449, 0.380429446972, 0.283006984131, 0.215017739696, 0.168869545842, 0.136153931001;
  93.            0.3265091542, 0.436405793551, 0.372504254596, 0.295774038565, 0.226951348204, 0.17408067321, 0.136153931001, 0.109083846276];
  94.  
  95.   CSFCof_cb  = [1.91113096927, 2.46074210438, 1.18284184739, 1.14982565193, 1.05017074788, 0.898018824055, 0.74725392039, 0.615105596242;
  96.            2.46074210438, 1.58529308355, 1.21363250036, 1.38190029285, 1.33100189972, 1.17428548929, 0.996404342439, 0.830890433625;
  97.            1.18284184739, 1.21363250036, 0.978712413627, 1.02624506078, 1.03145147362, 0.960060382087, 0.849823426169, 0.731221236837;
  98.            1.14982565193, 1.38190029285, 1.02624506078, 0.861317501629, 0.801821139099, 0.751437590932, 0.685398513368, 0.608694761374;
  99.            1.05017074788, 1.33100189972, 1.03145147362, 0.801821139099, 0.676555426187, 0.605503172737, 0.55002013668, 0.495804539034;
  100.            0.898018824055, 1.17428548929, 0.960060382087, 0.751437590932, 0.605503172737, 0.514674450957, 0.454353482512, 0.407050308965;
  101.            0.74725392039, 0.996404342439, 0.849823426169, 0.685398513368, 0.55002013668, 0.454353482512, 0.389234902883, 0.342353999733;
  102.            0.615105596242, 0.830890433625, 0.731221236837, 0.608694761374, 0.495804539034, 0.407050308965, 0.342353999733, 0.295530605237];
  103.  
  104.   CSFCof_cr  = [2.03871978502, 2.62502345193, 1.26180942886, 1.11019789803, 1.01397751469, 0.867069376285, 0.721500455585, 0.593906509971;
  105.            2.62502345193, 1.69112867013, 1.17180569821, 1.3342742857, 1.28513006198, 1.13381474809, 0.962064122248, 0.802254508198;
  106.            1.26180942886, 1.17180569821, 0.944981930573, 0.990876405848, 0.995903384143, 0.926972725286, 0.820534991409, 0.706020324706;
  107.            1.11019789803, 1.3342742857, 0.990876405848, 0.831632933426, 0.77418706195, 0.725539939514, 0.661776842059, 0.587716619023;
  108.            1.01397751469, 1.28513006198, 0.995903384143, 0.77418706195, 0.653238524286, 0.584635025748, 0.531064164893, 0.478717061273;
  109.            0.867069376285, 1.13381474809, 0.926972725286, 0.725539939514, 0.584635025748, 0.496936637883, 0.438694579826, 0.393021669543;
  110.            0.721500455585, 0.962064122248, 0.820534991409, 0.661776842059, 0.531064164893, 0.438694579826, 0.375820256136, 0.330555063063;
  111.            0.593906509971, 0.802254508198, 0.706020324706, 0.587716619023, 0.478717061273, 0.393021669543, 0.330555063063, 0.285345396658];
  112.  
  113.   [y,x,z]=size(img1);
  114.   if z>1
  115.     a=rgb2ycbcr(img1);
  116.     a1=double(a(:,:,1));
  117.     a2=double(a(:,:,2));
  118.     a3=double(a(:,:,3));
  119.     b=rgb2ycbcr(img2);
  120.     b1=double(b(:,:,1));
  121.     b2=double(b(:,:,2));
  122.     b3=double(b(:,:,3));
  123.  
  124.     [p11,p12]=msehma(a1, b1, step, CSFCof_y, 0, 0.37);
  125.     [p21,p22]=msehma(a2, b2, step, CSFCof_cb, 0.25, 2);
  126.     [p31,p32]=msehma(a3, b3, step, CSFCof_cr, 0.25, 2);
  127.  
  128.     S1=(p11+(p21+p31)*0.5)/2;
  129.     S2=(p12+(p22+p32)*0.5)/2;    
  130.   else
  131.     [S1, S2]=msehma(img1, img2, step, CSFCof_y, 0, 0.37);
  132.   end
  133.  
  134.   if S1 == 0
  135.     p_hvs_m = 100000; % img1 and img2 are visually undistingwished
  136.   else
  137.     p_hvs_m = 10*log10(255*255/S1);
  138.   end
  139.   if S2 == 0  
  140.     p_hvs = 100000; % img1 and img2 are identical
  141.   else
  142.     p_hvs = 10*log10(255*255/S2);
  143.   end
  144.  
  145. function [S1, S2] = msehma(img1, img2, step, CSFCof, KofContr1, KofContr2)
  146.  
  147.   LenXY=size(img1);LenY=LenXY(1);LenX=LenXY(2);
  148.  
  149.   MaskCof = (CSFCof.*0.3885746225901003).^2;
  150.   % see an explanation in [1]
  151.  
  152.   delt=(sum(double(img1(:)))-sum(double(img2(:))))/length(img1(:));
  153.   img2m=double(img2)+delt;
  154.  
  155.   mean1=mean(img1(:));
  156.   mean2=mean(img2m(:));
  157.   tmp=(img1-mean1).*(img2m-mean2);
  158.   sq=var(img2m(:),1);
  159.   l=sum(tmp(:))/length(tmp(:))/sq;
  160.   if l<1
  161.     KofContr=KofContr1;
  162.   else
  163.     KofContr=KofContr2;
  164.   end;
  165.   img3m=mean2+(img2m-mean2)*l;
  166.   S3=(sum(sum((img2m-img1).^2))-sum(sum((img3m-img1).^2)))/length(img1(:));
  167.  
  168.   S1 = 0; S2 = 0; Num = 0;
  169.   SS1=0;SS2=0;
  170.   X=1;Y=1;
  171.   while Y <= LenY-7
  172.     while X <= LenX-7
  173.       A = img1(Y:Y+7,X:X+7);
  174.       B = img2m(Y:Y+7,X:X+7);
  175.       B2 = img3m(Y:Y+7,X:X+7);
  176.       A_dct = dct2(A); B_dct = dct2(B); B_dct2 = dct2(B2);
  177.       MaskA = maskeff(A,A_dct,MaskCof);
  178.       MaskB = maskeff(B,B_dct,MaskCof);
  179.       if MaskB > MaskA
  180.         MaskA = MaskB;
  181.       end
  182.       X = X + step;
  183.       for k = 1:8
  184.         for l = 1:8
  185.           u = abs(A_dct(k,l)-B_dct(k,l));
  186.           u2= abs(A_dct(k,l)-B_dct2(k,l));
  187.           S2 = S2 + (u*CSFCof(k,l)).^2;    % PSNR-HVS
  188.           SS2 = SS2 + (u2*CSFCof(k,l)).^2;    % PSNR-HVS
  189.  
  190.           if (k~=1) || (l~=1)               % See equation 3 in [1]
  191.             if u < MaskA/MaskCof(k,l)
  192.               u = 0;
  193.             else
  194.               u = u - MaskA/MaskCof(k,l);
  195.             end;
  196.             if u2 < MaskA/MaskCof(k,l)
  197.               u2 = 0;
  198.             else
  199.               u2 = u2 - MaskA/MaskCof(k,l);
  200.             end;
  201.           end
  202.           S1 = S1 + (u*CSFCof(k,l)).^2;    % PSNR-HVS-M
  203.           SS1 = SS1 + (u2*CSFCof(k,l)).^2;    % PSNR-HVS-M
  204.           Num = Num + 1;
  205.         end
  206.       end
  207.     end
  208.     X = 1; Y = Y + step;
  209.   end
  210.  
  211.   if Num ~=0
  212.     S1 = S1/Num;S2 = S2/Num;
  213.     SS1 = SS1/Num;SS2=SS2/Num;
  214.     delt=delt^2;
  215.  
  216.     if (S1>SS1) S1=SS1+(S1-SS1)*KofContr;
  217.     end
  218.     S1=S1 + 0.045*delt;
  219.     if (S2>SS2) S2=SS2+(S2-SS2)*KofContr;
  220.     end
  221.     S2=S2 + 0.045*delt;
  222.   end
  223.  
  224. function m=maskeff(z,zdct,MaskCof)  
  225. % Calculation of Enorm value (see [1])
  226.   m = 0;
  227.   for k = 1:8
  228.     for l = 1:8
  229.       if (k~=1) || (l~=1)
  230.         m = m + (zdct(k,l).^2) * MaskCof(k,l);
  231.       end
  232.     end
  233.   end
  234.   pop=vari(z);
  235.   if pop ~= 0
  236.     pop=(vari(z(1:4,1:4))+vari(z(1:4,5:8))+vari(z(5:8,5:8))+vari(z(5:8,1:4)))/pop;
  237.   end
  238.   m = sqrt(m*pop)/32;   % sqrt(m*pop/16/64)
  239.  
  240. function d=vari(AA)
  241.   d=var(AA(:))*length(AA(:));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement