Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

first of all, LBP features are mostly used with histograms, rarely by themselves, and uniform LBP is used for compressing the histograms from 256 bins to 59, which gives quite some speedup when comparing them later.

i hope, you read the paper

so, nice, you finished the first step of it ;) , steps 2 and 3 would be:

2: in your lbp function, calculate a histogram from the center_lbp values

Mat hist(1,256,CV_32F); // 256 bins, float for later interpolation

// later, inside the loop:
// increment the counter for each seen value, hist with 256 elements
hist.at<float>(0,center_lbp) += 1;  
// or, if uniform LBP, you'd use the lookup:
hist.at<float>(0,lookup[center_lbp]) += 1;  // hist with 59 elements

3: don't do that for the whole image, but divide it into , like 8x8 patches, collect a histogram for each small patch, and in the end, concat them to a big 1-d feature vector , the final product.

Mat spatial_histogram(Mat src, int grid_x, int grid_y, uchar lookup[256] ) {
    // calculate LBP patch size
    int width = src.cols/grid_x;
    int height = src.rows/grid_y;
    Mat result = Mat::zeros(0, 0, CV_32F);
    // iterate through grid
    for(int i = 0; i < grid_y; i++) {
        for(int j = 0; j < grid_x; j++) {
            Mat patch(src, Range(i*height,(i+1)*height), Range(j*width,(j+1)*width));
            int len = isUniform ? 59 : 256;
            Mat hist = Mat::zeros(1,len,CV_32F);
          // !! your code here (minus the img loading) :  lbp(patch,hist,lookup);
            result.push_back(hist);
        }
    }
    return result;
}

the uniform lookup itself is not too difficult.

  • a bitmask is 'uniform' if the number of transitions <= 2.
  • for an 8bit histogram, you'd need 256 slots, and only 58 of the 255 possible values are uniform.
  • a histogram for 'uniform' lbp features would have only 59 bins, one for each uniform value, and the last fo all others ( 58+noise )
  • you would make a lookup[256] table to translate from 8bit_value [0-255] -> 59 bins[0-58] in the histogram, run from 0 to 255, count the binary transitions for each number, and if it's uniform, set to an (increased) index, anything else gets set to 'noise' value

i'm actually getting lazy, for code look here