SSIM(結構類似度算法)不一樣實現版本的差別

前言

最近用ssim測試圖片畫質損傷時,發現matlab自帶ssim與以前一直使用的ssim計算得分有差別,故和同事開始肯定差別所在。

app

不一樣的SSIM版本

這裏提到不一樣的ssim版本主要基於matlab。如前言所述,主要分爲2個實現。測試

2個版本的差別

雖然2個版本的代碼實現徹底不同,但總的說,差別能夠歸結爲如下幾點:3d

1. downsample

  • Zhou Wang實現版本有downsample,他也推薦這麼作,緣由以下:

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版ssim對比時,原圖與失真圖都作了downsample(縮小),其產生的影響是:
    • downsample後的類似度 比 不作downsample的類似度 要高
    • 損失程度不一樣2張失真圖,在downsample後進行ssim,2者類似度差距減小
    downsample是致使 Zhou Wang版本 與 matlab官方版本 計算結果相差較大的主要緣由。 而大學官網上其實也提供了Zhou Wang實現的非downsample版本,只不過名字是: ssim_index.m。

2. 濾波部分

  • Zhou Wang版本去掉downsample後,和matlab官方的結果還有約±0.002差距,其緣由主要是濾波部分存在差別。
    • 濾波參數:
      • 輸入矩陣(圖像)
        • 這個是咱們傳入的,不存差別;
      • 濾波掩模
        • 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;
          % 以上參數皆可經過傳參改變
      • 濾波模式
        • Zhou Wang版本:
          使用互相關(correlation)
        • matlab版本:
          使用卷積(convolution)
      • 邊界填充方式
        • Zhou Wang版本:
          邊界經過填充0來擴展,例如:
        • matlab版本:
          經過複製外邊界的值來擴展,例如:
      • 結果矩陣
        • Zhou Wang版本
          只返回濾波時未使用邊緣補 0 部分進行計算的結果部分(返回矩陣小於輸入矩陣),例如:
        • matlab版本
          與輸入矩陣大小一致,例如:
    因爲以上差別,致使了 Zhou Wang版ssim 與 matlab官方ssim 在計算結果上存在輕微差距

3. 2D與3D圖片支持

  • matlab官方的ssim,除了支持2D圖片,外還支持3D圖片;而Zhou Wang版本只支持2D圖片。
  • 2D圖片
    ssim只支持對2D灰度圖進行結構類似度計算,rgb圖(彩圖)需轉換爲灰度圖才能計算(可調用rgb2gray進行轉換)。
  • 3D圖片
    3D圖片如今在醫療領域用得較多:
    在matlab2015中,若是直接讀入rgb圖(不轉換爲灰度)會被認爲是3D圖,這個比較坑爹,ssim運行不報錯,但結果倒是徹底錯的。
相關文章
相關標籤/搜索