LBP算法

1 灰度不变性LBP( gray scale invariant)

2 旋转不变性LBP(rotation invariant)

3 旋转不变等价LBP(rotation & uniform invariant)

1.INTRODUCTION

   二维纹理分析有许多潜在的应用,比如工业表面检查、遥感、生物图片分析,但是只有很少的例子成功发现了纹理的存在。主要的问题是,在现实世界中,由于方向、尺度及其他视觉外观的变化导致纹理常常不是均一的。灰度尺度不变性通常变得非常重要,因为不均匀的光照或图像内大幅变化。另外,已经提出的纹理测量算法计算量太大, 绝大多数纹理分类方法都显式或隐式的假设,待分类的未知样本与训练样本在在空间大小、方向和灰度尺度属性上都一致。然而,现实世界的纹理受变化的光照条件影响,可以存在任意空间分辨率和旋转方向。

  然后,罗列了一堆前人的工作……学者提出了一种计算量小并且可以有效检测大量旋转和灰度尺度不同的纹理。他们提出一种基于Local Binary Patterns【LBP特征】 的灰度尺度不变性和旋转不变性的纹理算子。从局部邻域的圆形区域内像素灰度的联合分布开始,他们推导了一个算子,对任何单调的灰度尺度变化都能显示不变性。旋转不变性是通过灰度尺度不变算子配合一系列旋转不变模式实现的。

  2.GRAY SCALE AND ROTATION INVARIANT LOCAL BINARY PATTERNS

   定义纹理 T 为一副黑白纹理图像局部邻域内图像像素灰度的联合分布:

《LBP算法》

  其中,gc 对应局部邻域中心像素的灰度值,gp (p = 1,2,…,P-1)对应半径为 R 的圆形对称邻域内 P等分的像素灰度。如果 gc 座标为 (0,0),那么 gp 的座标为…(- R sin…(2πp/P),R cos… (2πp/P))†。如下图所示,为不同采样点 P 和半径 R 的圆形对称邻域。其中对于邻域内那些没有直接落在像素【方格】中央的点的灰度值将通过二线性插值完成。

《LBP算法》

《LBP算法》

2.1  Achieving Gray-Scale Invariance

   首先,我们将邻域内中心像素点 gc 的灰度值与圆形对称区域内其他点 gp 的灰度值相减:

《LBP算法》

  接下来,我们假设差分 gp – gc  和 gc 是相互独立的,于是就有:

《LBP算法》

  在实际中,这种直接独立是不能被保证的,因此上式仅是联合分布的一种近似。由于分布 t ( gc )描述了图像整体光照,因此没有给纹理分析提供有用的信息。这样,纹理特征可以由以下联合差分分布表示:

《LBP算法》

  这是一个具有高度判别性的纹理算子。它在 P 维直方图中,记录了每个像素点的邻域中模式变化的发生次数。对于恒定的区域,所有方向的差分均为 0。对于缓慢的斜边,该算子记录了梯度方向最大的差分,沿着边的方向差分为0 。对于一个斑点,所有方向的差分都很大。

  带符号差分 gp – gc 不受平均光源变化影响。因此,联合差分分布对于灰度尺度变化具有不变性。我们通过仅使用差分的符号,而不是差分本身的值来实现灰度尺度不变性:

《LBP算法》

  通过对每个符号 s( gp – gc ) 赋予因子2^P,我们将上述公式变成唯一的 LBP值,用来描述局部图像纹理的空间结构:

《LBP算法》

当P=8,R=1时,这里的LBP值和经典的LBP值很相似,但差别在于,这边采样区域为圆形,且像素值必须通过二线性插值获得。

2.2 Achieving Rotation Invariance

 上述LBP算子将产生 2^P 个不同输出,对应由邻域内P个像素点形成的2 ^ P个不同二进制模式。当图像被旋转,【以下为意译】,灰度值gp 和g0的相对位置将发生变化,而g0通常取中心 gc 的正右 ,座标(0,R) ,这会导致不同的LBP值。但任何角度的旋转不影响,圆形对称邻域内 0 和 1 的相对位置关系,为了移除旋转获得唯一的LBP值,我们定义:

