这个玩具示例向您展示了如何从 LBP 直方图计算中过滤掉不需要的图像区域:
import numpy as np
from skimage.feature.texture import local_binary_pattern
P, R = 8, 1
dim = 2**P
img = np.asarray([[5, 5, 5, 5], [5, 99, 100, 5], [5, 5, 5, 5]]], dtype=np.uint8)
mask = img > 5
codes = local_binary_pattern(img, P, R)
hist, _ = np.histogram(codes[mask], bins=np.arange(dim+1), range=(0, dim))
演示
In [97]: img
Out[97]:
array([[ 5, 5, 5, 5],
[ 5, 99, 100, 5],
[ 5, 5, 5, 5]], dtype=uint8)
In [98]: codes
Out[98]:
array([[ 193., 241., 241., 112.],
[ 199., 1., 0., 124.],
[ 7., 31., 31., 28.]])
In [99]: mask
Out[99]:
array([[False, False, False, False],
[False, True, True, False],
[False, False, False, False]], dtype=bool)
In [100]: hist
Out[100]:
array([1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0], dtype=int32)