YUV影像質量的客觀評估SSIM matlab程式碼實現

ccwwff發表於2018-12-06

YUV影像質量的客觀評估SSIM matlab程式碼實現

yuv420p-ssim.m

function [ mssim_Y, mssim_U, mssim_V ] = yuv420p_ssim( file0, N0, w0, h0, file1, N1, w1, h1 )

  fp0 = fopen(file0, 'rb');
  fp1 = fopen(file1, 'rb');
  
  fseek(fp0, N0 * w0 * h0 * 3 / 2, 0);
  fseek(fp1, N1 * w1 * h1 * 3 / 2, 0);
  ima_Y = fread(fp0, [w0, h0], 'uchar');
  ima_U = fread(fp0, [w0 / 2, h0 / 2], 'uchar');
  ima_V = fread(fp0, [w0 / 2, h0 / 2], 'uchar');
  imb_Y = fread(fp1, [w1, h1], 'uchar');
  imb_U = fread(fp1, [w1 / 2, h1 / 2], 'uchar');
  imb_V = fread(fp1, [w1 / 2, h1 / 2], 'uchar');
  
  [mssim_Y, ssim_map_Y,siga_sq_Y,sigb_sq_Y] = ssim(ima_Y, imb_Y);
  [mssim_U, ssim_map_U,siga_sq_U,sigb_sq_U] = ssim(ima_U, imb_U);
  [mssim_V, ssim_map_V,siga_sq_V,sigb_sq_V] = ssim(ima_V, imb_V);

  fclose(fp0);
  fclose(fp1);
  
end

ssim.m

function [mssim, ssim_map,siga_sq,sigb_sq] = SSIM(ima, imb)  
% ========================================================================  
%ssim的演算法主要參考如下論文:  
%Z. Wang, A. C. Bovik, H. R. Sheikh, and E. P. Simoncelli, "Image  
% quality assessment: From error visibility to structural similarity,"  
% IEEE Transactios on Image Processing, vol. 13, no. 4, pp. 600-612,  
% Apr. 2004.  
%  首先對影像加窗處理,w=fspecial('gaussian', 11, 1.5);  
%                 (2*ua*ub+C1)*(2*sigmaa*sigmab+C2)  
%   SSIM(A,B)=————————————————————————  
%              (ua*ua+ub*ub+C1)(sigmaa*sigmaa+sigmab*sigmab+C2)  
%     C1=(K1*L);  
%     C2=(K2*L);   K1=0.01,K2=0.03  
%     L為灰度級數,L=255  
%-------------------------------------------------------------------  
%     ima - 比較影像A  
%     imb - 比較影像B  
%  
% ssim_map - 各加窗後得到的SSIM(A,B|w)組成的對映矩陣  
%    mssim - 對加窗得到的SSIM(A,B|w)求平均,即最終的SSIM(A,B)  
%  siga_sq - 影像A各視窗內灰度值的方差  
%  sigb_sq - 影像B各視窗內灰度值的方差  
%-------------------------------------------------------------------  
%  Cool_ben  
%========================================================================  
  
w = fspecial('gaussian', 11, 1.5);  %window 加窗  
K(1) = 0.01;                      
K(2) = 0.03;                      
L = 255;       
ima = double(ima);  
imb = double(imb);  
  
C1 = (K(1)*L)^2;  
C2 = (K(2)*L)^2;  
w = w/sum(sum(w));  
  
ua   = filter2(w, ima, 'valid');%對視窗內並沒有進行平均處理,而是與高斯卷積,  
ub   = filter2(w, imb, 'valid'); % 類似加權平均  
ua_sq = ua.*ua;  
ub_sq = ub.*ub;  
ua_ub = ua.*ub;  
siga_sq = filter2(w, ima.*ima, 'valid') - ua_sq;  
sigb_sq = filter2(w, imb.*imb, 'valid') - ub_sq;  
sigab = filter2(w, ima.*imb, 'valid') - ua_ub;  
  
ssim_map = ((2*ua_ub + C1).*(2*sigab + C2))./((ua_sq + ub_sq + C1).*(siga_sq + sigb_sq + C2));   
mssim = mean2(ssim_map);  
  
return 

reference:
https://blog.csdn.net/ecnu18918079120/article/details/60149864

相關文章