Local binary pattern (LBP),在机器视觉领域,是非常重要的一种特征。LBP可以有效地处理光照变化,在纹理分析,纹理识别方面被广泛应用。
LBP 的算法非常简单,简单来说,就是对图像中的某一像素点的灰度值与其邻域的像素点的灰度值做比较,如下图所示:
如果邻域像素值比该点大,则赋为1,反之,则赋为0,这样从左上角开始,可以形成一个bit chain,然后将该 bit chain 转换为一个十进制的数,用表达式可以表达如下:
其中,ss 表示一个阈值函数,满足如下关系:
通过这种转换,可以将一个像素点与邻域的差值关系用一个数表示,因为LBP 记录的是像素点与邻域像素的差值关系,所以光照变化引起像素值的同增同减不会改变LBP的大小,特别是在局部的区域,我们可以认为光照对图像造成的像素值变化是单向的,所以LBP可以很好的保存图像中像素值的差值关系。可以进一步将LBP 做直方图统计,这个直方图可以用来作为纹理分析的特征算子。
我们可以看到,RR 表示邻域的半径,PP 表示邻域像素的个数,或者bit chain 的长度,如果邻域的半径为1,则邻域的像素个数为8, bit chain 的长度为8,如果邻域半径为2,则邻域的像素个数为16,bit chain 的长度为16,邻域半径为3, 邻域的像素个数为24,bit chain 长度为24,如下图所示:
我们考虑最简单的R=1,P=8R=1,P=8 的情况,LBP 映射成一个0-255之间的某一数值,如果用直方图表示,需要一个256维的数组在存储这个直方图。为了缩小存储空间,有人提出了 uniform pattern 的编码方式,根据一个 bit chain 中 0,1之间的转换次数定义了uniform pattern。如果一个 bit chain 中,0,1 之间的转换次数不超过两次,那么这个bit chain 就是 uniform pattern, 比如,00000000 转换次数为0, 00001111 转换次数为1, 00011100 转换次数为2, 01101100 转换次数为4, 01101010 转换次数为6,那些转换次数不超过两次的pattern 都属于uniform pattern,可以证明,绝大多数的binary pattern 都是uniform pattern,通过这种定义,对于 8 bit 的 LBP, 可以从256 维降到 59维,减少了90%。
LBP 的 code 可以在下面的网站上下载:
http://www.cse.oulu.fi/CMV/Downloads/LBPMatlab
from __future__ import division import skimage.io import skimage.feature import skimage.color import skimage.transform import skimage.util import skimage.segmentation import numpy import matplotlib.pyplot as plt from skimage.segmentation import felzenszwalb from skimage.data import coffee img = coffee() for colour_channel in (0, 1, 2): img[:, :, colour_channel] = skimage.feature.local_binary_pattern( img[:, :, colour_channel], 8,1.0,method='var') plt.imshow(img); plt.show() """Gray scale and rotation invariant LBP (Local Binary Patterns). LBP is an invariant descriptor that can be used for texture classification. Parameters ---------- image : (N, M) array Graylevel image. P : int 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. * 'default': original local binary pattern which is gray scale but not rotation invariant. * 'ror': extension of default implementation which is gray scale and rotation invariant. * 'uniform': improved rotation invariance with uniform patterns and finer quantization of the angular space which is gray scale and rotation invariant. * 'nri_uniform': non rotation-invariant uniform patterns variant which is only gray scale invariant [2]_. * 'var': rotation invariant variance measures of the contrast of local image texture which is rotation but not gray scale invariant. Returns ------- output : (N, M) array LBP image. References ---------- .. [1] Multiresolution Gray-Scale and Rotation Invariant Texture Classification with Local Binary Patterns. Timo Ojala, Matti Pietikainen, Topi Maenpaa. http://www.ee.oulu.fi/research/mvmp/mvg/files/pdf/pdf_94.pdf, 2002. .. [2] Face recognition with local binary patterns. Timo Ahonen, Abdenour Hadid, Matti Pietikainen, http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.214.6851, 2004. """