learner
learner

Reputation: 1219

Creating LBP histogram

I have been able to implement a basic local binary pattern (LBP), without interpolation. Following is the code: (OpenCV)

int center = 0;   
int center_lbp = 0;   

for (int row = 1; row < Image.rows; row++)   
{
  for (int col = 1; col < Image.cols; col++)   
  {   
    center = Image.at<int>(row, col);
    center_lbp = 0;   

    if ( center <= Image.at<int>(row-1, col-1) )   
      center_lbp += 1;   

    if ( center <= Image.at<int>(row-1, col) )   
      center_lbp += 2;   

    if ( center <= Image.at<int>(row-1, col+1) )   
      center_lbp += 4;   

    if ( center <= Image.at<int>(row, col-1) )   
      center_lbp += 8;   

    if ( center <= Image.at<int>(row, col+1) )   
      center_lbp += 16;   

    if ( center <= Image.at<int>(row+1, col-1) )   
      center_lbp += 32;   

    if ( center <= Image.at<int>(row+1, col) )   
      center_lbp += 64;

    if ( center <= Image.at<int>(row+1, col+1) )   
      center_lbp += 128;   

    cout << "center lbp value: " << center_lbp << endl;
    LBPImage.at<int>(row, col) = center_lbp;
  }
}

Read lot of things...but can't figure out how to create histogram now with uniform patterns concept...did check few links...nothing much there....can anybody please help...

thanks!

Upvotes: 3

Views: 3586

Answers (1)

Saikat
Saikat

Reputation: 1219

Your code seems that, you are using 8-neighborhood LBP with radius r=1. So you can get maximum 256(2^8) number of different values. To construct Histogram, declare a matrix containing single row and 256 columns(bins) and initialize it with 0 :-

Mat H(1,256,CV_8UC1,Scalar::all(0));

Now for each central pixel ( pixels not in border ) ,after applying LBP you will get a binary string of length 8, and it's decimal encoding ( stored in center_lbp variable in your code segment ) represents corresponding bin of the histogram. So after calculating center_lbp just increment the value into the corresponding bin of the histogram as follows :-

H.at<uchar>(center_lbp)+=1;

After parsing the image for each central pixel, you will get a LBP histogram for that image.

LBP With Uniform Patterns :-

A LBP is called uniform if the circular binary pattern ( clockwise ) contains maximum 2 transition from 0 to 1 and vice versa . For Ex:- 00111000 is uniform pattern but 10111000 is not. To construct LBP Histogram for uniform pattern, the rule is -

  • Each uniform pattern has a it's unique bin in the histogram.
  • All non uniform pattern will put into a single bin in the histogram.

for p number of neighbouring points, we can get maximum p*(p-1)+2 number of uniform patterns. So the histogram for uniform pattern will contain p*(p-1)+3 number of bins.

To construct the histogram :-

  • get binary pattern for each central pixel by applying LBP on it.
  • check whether the pattern is uniform or not.
  • if uniform, then increment bin value in the reserved bin for this pattern of histogram by 1 . you must have some mapping criteria to map a uniform pattern in the unique bin of the histogram.
  • else it is a non-uniform pattern. So increment bin value in the reserved bin( Ex.- the last bin ) for all non uniform pattern of the histogram by 1 .

Upvotes: 3

Related Questions