《LBP算法》

 其中,ROR(x,i)执行沿时钟方向将P位数 X 移动 i 次。对于图像像素而言,就是将邻域集合按照时钟方向旋转很多次,直到当前旋转下构成的LBP值最小。【即文中所说的,最重要位的最大值为0,即gp-1为0】

 如下图所示,对于8个采样点,将有36种唯一的旋转不变二值模式:【黑点为0,白点为1】

《LBP算法》

2.3  Improved Rotation Invariance with ªUniformº Patterns

  对于8个采样点,灰度不变性LBP将产生256中输出,旋转不变性LBP将产生36个输出,而基于unifrom的旋转不变LBP将只有9中输出。【uniform形式有58中输出】

  【上面讲的种类对于后面编程实现,计算映射mapping很重要】

  首先介绍什么是uniform,它是指均匀环形结构内包含非常少的空间转换。我们定义U(pattern),用来记录空间转换的数量,即0-1变化的次数。比如,上图第一行中00000000和11111111,它们的U值均为0,而其余种类的U均为2.类似的,上述其他27中模式的 U 值至少为4。我们将 U 值为2的模式称为 uniform

那么:

  对于8个采样点,共有58种唯一的 uniform类型的LBP值输出【此处没考虑旋转不变性,58种输出是LBP的2进制值,而非U值,U值都是2】,如下图所示:

《LBP算法》

关于uniform输出的种类,并不在该论文中,属于题外话,因为有人直接拿uniform形式去提取LBP特征。详情见《Rotation Invariant Image Description with Local Binary Pattern Histogram Fourier Features》一文。

接下来继续介绍基于uniform的旋转不变LBP,我们定义如下公式:

《LBP算法》

《LBP算法》

其中,P为采样点个数。

3.具体实现

 下面代码用C实现,读入一副灰度图像,采样点个数,采样半径,对每个像素计算LBP特征并输出,LBP图像及其直方图。

  对于旋转不变性及uniform旋转不变性,我没有对每个像素都进行上述公式的操作,由于是2进制的圆形循环,可以提前做个mapping映射关系,加快程序执行速度。

 采样点数量均为8,采样半径为10

输入图像:

《LBP算法》

3.1 灰度不变性LBP:

