zoukankan      html  css  js  c++  java
  • 【短道速滑四】Halcon的texture_laws算子自我研究

      Halcon里有个texture_laws 算子,最近实现了下,记录下相关细节。

           Halcon的文档里对该算子是这样描述的:

           texture_laws — Filter an image using a Laws texture filter.

      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:

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

          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, 7

      这个算子通常用来进行纹理分析,其实现过程其实很简单。第一,滤波器的大小只有3、5、7三种,第二、滤波器的类型根据滤波器大小决定由多少种。

      以滤波器尺寸为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数据, 分别使用3、5、7的滤波器尺寸,时间比例如下:

            

      Halcon不同尺寸的耗时基本相同,我这里明显尺寸越小,耗时越短,并且速度比halcon要稍微快那么一点点。

       测试算法在我的SSE Demo的ImageInfo 菜单下Laws Texture下。

            本文Demo下载地址:  http://files.cnblogs.com/files/Imageshop/SSE_Optimization_Demo.rar,里面的所有算法都是基于SSE实现的。

  • 相关阅读:
    【APIO2008】免费道路[最小生成树 kruskal]
    【2019.8.13】
    【矩阵】
    [POI2008]BLO-Blockade [tarjan 割点]
    poj1458 最长公共子序列 (动态规划)
    最长上升子序列
    poj1163 数字三角形 (动态规划)
    快速幂 (分治)
    求排列的逆序数(分治)
    快速排序 (分治)
  • 原文地址:https://www.cnblogs.com/Imageshop/p/13490534.html
Copyright © 2011-2022 走看看