LBP(Local Binary Pattern,局部二值模式)是一种用来描述图像局部纹理特征的算子;它具有旋转不变性和灰度不变性等显著的优点。它是首先由T. Ojala, M.Pietik?inen, 和D. Harwood 在1994年提出,用于纹理特征提取。而且,提取的特征是图像的局部的纹理特征;
1、LBP特征的描述
原始的LBP算子定义为在3*3的窗口内,以窗口中心像素为阈值,将相邻的8个像素的灰度值与其进行比较,若周围像素值大于中心像素值,则该像素点的位置被标记为1,否则为0。这样,3*3邻域内的8个点经比较可产生8位二进制数(通常转换为十进制数即LBP码,共256种),即得到该窗口中心像素点的LBP值,并用这个值来反映该区域的纹理信息。如下图所示:

LBP的改进版本:
原始的LBP提出后,研究人员不断对其提出了各种改进和优化。
(1)圆形LBP算子:
基本的 LBP 算子的最大缺陷在于它只覆盖了一个固定半径范围内的小区域,这显然不能满足不同尺寸和频率纹理的需要。为了适应不同尺度的纹理特征,并达到灰度和旋转不变性的要求,Ojala 等对 LBP 算子进行了改进,将 3×3 邻域扩展到任意邻域,并用圆形邻域代替了正方形邻域,改进后的 LBP 算子允许在半径为 R 的圆形邻域内有任意多个像素点。从而得到了诸如半径为R的圆形区域内含有P个采样点的LBP算子;

(2)LBP旋转不变模式
从 LBP 的定义可以看出,LBP 算子是灰度不变的,但却不是旋转不变的。图像的旋转就会得到不同的 LBP值。
Maenpaa等人又将 LBP 算子进行了扩展,提出了具有旋转不变性的 LBP 算子,即不断旋转圆形邻域得到一系列初始定义的 LBP 值,取其最小值作为该邻域的 LBP 值。
图 2.5 给出了求取旋转不变的 LBP 的过程示意图,图中算子下方的数字表示该算子对应的 LBP 值,图中所示的 8 种 LBP模式,经过旋转不变的处理,最终得到的具有旋转不变性的 LBP 值为 15。也就是说,图中的 8 种 LBP 模式对应的旋转不变的 LBP 模式都是00001111。

(3)LBP等价模式
一个LBP算子可以产生不同的二进制模式,对于半径为R的圆形区域内含有P个采样点的LBP算子将会产生P2 种模式。很显然,随着邻域集内采样点数的增加,二进制模式的种类是急剧增加的。例如:5×5邻域内20个采样点,有220=1,048,576种二进制模式。如此多的二值模式无论对于纹理的提取还是对于纹理的识别、分类及信息的存取都是不利的。同时,过多的模式种类对于纹理的表达是不利的。例如,将LBP算子用于纹理分类或人脸识别时,常采用LBP模式的统计直方图来表达图像的信息,而较多的模式种类将使得数据量过大,且直方图过于稀疏。因此,需要对原始的LBP模式进行降维,使得数据量减少的情况下能最好的代表图像的信息。
为了解决二进制模式过多的问题,提高统计性,Ojala提出了采用一种“等价模式”(Uniform Pattern)来对LBP算子的模式种类进行降维。Ojala等认为,在实际图像中,绝大多数LBP模式最多只包含两次从1到0或从0到1的跳变。因此,Ojala将“等价模式”定义为:当某个LBP所对应的循环二进制数从0到1或从1到0最多有两次跳变时,该LBP所对应的二进制就称为一个等价模式类。如00000000(0次跳变),00000111(只含一次从0到1的跳变),10001111(先由1跳到0,再由0跳到1,共两次跳变)都是等价模式类。除等价模式类以外的模式都归为另一类,称为混合模式类,例如10010111(共四次跳变)(这是我的个人理解,不知道对不对)。
通过这样的改进,二进制模式的种类大大减少,而不会丢失任何信息。模式数量由原来的2P种减少为 P ( P-1)+2种,其中P表示邻域集内的采样点数。对于3×3邻域内8个采样点来说,二进制模式由原始的256种减少为58种,这使得特征向量的维数更少,并且可以减少高频噪声带来的影响。
2、LBP特征用于检测的原理
显而易见的是,上述提取的LBP算子在每个像素点都可以得到一个LBP“编码”,那么,对一幅图像(记录的是每个像素点的灰度值)提取其原始的LBP算子之后,得到的原始LBP特征依然是“一幅图片”(记录的是每个像素点的LBP值)。