[cpp] 
view plain
copy
《LBP算法》
《LBP算法》

  1. void gray_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint)  
  2. {  
  3.     IplImage *target,*hist;  
  4.     int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;  
  5.     double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;  
  6.     int *result;  
  7.     float dishu;  
  8.       
  9.     dishu = 2.0;  
  10.     max_x=0;max_y=0;min_x=0;min_y=0;  
  11.     for (k=0;k<num_sp;k++)  
  12.     {  
  13.         if (max_x<spoint[k].x)  
  14.         {  
  15.             max_x=spoint[k].x;  
  16.         }  
  17.         if (max_y<spoint[k].y)  
  18.         {  
  19.             max_y=spoint[k].y;  
  20.         }  
  21.         if (min_x>spoint[k].x)  
  22.         {  
  23.             min_x=spoint[k].x;  
  24.         }  
  25.         if (min_y>spoint[k].y)  
  26.         {  
  27.             min_y=spoint[k].y;  
  28.         }  
  29.     }  
  30.   
  31.     //计算模版大小  
  32.     box_x = ceil(MAX(max_x,0)) – floor(MIN(min_x,0)) + 1;  
  33.     box_y = ceil(MAX(max_y,0)) – floor(MIN(min_y,0)) + 1;  
  34.       
  35.     if (width<box_x||height<box_y)  
  36.     {  
  37.         printf(“Too small input image. Should be at least (2*radius+1) x (2*radius+1)”);  
  38.         return;  
  39.     }  
  40.   
  41.     //计算可滤波图像大小,opencv图像数组下标从0开始  
  42.     orign_x = 0 – floor(MIN(min_x,0));//起点  
  43.     orign_y = 0 – floor(MIN(min_x,0));  
  44.       
  45.     dx = width – box_x+1;//终点  
  46.     dy = height – box_y+1;  
  47.       
  48.     int cols = pow(dishu,(float)num_sp);  
  49.     hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);//直方图图像  
  50.     target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);  
  51.     result = (int *)malloc(sizeof(int)*dx*dy);  
  52.     double *val_hist = (double *)malloc(sizeof(double)*cols);   //直方图数组  
  53.       
  54.     memset(result,0,sizeof(int)*dx*dy);  
  55.     CvRect roi =cvRect(orign_x, orign_y, dx, dy);  
  56.     cvSetImageROI(src, roi);  
  57.     cvCopy(src, target);    
  58.     cvResetImageROI(src);  
  59.     cvSaveImage(“haha.jpg”,target);  
  60.       
  61.     for ( k = 0; k<num_sp;k++)  
  62.     {  
  63.         x = spoint[k].x+orign_x;  //在原点处的样本点的座标
  64.         y = spoint[k].y+orign_y;  
  65.   
  66.         //二线性插值图像  
  67.         fy = floor(y);  //向下取整  
  68.         fx = floor(x);  
  69.         cy = ceil(y);   //向上取整  
  70.         cx = ceil(x);  
  71.         ty = y – fy;  
  72.         tx = x – fx;  
  73.         w1 = (1 – tx) * (1 – ty);  
  74.         w2 = tx  * (1 – ty);  
  75.         w3 = (1 – tx) * ty ;  
  76.         w4 = tx * ty ;  
  77.         v = pow(dishu,(float)k);  
  78.   
  79.         for (i = 0;i<dy;i++)  
  80.         {  
  81.             for (j = 0;j<dx;j++)  
  82.             {  
  83.                 //灰度插值图像像素,相当于将模板的原点的座标移动到(x,y)处,另外此处运用到了双线性插值的方法
  84.                 N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+  
  85.                       w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+  
  86.                       w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+  
  87.                       w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];  
  88.                   
  89.                 if( N >= (double)(unsigned char)target->imageData[i*dx+j])  
  90.                 {  
  91.                     result[i*dx+j] = result[i*dx+j] + v * 1;  
  92.                 }else{  
  93.                     result[i*dx+j] = result[i*dx+j] + v * 0;  
  94.                 }  
  95.             }  
  96.         }  
  97.     }  
  98.     //显示图像  
  99.     if (num_sp<=8)  
  100.     {  
  101.         //只有采样数小于8,则编码范围0-255,才能显示图像  
  102.         for (i = 0;i<dy;i++)  
  103.         {  
  104.             for (j = 0;j<dx;j++)  
  105.             {  
  106.                 target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];  
  107.                 //printf(“%d\n”,(unsigned char)target->imageData[i*width+j]);  
  108.             }  
  109.         }  
  110.         cvSaveImage(“result.jpg”,target);  
  111.     }  
  112.       
  113.     //显示直方图  
  114.   
  115.     for (i=0;i<cols;i++)  
  116.     {  
  117.         val_hist[i]=0.0;  
  118.     }  
  119.     for (i=0; i<dy*dx;i++)  
  120.     {  
  121.         val_hist[result[i]]++;  
  122.     }  
  123.       
  124.     double temp_max=0.0;  
  125.   
  126.     for (i=0;i<cols;i++)         //求直方图最大值,为了归一化  
  127.     {  
  128.         //printf(“%f\n”,val_hist[i]);  
  129.         if (temp_max<val_hist[i])  
  130.         {  
  131.             temp_max=val_hist[i];  
  132.         }  
  133.     }  
  134.     //画直方图  
  135.     CvPoint p1,p2;  
  136.     double bin_width=(double)hist->width/cols;  
  137.     double bin_unith=(double)hist->height/temp_max;  
  138.   
  139.     for (i=0;i<cols;i++)  
  140.     {  
  141.         p1.x=i*bin_width;p1.y=hist->height;  
  142.         p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;  
  143.         cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);  
  144.     }  
  145.     cvSaveImage(“hist.jpg”,hist);  
  146. }  

