Make different methods of LBP more readable in Cython code

This commit is contained in:
Johannes Schönberger
2012-08-20 19:29:06 +02:00
parent 36b22d7819
commit 2b7cb5f630
2 changed files with 16 additions and 15 deletions
+5 -4
View File
@@ -80,7 +80,7 @@ cdef inline int _bit_rotate_right(int value, int length):
def _local_binary_pattern(np.ndarray[double, ndim=2] image,
int P, float R, int method=0):
int P, float R, char method='D'):
# texture weights
cdef np.ndarray[int, ndim=1] weights = 2 ** np.arange(P, dtype=np.int32)
# local position of texture elements
@@ -116,7 +116,7 @@ def _local_binary_pattern(np.ndarray[double, ndim=2] image,
lbp = 0
# if method == 'uniform' or method == 'var':
if method == 2 or method == 3:
if method == 'U' or method == 'V':
# determine number of 0 - 1 changes
changes = 0
for i in range(P - 1):
@@ -128,7 +128,7 @@ def _local_binary_pattern(np.ndarray[double, ndim=2] image,
else:
lbp = P + 1
if method == 3:
if method == 'V':
var = np.var(texture)
if var != 0:
lbp /= var
@@ -139,7 +139,8 @@ def _local_binary_pattern(np.ndarray[double, ndim=2] image,
for i in range(P):
lbp += signed_texture[i] * weights[i]
if method == 1:
# method == 'ror'
if method == 'R':
# shift LBP P times to the right and get minimum value
rotation_chain[0] = <int>lbp
for i in range(1, P):
+11 -11
View File
@@ -235,14 +235,14 @@ def local_binary_pattern(image, P, R, method='default'):
Parameters
----------
image : (N, M) array
graylevel image
Graylevel image.
P : int
number of circularly symmetric neighbour set points (quantization of the
angular space)
Number of circularly symmetric neighbour set points (quantization of the
angular space).
R : float
radius of circle (spatial resolution of the operator)
method : {'default', 'ror', 'uniform', 'var'}
method to determine the pattern::
Radius of circle (spatial resolution of the operator).
method : {'D', 'R', 'U', 'V'}
Method to determine the pattern::
* 'default': original local binary pattern which is gray scale but not
rotation invariant.
* 'ror': extension of default implementation which is gray scale and
@@ -256,7 +256,7 @@ def local_binary_pattern(image, P, R, method='default'):
Returns
-------
output : (N, M) array
LBP image
LBP image.
References
----------
@@ -268,10 +268,10 @@ def local_binary_pattern(image, P, R, method='default'):
"""
methods = {
'default': 0,
'ror': 1,
'uniform': 2,
'var': 3
'default': ord('D'),
'ror': ord('R'),
'uniform': ord('U'),
'var': ord('V')
}
image = np.array(image, dtype='double', copy=True)
output = _local_binary_pattern(image, P, R, methods[method.lower()])