【短道速滑四】Halcon的texture_laws算子自我研究

  Halcon裏有個texture_laws 算子,最近實現了下,記錄下相關細節。算法

       Halcon的文檔裏對該算子是這樣描述的:app

       texture_laws — Filter an image using a Laws texture filter.ide

  Signature函數

    texture_laws(Image : ImageTexture : FilterTypes, Shift, FilterSize : )測試

  Description優化

    texture_laws applies a texture transformation (according to Laws) to an image. This is done by convolving the input image with a special filter mask. The filters are:ui

    9 different 3×3 matrices obtainable from the following three vectors:orm

      l = [ 1 2 1 ],
      e = [ -1 0 1 ],
      s = [ -1 2 -1 ]
    25 different 5×5 matrices obtainable from the following five vectors:
      l = [ 1 4 6 4 1 ],
      e = [ -1 -2 0 2 1 ],
      s = [ -1 0 2 0 -1 ],
      w = [ -1 2 0 -2 1 ]
      r = [ 1 -4 6 -4 1 ],
    49 different 7×7 matrices obtainable from the following seven vectors:
      l = [ 1 6 15 20 15 6 1 ],
      e = [ -1 -4 -5 0 5 4 1 ],
      s = [ -1 -2 1 4 1 -2 -1 ],
      w = [ -1 0 3 0 -3 0 1 ],
      r = [ 1 -2 -1 4 -1 -2 1 ],
      u = [ 1 -4 5 0 -5 4 -1 ]
      o = [ -1 6 -15 20 -15 6 -1 ]
  The names of the filters are mnemonics for 「level,」 「edge,」 「spot,」 「wave,」 「ripple,」 「undulation,」 and 「oscillation.」
  For most of the filters the resulting gray values must be modified by a Shift. This makes the different textures in the output image more comparable to each other, provided suitable filters are used.The name of the filter is composed of the letters of the two   vectors used, where the first letter denotes convolution in the column direction while the second letter denotes convolution in the row direction.對象


  FilterTypes (input_control) string → (string)
    Desired filter.
    Default value: 'el'
    Suggested values: 'll', 'le', 'ls', 'lw', 'lr', 'lu', 'lo', 'el', 'ee', 'es', 'ew', 'er', 'eu', 'eo', 'sl', 'se', 'ss', 'sw', 'sr', 'su', 'so', 'wl', 'we', 'ws', 'ww', 'wr', 'wu', 'wo', 'rl', 're', 'rs', 'rw', 'rr', 'ru', 'ro', 'ul', 'ue', 'us', 'uw', 'ur', 'uu', 'uo', 'ol', 'oe', 'os', 'ow', 'or', 'ou', 'oo'
  Shift (input_control) integer → (integer)
    Shift to reduce the gray value dynamics.
    Default value: 2
    List of values: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9
  FilterSize (input_control) integer → (integer)
    Size of the filter kernel.
    Default value: 5
    List of values: 3, 5, 7blog

  這個算子一般用來進行紋理分析,其實現過程其實很簡單。第一,濾波器的大小隻有三、五、7三種,第2、濾波器的類型根據濾波器大小決定由多少種。

  以濾波器尺寸爲3,濾波器類型爲‘el‘,Shift = 2爲例來講明計算過程。

    對應的e = [ -1 0 1 ], l = [ 1 2 1 ]。

       其實就是對原圖進行e‘*l的一個卷積。

         -1   -2   -1
         0    0    0
         1    2   1

  卷積矩陣如上所示,那麼假如卷積的結果爲s,則最終的結果爲s >> shift, shift起到了調整圖像最後亮度的做用。

  若是濾波器的尺寸爲5或者7,那麼對應的卷積矩陣就是5*5或者7*7的,這各時候直接卷積速度比比較慢,其實,在本算法中,是沒有必要這樣的,很明顯,這是個行列可分離的卷積。

  也就是說,能夠先進行方向的卷積,獲得中間結果,而後在對中間結果進行列方向的卷積。這樣濾波器尺寸爲5和7的分別指須要作5+5和7+7次計算,效率打了不少。

        具體實現上,從速度角度考慮,這個中間結果能夠用signed short類型來保存,在觀察這些卷積的係數,都在signed char範圍內,所以,在從原圖到中間結果的過程當中,能夠用一個很是高效的SSE函數來實現,即_mm_maddubs_epi16.

   這個函數的功能以下:

       他能夠一次性實現16次乘法和加法,地方分別是字節數和有符號的字節數,很是有效。

       在中間結果到最終值時,又能夠利用_mm_madd_epi16這個針對16位數的SSE函數,他一樣能一次性實現多個乘法和加法。

      

 

       

   就是這樣一個簡單的優化,我測試了一下速度,測試對象爲3000*2000的RGB數據, 分別使用三、五、7的濾波器尺寸,時間比例以下:

        

  Halcon不一樣尺寸的耗時基本相同,我這裏明顯尺寸越小,耗時越短,而且速度比halcon要稍微快那麼一點點。

   測試算法在個人SSE Demo的ImageInfo 菜單下Laws Texture下。

        本文Demo下載地址:  http://files.cnblogs.com/files/Imageshop/SSE_Optimization_Demo.rar,裏面的全部算法都是基於SSE實現的。

相關文章
相關標籤/搜索