实验结果:

《LBP算法》

《LBP算法》

3.2 旋转不变性:

[cpp] 
view plain
copy
《LBP算法》
《LBP算法》

  1. void rotation_invariant_mapping(int range,int num_sp,int *Mapping)  
  2. {  
  3.     int newMax,rm,r;  
  4.     int *tmpMap;  
  5.   
  6.     newMax = 0;  
  7.     tmpMap = (int *)malloc(sizeof(int)*range);  
  8.     memset(tmpMap,-1,sizeof(int)*range);  
  9.   
  10.     for (int i = 0 ; i < range ; i++)  
  11.     {  
  12.         rm = i;  
  13.         r = i;  
  14.         for (int j = 0 ; j < num_sp -1 ;j++)  
  15.         {  
  16.             //将r向左循环移动一位,当r超过num_sp位时,舍弃  
  17.             r = r << 1;  
  18.             if (r > range -1)  
  19.             {  
  20.                 r = r – (range -1);  
  21.             }  
  22.             //printf(“%d,%d\n”,r,rm);  
  23.             if (r < rm)  
  24.             {  
  25.                 rm = r;  
  26.             }  
  27.         }  
  28.         if (tmpMap[rm] < 0)  
  29.         {  
  30.             tmpMap[rm] = newMax;  
  31.             newMax++;  
  32.               
  33.         }  
  34.         Mapping[i] = tmpMap[rm];  
  35.     }  
  36.     free(tmpMap);  
  37. }  
  38.   
  39. void rotation_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint,int *Mapping)  
  40. {  
  41.     IplImage *target,*hist;  
  42.     int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;  
  43.     double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;  
  44.     int *result;  
  45.     float dishu;  
  46.   
  47.     dishu = 2.0;  
  48.     max_x=0;max_y=0;min_x=0;min_y=0;  
  49.     for (k=0;k<num_sp;k++)  
  50.     {  
  51.         if (max_x<spoint[k].x)  
  52.         {  
  53.             max_x=spoint[k].x;  
  54.         }  
  55.         if (max_y<spoint[k].y)  
  56.         {  
  57.             max_y=spoint[k].y;  
  58.         }  
  59.         if (min_x>spoint[k].x)  
  60.         {  
  61.             min_x=spoint[k].x;  
  62.         }  
  63.         if (min_y>spoint[k].y)  
  64.         {  
  65.             min_y=spoint[k].y;  
  66.         }  
  67.     }  
  68.   
  69.     //计算模版大小  
  70.     box_x = ceil(MAX(max_x,0)) – floor(MIN(min_x,0)) + 1;  
  71.     box_y = ceil(MAX(max_y,0)) – floor(MIN(min_y,0)) + 1;  
  72.   
  73.     if (width<box_x||height<box_y)  
  74.     {  
  75.         printf(“Too small input image. Should be at least (2*radius+1) x (2*radius+1)”);  
  76.         return;  
  77.     }  
  78.   
  79.     //计算可滤波图像大小,opencv图像数组下标从0开始  
  80.     orign_x = 0 – floor(MIN(min_x,0));//起点  
  81.     orign_y = 0 – floor(MIN(min_x,0));  
  82.   
  83.     dx = width – box_x+1;//终点  
  84.     dy = height – box_y+1;  
  85.   
  86.     target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);  
  87.     result = (int *)malloc(sizeof(int)*dx*dy);  
  88.       
  89.     memset(result,0,sizeof(int)*dx*dy);  
  90.     CvRect roi =cvRect(orign_x, orign_y, dx, dy);  
  91.     cvSetImageROI(src, roi);  
  92.     cvCopy(src, target);    
  93.     cvResetImageROI(src);  
  94.     cvSaveImage(“haha.jpg”,target);  
  95.   
  96.     for ( k = 0; k<num_sp;k++)  
  97.     {  
  98.         x = spoint[k].x+orign_x;  
  99.         y = spoint[k].y+orign_y;  
  100.   
  101.         //二线性插值图像  
  102.         fy = floor(y);  //向下取整  
  103.         fx = floor(x);  
  104.         cy = ceil(y);   //向上取整  
  105.         cx = ceil(x);  
  106.         ty = y – fy;  
  107.         tx = x – fx;  
  108.         w1 = (1 – tx) * (1 – ty);  
  109.         w2 = tx  * (1 – ty);  
  110.         w3 = (1 – tx) * ty ;  
  111.         w4 = tx * ty ;  
  112.         v = pow(dishu,(float)k);  
  113.   
  114.         for (i = 0;i<dy;i++)  
  115.         {  
  116.             for (j = 0;j<dx;j++)  
  117.             {  
  118.                 //灰度插值图像像素  
  119.                 N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+  
  120.                     w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+  
  121.                     w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+  
  122.                     w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];  
  123.   
  124.                 if( N >= (double)(unsigned char)target->imageData[i*dx+j])  
  125.                 {  
  126.                     result[i*dx+j] = result[i*dx+j] + v * 1;  
  127.                 }else{  
  128.                     result[i*dx+j] = result[i*dx+j] + v * 0;  
  129.                 }  
  130.             }  
  131.         }  
  132.     }  
  133.   
  134.     //将result的值映射为mapping的值  
  135.     for(i = 0; i < dy ;i++)  
  136.     {  
  137.         for (j = 0; j < dx ;j ++)  
  138.         {  
  139.             result[i*dx+j] = Mapping[result[i*dx+j]];  
  140.         }  
  141.     }  
  142.   
  143.     //显示图像  
  144.     int cols = 0;//直方图的横座标,也是result数组的元素种类  
  145.     int mapping_size = pow(dishu,(float)num_sp);  
  146.     for (i = 0;i < mapping_size; i++ )  
  147.     {  
  148.         if (cols < Mapping[i])  
  149.         {  
  150.             cols = Mapping[i];  
  151.         }  
  152.     }  
  153.   
  154.     if (cols < 255)  
  155.     {  
  156.         //只有采样数小于8,则编码范围0-255,才能显示图像  
  157.         for (i = 0;i<dy;i++)  
  158.         {  
  159.             for (j = 0;j<dx;j++)  
  160.             {  
  161.                 target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];  
  162.                 //printf(“%d\n”,(unsigned char)target->imageData[i*width+j]);  
  163.             }  
  164.         }  
  165.         cvSaveImage(“result.jpg”,target);  
  166.     }  
  167.   
  168.     //计算直方图  
  169.     hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);//直方图图像  
  170.       
  171.     double *val_hist = (double *)malloc(sizeof(double)*cols);   //直方图数组  
  172.     for (i=0;i<cols;i++)  
  173.     {  
  174.         val_hist[i]=0.0;  
  175.     }  
  176.     for (i=0; i<dy*dx;i++)  
  177.     {  
  178.         val_hist[result[i]]++;  
  179.     }  
  180.   
  181.     double temp_max=0.0;  
  182.   
  183.     for (i=0;i<cols;i++)         //求直方图最大值,为了归一化  
  184.     {  
  185.         //printf(“%f\n”,val_hist[i]);  
  186.         if (temp_max<val_hist[i])  
  187.         {  
  188.             temp_max=val_hist[i];  
  189.         }  
  190.     }  
  191.     //画直方图  
  192.     CvPoint p1,p2;  
  193.     double bin_width=(double)hist->width/cols;  
  194.     double bin_unith=(double)hist->height/temp_max;  
  195.   
  196.     for (i=0;i<cols;i++)  
  197.     {  
  198.         p1.x=i*bin_width;p1.y=hist->height;  
  199.         p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;  
  200.         cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);  
  201.     }  
  202.     cvSaveImage(“hist.jpg”,hist);  
  203. }  