LBP的应用中,如纹理分类、人脸分析等,一般都不将LBP图谱作为特征向量用于分类识别,而是采用LBP特征谱的统计直方图作为特征向量用于分类识别。
因为,从上面的分析我们可以看出,这个“特征”跟位置信息是紧密相关的。直接对两幅图片提取这种“特征”,并进行判别分析的话,会因为“位置没有对准”而产生很大的误差。后来,研究人员发现,可以将一幅图片划分为若干的子区域,对每个子区域内的每个像素点都提取LBP特征,然后,在每个子区域内建立LBP特征的统计直方图。如此一来,每个子区域,就可以用一个统计直方图来进行描述;整个图片就由若干个统计直方图组成;
例如:一幅100*100像素大小的图片,划分为10*10=100个子区域(可以通过多种方式来划分区域),每个子区域的大小为10*10像素;在每个子区域内的每个像素点,提取其LBP特征,然后,建立统计直方图;这样,这幅图片就有10*10个子区域,也就有了10*10个统计直方图,利用这10*10个统计直方图,就可以描述这幅图片了。之后,我们利用各种相似性度量函数,就可以判断两幅图像之间的相似性了;
3、对LBP特征向量进行提取的步骤
(1)首先将检测窗口划分为16×16的小区域(cell);
(2)对于每个cell中的一个像素,将相邻的8个像素的灰度值与其进行比较,若周围像素值大于中心像素值,则该像素点的位置被标记为1,否则为0。这样,3*3邻域内的8个点经比较可产生8位二进制数,即得到该窗口中心像素点的LBP值;
(3)然后计算每个cell的直方图,即每个数字(假定是十进制数LBP值)出现的频率;然后对该直方图进行归一化处理。
(4)最后将得到的每个cell的统计直方图进行连接成为一个特征向量,也就是整幅图的LBP纹理特征向量;
然后便可利用SVM或者其他机器学习算法进行分类了。
4、LBP特征的实现(参考博客:http://blog.csdn.net/jinshengtao/article/details/18219697)
下面代码用C实现,读入一副灰度图像,采样点个数,采样半径,对每个像素计算LBP特征并输出,LBP图像及其直方图。
对于旋转不变性及uniform旋转不变性,我没有对每个像素都进行上述公式的操作,由于是2进制的圆形循环,可以提前做个mapping映射关系,加快程序执行速度。
采样点数量均为8,采样半径为10
输入图像:

4.1 灰度不变性LBP:
- void gray_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint)
- {
- IplImage *target,*hist;
- int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;
- double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;
- int *result;
- float dishu;
-
- dishu = 2.0;
- max_x=0;max_y=0;min_x=0;min_y=0;
- for (k=0;k
- {
- if (max_x
- {
- max_x=spoint[k].x;
- }
- if (max_y
- {
- max_y=spoint[k].y;
- }
- if (min_x>spoint[k].x)
- {
- min_x=spoint[k].x;
- }
- if (min_y>spoint[k].y)
- {
- min_y=spoint[k].y;
- }
- }
-
-
- box_x = ceil(MAX(max_x,0)) - floor(MIN(min_x,0)) + 1;
- box_y = ceil(MAX(max_y,0)) - floor(MIN(min_y,0)) + 1;
-
- if (width
- {
- printf("Too small input image. Should be at least (2*radius+1) x (2*radius+1)");
- return;
- }
-
-
- orign_x = 0 - floor(MIN(min_x,0));
- orign_y = 0 - floor(MIN(min_x,0));
-
- dx = width - box_x+1;
- dy = height - box_y+1;
-
- int cols = pow(dishu,(float)num_sp);
- hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);
- target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);
- result = (int *)malloc(sizeof(int)*dx*dy);
- double *val_hist = (double *)malloc(sizeof(double)*cols);
-
- memset(result,0,sizeof(int)*dx*dy);
- CvRect roi =cvRect(orign_x, orign_y, dx, dy);
- cvSetImageROI(src, roi);
- cvCopy(src, target);
- cvResetImageROI(src);
- cvSaveImage("haha.jpg",target);
-
- for ( k = 0; k
- {
- x = spoint[k].x+orign_x;
- y = spoint[k].y+orign_y;
-
-
- fy = floor(y);
- fx = floor(x);
- cy = ceil(y);
- cx = ceil(x);
- ty = y - fy;
- tx = x - fx;
- w1 = (1 - tx) * (1 - ty);
- w2 = tx * (1 - ty);
- w3 = (1 - tx) * ty ;
- w4 = tx * ty ;
- v = pow(dishu,(float)k);
-
- for (i = 0;i
- {
- for (j = 0;j
- {
-
- N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+
- w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+
- w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+
- w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];
-
- if( N >= (double)(unsigned char)target->imageData[i*dx+j])
- {
- result[i*dx+j] = result[i*dx+j] + v * 1;
- }else{
- result[i*dx+j] = result[i*dx+j] + v * 0;
- }
- }
- }
- }
-
- if (num_sp<=8)
- {
-
- for (i = 0;i
- {
- for (j = 0;j
- {
- target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];
-
- }
- }
- cvSaveImage("result.jpg",target);
- }
-
-
-
- for (i=0;i
- {
- val_hist[i]=0.0;
- }
- for (i=0; i
- {
- val_hist[result[i]]++;
- }
-
- double temp_max=0.0;
-
- for (i=0;i
- {
-
- if (temp_max
- {
- temp_max=val_hist[i];
- }
- }
-
- CvPoint p1,p2;
- double bin_width=(double)hist->width/cols;
- double bin_unith=(double)hist->height/temp_max;
-
- for (i=0;i
- {
- p1.x=i*bin_width;p1.y=hist->height;
- p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;
- cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);
- }
- cvSaveImage("hist.jpg",hist);
- }
实验结果:


4.2 旋转不变性:
- void rotation_invariant_mapping(int range,int num_sp,int *Mapping)
- {
- int newMax,rm,r;
- int *tmpMap;
-
- newMax = 0;
- tmpMap = (int *)malloc(sizeof(int)*range);
- memset(tmpMap,-1,sizeof(int)*range);
-
- for (int i = 0 ; i < range ; i++)
- {
- rm = i;
- r = i;
- for (int j = 0 ; j < num_sp -1 ;j++)
- {
-
- r = r << 1;
- if (r > range -1)
- {
- r = r - (range -1);
- }
-
- if (r < rm)
- {
- rm = r;
- }
- }
- if (tmpMap[rm] < 0)
- {
- tmpMap[rm] = newMax;
- newMax++;
-
- }
- Mapping[i] = tmpMap[rm];
- }
- free(tmpMap);
- }
-
- void rotation_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint,int *Mapping)
- {
- IplImage *target,*hist;
- int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;
- double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;
- int *result;
- float dishu;
-
- dishu = 2.0;
- max_x=0;max_y=0;min_x=0;min_y=0;
- for (k=0;k
- {
- if (max_x
- {
- max_x=spoint[k].x;
- }
- if (max_y
- {
- max_y=spoint[k].y;
- }
- if (min_x>spoint[k].x)
- {
- min_x=spoint[k].x;
- }
- if (min_y>spoint[k].y)
- {
- min_y=spoint[k].y;
- }
- }
-
-
- box_x = ceil(MAX(max_x,0)) - floor(MIN(min_x,0)) + 1;
- box_y = ceil(MAX(max_y,0)) - floor(MIN(min_y,0)) + 1;
-
- if (width
- {
- printf("Too small input image. Should be at least (2*radius+1) x (2*radius+1)");
- return;
- }
-
-
- orign_x = 0 - floor(MIN(min_x,0));
- orign_y = 0 - floor(MIN(min_x,0));
-
- dx = width - box_x+1;
- dy = height - box_y+1;
-
- target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);
- result = (int *)malloc(sizeof(int)*dx*dy);
-
- memset(result,0,sizeof(int)*dx*dy);
- CvRect roi =cvRect(orign_x, orign_y, dx, dy);
- cvSetImageROI(src, roi);
- cvCopy(src, target);
- cvResetImageROI(src);
- cvSaveImage("haha.jpg",target);
-
- for ( k = 0; k
- {
- x = spoint[k].x+orign_x;
- y = spoint[k].y+orign_y;
-
-
- fy = floor(y);
- fx = floor(x);
- cy = ceil(y);
- cx = ceil(x);
- ty = y - fy;
- tx = x - fx;
- w1 = (1 - tx) * (1 - ty);
- w2 = tx * (1 - ty);
- w3 = (1 - tx) * ty ;
- w4 = tx * ty ;
- v = pow(dishu,(float)k);
-
- for (i = 0;i
- {
- for (j = 0;j
- {
-
- N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+
- w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+
- w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+
- w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];
-
- if( N >= (double)(unsigned char)target->imageData[i*dx+j])
- {
- result[i*dx+j] = result[i*dx+j] + v * 1;
- }else{
- result[i*dx+j] = result[i*dx+j] + v * 0;
- }
- }
- }
- }
-
-
- for(i = 0; i < dy ;i++)
- {
- for (j = 0; j < dx ;j ++)
- {
- result[i*dx+j] = Mapping[result[i*dx+j]];
- }
- }
-
-
- int cols = 0;
- int mapping_size = pow(dishu,(float)num_sp);
- for (i = 0;i < mapping_size; i++ )
- {
- if (cols < Mapping[i])
- {
- cols = Mapping[i];
- }
- }
-
- if (cols < 255)
- {
-
- for (i = 0;i
- {
- for (j = 0;j
- {
- target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];
-
- }
- }
- cvSaveImage("result.jpg",target);
- }
-
-
- hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);
-
- double *val_hist = (double *)malloc(sizeof(double)*cols);
- for (i=0;i
- {
- val_hist[i]=0.0;
- }
- for (i=0; i
- {
- val_hist[result[i]]++;
- }
-
- double temp_max=0.0;
-
- for (i=0;i
- {
-
- if (temp_max
- {
- temp_max=val_hist[i];
- }
- }
-
- CvPoint p1,p2;
- double bin_width=(double)hist->width/cols;
- double bin_unith=(double)hist->height/temp_max;
-
- for (i=0;i
- {
- p1.x=i*bin_width;p1.y=hist->height;
- p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;
- cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);
- }
- cvSaveImage("hist.jpg",hist);
- }
实验结果:


4.3 uniform 旋转不变性,给出完整代码:
- #include "stdafx.h"
- #include "cv.h"
- #include "highgui.h"
- #define PI 3.1415926
- #define MAX(x,y) (x)>(y)?(x):(y)
- #define MIN(x,y) (x)<(y)?(x):(y)
-
- typedef struct MyPoint
- {
- double x;
- double y;
- }MyPoint;
- void calc_position(int radius,int num_sp,MyPoint *spoint)
- {
- double theta;
-
- theta = 2*PI/num_sp;
-
- for (int i = 0; i < num_sp; i++)
- {
- spoint[i].y = -radius * sin(i * theta);
- spoint[i].x = radius * cos(i * theta);
- }
- }
- int calc_sum(int r)
- {
- int res_sum;
-
- res_sum = 0;
- while (r)
- {
- res_sum = res_sum + r % 2;
- r /= 2;
- }
- return res_sum;
- }
- void rotation_uniform_invariant_mapping(int range,int num_sp,int *Mapping)
- {
- int numt,i,j,tem_xor;
-
- numt = 0;
- tem_xor = 0;
- for (i = 0; i< range; i++)
- {
- j = i << 1;
- if (j > range -1)
- {
- j = j - (range -1);
- }
-
- tem_xor = i ^ j;
- numt = calc_sum(tem_xor);
-
- if (numt <= 2)
- {
- Mapping[i] = calc_sum(i);
- }else{
- Mapping[i] = num_sp+1;
- }
- }
-
- }
-
- void rotation_uniform_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint,int *Mapping)
- {
- IplImage *target,*hist;
- int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;
- double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;
- int *result;
- float dishu;
-
- dishu = 2.0;
- max_x=0;max_y=0;min_x=0;min_y=0;
- for (k=0;k
- {
- if (max_x
- {
- max_x=spoint[k].x;
- }
- if (max_y
- {
- max_y=spoint[k].y;
- }
- if (min_x>spoint[k].x)
- {
- min_x=spoint[k].x;
- }
- if (min_y>spoint[k].y)
- {
- min_y=spoint[k].y;
- }
- }
-
-
- box_x = ceil(MAX(max_x,0)) - floor(MIN(min_x,0)) + 1;
- box_y = ceil(MAX(max_y,0)) - floor(MIN(min_y,0)) + 1;
-
- if (width
- {
- printf("Too small input image. Should be at least (2*radius+1) x (2*radius+1)");
- return;
- }
-
-
- orign_x = 0 - floor(MIN(min_x,0));
- orign_y = 0 - floor(MIN(min_x,0));
-
- dx = width - box_x+1;
- dy = height - box_y+1;
-
- target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);
- result = (int *)malloc(sizeof(int)*dx*dy);
-
- memset(result,0,sizeof(int)*dx*dy);
- CvRect roi =cvRect(orign_x, orign_y, dx, dy);
- cvSetImageROI(src, roi);
- cvCopy(src, target);
- cvResetImageROI(src);
- cvSaveImage("haha.jpg",target);
-
- for ( k = 0; k
- {
- x = spoint[k].x+orign_x;
- y = spoint[k].y+orign_y;
-
-
- fy = floor(y);
- fx = floor(x);
- cy = ceil(y);
- cx = ceil(x);
- ty = y - fy;
- tx = x - fx;
- w1 = (1 - tx) * (1 - ty);
- w2 = tx * (1 - ty);
- w3 = (1 - tx) * ty ;
- w4 = tx * ty ;
- v = pow(dishu,(float)k);
-
- for (i = 0;i
- {
- for (j = 0;j
- {
-
- N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+
- w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+
- w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+
- w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];
-
- if( N >= (double)(unsigned char)target->imageData[i*dx+j])
- {
- result[i*dx+j] = result[i*dx+j] + v * 1;
- }else{
- result[i*dx+j] = result[i*dx+j] + v * 0;
- }
- }
- }
- }
-
-
- for(i = 0; i < dy ;i++)
- {
- for (j = 0; j < dx ;j ++)
- {
- result[i*dx+j] = Mapping[result[i*dx+j]];
- }
- }
-
-
- int cols = 0;
- int mapping_size = pow(dishu,(float)num_sp);
- for (i = 0;i < mapping_size; i++ )
- {
- if (cols < Mapping[i])
- {
- cols = Mapping[i];
- }
- }
-
- if (cols < 255)
- {
-
- for (i = 0;i
- {
- for (j = 0;j
- {
- target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];
-
- }
- }
- cvSaveImage("result.jpg",target);
- }
-
-
- hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);
- double *val_hist = (double *)malloc(sizeof(double)*cols);
- for (i=0;i
- {
- val_hist[i]=0.0;
- }
- for (i=0; i
- {
- val_hist[result[i]]++;
- }
-
- double temp_max=0.0;
-
- for (i=0;i
- {
-
- if (temp_max
- {
- temp_max=val_hist[i];
- }
- }
-
- CvPoint p1,p2;
- double bin_width=(double)hist->width/cols;
- double bin_unith=(double)hist->height/temp_max;
-
- for (i=0;i
- {
- p1.x=i*bin_width;p1.y=hist->height;
- p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;
- cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);
- }
- cvSaveImage("hist.jpg",hist);
- }
-
- int _tmain(int argc, _TCHAR* argv[])
- {
- IplImage *src,*grey,*result;
- int samples,radius,range,*mapping;
- MyPoint *spoint;
- float Mi;
-
- samples = 8;
- radius = 10;
- Mi = 2.0;
- range = pow(Mi,samples);
-
- src = cvLoadImage("test2.jpg");
- grey = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);
- cvCvtColor(src,grey,CV_BGR2GRAY);
- mapping = (int *)malloc(sizeof(int)*range);
- memset(mapping,0,sizeof(int)*range);
-
-
- spoint = (MyPoint *)malloc(sizeof(MyPoint)*samples);
- calc_position(radius,samples,spoint);
-
-
-
-
-
-
-
-
-
- rotation_uniform_invariant_mapping(range,samples,mapping);
- rotation_uniform_invariant_lbp(grey,src->height,src->width,samples,spoint,mapping);
- return 0;
- }
实验结果:

越到后面越黑,因为输出的种类越来越少。