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實現的。