实验结果:

《LBP算法》

《LBP算法》

3.3 uniform 旋转不变性,给出完整代码:

[cpp] 
view plain
copy
《LBP算法》
《LBP算法》

  1. #include “stdafx.h”  
  2. #include “cv.h”  
  3. #include “highgui.h”  
  4. #define  PI 3.1415926  
  5. #define MAX(x,y) (x)>(y)?(x):(y)  
  6. #define MIN(x,y) (x)<(y)?(x):(y)  
  7.   
  8. typedef struct MyPoint  
  9. {  
  10.     double x;  
  11.     double y;  
  12. }MyPoint;  
  13. void calc_position(int radius,int num_sp,MyPoint *spoint)  
  14. {  
  15.     double theta;  
  16.   
  17.     theta = 2*PI/num_sp;  
  18.       
  19.     for (int i = 0; i < num_sp; i++)  
  20.     {  
  21.         spoint[i].y = -radius * sin(i * theta);  
  22.         spoint[i].x = radius * cos(i * theta);  
  23.     }  
  24. }  
  25. int calc_sum(int r)  
  26. {  
  27.     int res_sum;  
  28.   
  29.     res_sum = 0;  
  30.     while (r)  
  31.     {  
  32.         res_sum = res_sum + r % 2;  
  33.         r /= 2;  
  34.     }  
  35.     return res_sum;  
  36. }  
  37. void rotation_uniform_invariant_mapping(int range,int num_sp,int *Mapping)  
  38. {  
  39.     int numt,i,j,tem_xor;  
  40.   
  41.     numt = 0;  
  42.     tem_xor = 0;  
  43.     for (i = 0; i< range; i++)  
  44.     {  
  45.         j = i << 1;  
  46.         if (j > range -1)  
  47.         {  
  48.             j = j – (range -1);  
  49.         }  
  50.           
  51.         tem_xor = i ^ j;    // 异或 跳变的位置经过左移异或之后,几次调表就几个1
  52.         numt = calc_sum(tem_xor);//计算异或结果中1的个数,即跳变个数  
  53.           
  54.         if (numt <= 2)  
  55.         {  
  56.             Mapping[i] = calc_sum(i);  
  57.         }else{  
  58.             Mapping[i] = num_sp+1;  
  59.         }  
  60.     }     
  61.   
  62. }  
  63.   
  64. void rotation_uniform_invariant_lbp(IplImage *src,int height,int width,int num_sp,MyPoint *spoint,int *Mapping)  
  65. {  
  66.     IplImage *target,*hist;  
  67.     int i,j,k,box_x,box_y,orign_x,orign_y,dx,dy,tx,ty,fy,fx,cy,cx,v;  
  68.     double min_x,max_x,min_y,max_y,w1,w2,w3,w4,N,x,y;  
  69.     int *result;  
  70.     float dishu;  
  71.   
  72.     dishu = 2.0;  
  73.     max_x=0;max_y=0;min_x=0;min_y=0;  
  74.     for (k=0;k<num_sp;k++)  
  75.     {  
  76.         if (max_x<spoint[k].x)  
  77.         {  
  78.             max_x=spoint[k].x;  
  79.         }  
  80.         if (max_y<spoint[k].y)  
  81.         {  
  82.             max_y=spoint[k].y;  
  83.         }  
  84.         if (min_x>spoint[k].x)  
  85.         {  
  86.             min_x=spoint[k].x;  
  87.         }  
  88.         if (min_y>spoint[k].y)  
  89.         {  
  90.             min_y=spoint[k].y;  
  91.         }  
  92.     }  
  93.   
  94.     //计算模版大小  
  95.     box_x = ceil(MAX(max_x,0)) – floor(MIN(min_x,0)) + 1;  
  96.     box_y = ceil(MAX(max_y,0)) – floor(MIN(min_y,0)) + 1;  
  97.   
  98.     if (width<box_x||height<box_y)  
  99.     {  
  100.         printf(“Too small input image. Should be at least (2*radius+1) x (2*radius+1)”);  
  101.         return;  
  102.     }  
  103.   
  104.     //计算可滤波图像大小,opencv图像数组下标从0开始  
  105.     orign_x = 0 – floor(MIN(min_x,0));//起点  
  106.     orign_y = 0 – floor(MIN(min_x,0));  
  107.   
  108.     dx = width – box_x+1;//终点  
  109.     dy = height – box_y+1;  
  110.   
  111.     target = cvCreateImage(cvSize(dx,dy),IPL_DEPTH_8U,1);  
  112.     result = (int *)malloc(sizeof(int)*dx*dy);  
  113.   
  114.     memset(result,0,sizeof(int)*dx*dy);  
  115.     CvRect roi =cvRect(orign_x, orign_y, dx, dy);  
  116.     cvSetImageROI(src, roi);  
  117.     cvCopy(src, target);    
  118.     cvResetImageROI(src);  
  119.     cvSaveImage(“haha.jpg”,target);  
  120.   
  121.     for ( k = 0; k<num_sp;k++)  
  122.     {  
  123.         x = spoint[k].x+orign_x;  
  124.         y = spoint[k].y+orign_y;  
  125.   
  126.         //二线性插值图像  
  127.         fy = floor(y);  //向下取整  
  128.         fx = floor(x);  
  129.         cy = ceil(y);   //向上取整  
  130.         cx = ceil(x);  
  131.         ty = y – fy;  
  132.         tx = x – fx;  
  133.         w1 = (1 – tx) * (1 – ty);  
  134.         w2 = tx  * (1 – ty);  
  135.         w3 = (1 – tx) * ty ;  
  136.         w4 = tx * ty ;  
  137.         v = pow(dishu,(float)k);  
  138.   
  139.         for (i = 0;i<dy;i++)  
  140.         {  
  141.             for (j = 0;j<dx;j++)  
  142.             {  
  143.                 //灰度插值图像像素  
  144.                 N = w1 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+fx]+  
  145.                     w2 * (double)(unsigned char)src->imageData[(i+fy)*src->width+j+cx]+  
  146.                     w3 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+fx]+  
  147.                     w4 * (double)(unsigned char)src->imageData[(i+cy)*src->width+j+cx];  
  148.   
  149.                 if( N >= (double)(unsigned char)target->imageData[i*dx+j])  
  150.                 {  
  151.                     result[i*dx+j] = result[i*dx+j] + v * 1;  
  152.                 }else{  
  153.                     result[i*dx+j] = result[i*dx+j] + v * 0;  
  154.                 }  
  155.             }  
  156.         }  
  157.     }  
  158.   
  159.     //将result的值映射为mapping的值  
  160.     for(i = 0; i < dy ;i++)  
  161.     {  
  162.         for (j = 0; j < dx ;j ++)  
  163.         {  
  164.             result[i*dx+j] = Mapping[result[i*dx+j]];  
  165.         }  
  166.     }  
  167.   
  168.     //显示图像  
  169.     int cols = 0;//直方图的横座标,也是result数组的元素种类  
  170.     int mapping_size = pow(dishu,(float)num_sp);  
  171.     for (i = 0;i < mapping_size; i++ )  
  172.     {  
  173.         if (cols < Mapping[i])  
  174.         {  
  175.             cols = Mapping[i];  
  176.         }  
  177.     }  
  178.   
  179.     if (cols < 255)  
  180.     {  
  181.         //只有采样数小于8,则编码范围0-255,才能显示图像  
  182.         for (i = 0;i<dy;i++)  
  183.         {  
  184.             for (j = 0;j<dx;j++)  
  185.             {  
  186.                 target->imageData[i*dx+j] = (unsigned char)result[i*dx+j];  
  187.                 //printf(“%d\n”,(unsigned char)target->imageData[i*width+j]);  
  188.             }  
  189.         }  
  190.         cvSaveImage(“result.jpg”,target);  
  191.     }  
  192.   
  193.     //计算直方图  
  194.     hist = cvCreateImage(cvSize(300,200),IPL_DEPTH_8U,3);//直方图图像  
  195.     double *val_hist = (double *)malloc(sizeof(double)*cols);   //直方图数组  
  196.     for (i=0;i<cols;i++)  
  197.     {  
  198.         val_hist[i]=0.0;  
  199.     }  
  200.     for (i=0; i<dy*dx;i++)  
  201.     {  
  202.         val_hist[result[i]]++;  
  203.     }  
  204.   
  205.     double temp_max=0.0;  
  206.   
  207.     for (i=0;i<cols;i++)         //求直方图最大值,为了归一化  
  208.     {  
  209.         //printf(“%f\n”,val_hist[i]);  
  210.         if (temp_max<val_hist[i])  
  211.         {  
  212.             temp_max=val_hist[i];  
  213.         }  
  214.     }  
  215.     //画直方图  
  216.     CvPoint p1,p2;  
  217.     double bin_width=(double)hist->width/cols;  
  218.     double bin_unith=(double)hist->height/temp_max;  
  219.   
  220.     for (i=0;i<cols;i++)  
  221.     {  
  222.         p1.x=i*bin_width;p1.y=hist->height;  
  223.         p2.x=(i+1)*bin_width;p2.y=hist->height-val_hist[i]*bin_unith;  
  224.         cvRectangle(hist,p1,p2,cvScalar(0,255),-1,8,0);  
  225.     }  
  226.     cvSaveImage(“hist.jpg”,hist);  
  227. }  
  228.   
  229. int _tmain(int argc, _TCHAR* argv[])  
  230. {  
  231.     IplImage *src,*grey,*result;  
  232.     int samples,radius,range,*mapping;  
  233.     MyPoint *spoint;  
  234.     float Mi;  
  235.   
  236.     samples = 8;  
  237.     radius = 10;  
  238.     Mi = 2.0;  
  239.     range = pow(Mi,samples);  
  240.   
  241.     src = cvLoadImage(“test2.jpg”);  
  242.     grey = cvCreateImage(cvSize(src->width,src->height),IPL_DEPTH_8U,1);  
  243.     cvCvtColor(src,grey,CV_BGR2GRAY);  
  244.     mapping = (int *)malloc(sizeof(int)*range);  
  245.     memset(mapping,0,sizeof(int)*range);  
  246.       
  247.     //计算采样点相对座标  
  248.     spoint = (MyPoint *)malloc(sizeof(MyPoint)*samples);   
  249.     calc_position(radius,samples,spoint);  
  250.       
  251.     //计算灰度不变性LBP特征,写回浮点数图像矩阵中  
  252.     //gray_invariant_lbp(grey,src->height,src->width,samples,spoint);  
  253.   
  254.     //计算旋转不变形LBP特征  
  255.     //rotation_invariant_mapping(range,samples,mapping);  
  256.     //rotation_invariant_lbp(grey,src->height,src->width,samples,spoint,mapping);  
  257.   
  258.     //计算旋转不变等价LBP特征  
  259.     rotation_uniform_invariant_mapping(range,samples,mapping);  
  260.     rotation_uniform_invariant_lbp(grey,src->height,src->width,samples,spoint,mapping);  
  261.     return 0;  
  262. }  

 实验结果:

《LBP算法》《LBP算法》

点赞