最近用ssim測試圖片畫質損傷時,發現matlab自帶ssim與以前一直使用的ssim計算得分有差別,故和同事開始肯定差別所在。
app
這裏提到不一樣的ssim版本主要基於matlab。如前言所述,主要分爲2個實現。測試
雖然2個版本的代碼實現徹底不同,但總的說,差別能夠歸結爲如下幾點:3d
The precisely right scale depends on both the image resolution and the viewing distance and is usually difficult to be obtained. In practice, we suggest to use the following empirical formula to determine the scale for images viewed from a typical distance (say 3~5 times of the image height or width): 1) Let F = max(1, round(N/256)), where N is the number of pixels in image height (or width); 2) Average local F by F pixels and then downsample the image by a factor of F; and 3) apply the ssim_index.m program. For example, for an 512 by 512 image, F = max(1, round(512/256)) = 2, so the image should be averaged within a 2 by 2 window and downsampled by a factor of 2 before applying ssim_index.m.
code
上面這段意思:人看圖片時,與圖片有必定距離(至關於圖片縮小),一些細節可被忽略,若是進行downsample,除了減低運算複雜度,還能更貼合人的主觀觀看感覺。
orm
涉及downsample的代碼部分:blog
% automatic downsampling f = max(1,round(min(M,N)/256)); %downsampling by f %use a simple low-pass filter if(f>1) lpf = ones(f,f); lpf = lpf/sum(lpf(:)); img1 = imfilter(img1,lpf,'symmetric','same'); img2 = imfilter(img2,lpf,'symmetric','same'); img1 = img1(1:f:end,1:f:end); img2 = img2(1:f:end,1:f:end); end
Zhou Wang版本
代碼:圖片
window = fspecial('gaussian', 11, 1.5); % 算子類型:gaussian(高斯) % 模版尺寸:11*11 % 標準差:1.5 % 以上參數皆hardcode
matlab版本
代碼:ci
filtRadius = ceil(radius*3); filtSize = 2*filtRadius + 1; if (N < 3) gaussFilt = fspecial('gaussian',[filtSize filtSize],radius); % 2D mask else ...% 3D mask end % 算子類型:gaussian(高斯) % 模版尺寸:[filtSize filtSize],使用默認值計算後,爲:11*11矩陣; % 標準差:radius,默認值爲:1.5; % 以上參數皆可經過傳參改變