In the earlier article, we had seen that we had computed a LBP image. Each pixel in this image still can take $2^8$ possible values. The idea of uniform LBP is to perform lossy encoding so that we eliminate some irrelevant data and retain useful information which takes on 58 possible values. In this article, we will look at Uniform LBP and computation of Spatial Histogram of LBP which can be used as a feature for image description.
- Uniform LBP features are considered as one which has only 2 contiguous regions corresponding to 0's and 1's, while non uniform LBP have more than 1 contiguous regions corresponding to 0's and 1's.
- This we need a mapping from which assign each on $2^8$ possible codes to one of 58 encoded uniform LBP values.
- All the non uniform codes are assigned to a single values.
- The uniform LBPs can be viewed as corners, line, ramp gradient
- The non uniform LBPs are assumed to be irrelevant and can be ignored.
- A uniform binary pattern can be identified by performing a bit wise traversal and checking if number of bit transitions are at most 2 .
- This is done by first circular right/left shifting the given code and performing XOR operation with the original code.
- Since we need to only consider 8 bit numbers, we need perform circular shift and masking MSB for integers.
1 int rightshift(int num, int shift)
5 return (num >> shift) | ((num << (8 - shift)&0xFF));
- Now number of bit operations is simply the number of set bits in the result.
- The code for this can be as follows:
01 int countSetBits(int code)
03 int count=0;
10 return count;
12 bool checkUniform(int code)
14 int b = rightshift(code,1);
15 int c = code ^ b;
16 int count=countSetBits(c);
17 if (count <=2)
18 return true;
20 return false;
- The method to count bits is a naive way and since we are using 8 bit words, it will take 8 iterations of the
- Brian Kernighan's method performs this by going through as many iterations as set bits:
01 int countSetBits(int code)
03 int count=0;
04 int v=code;
09 return count;
- The result can be precomputed for possible input code to determine if a LBP is a uniform code or not.
- The next task is to map the LBP codes to one of 58 uniform codes.
- The encoding will be done along the rows as per the below figure:
- Since we know the possible input codes beforehand, we can prepare a lookup table beforehand to check if there is a uniform code or not
- Now if it is a uniform code, we need to map it to one of possible 58 codes.
- To do this, we move from all numbers from $0-2^8$, check if they are uniform and assign them to a one of 58 possible codes.
- However we can see then:
03 void initUniform()
06 int index=0;
07 for(int i=0;i<=255;i++)
09 bool status=checkUniform(i);
- Thus we modify the existing lbp image code to return only uniform lbp coded as destination lbp image by performing a simple lookup operations.
- Now, the next task is to compute a spatial histogram, the histogram may be computed over the entire image or dividing the image into grids:
01 ocv::Histogram hist;
03 vector<float> spatialHistogram(Mat lbpImage,Size grid)
06 vector<float> histogram;
09 int width=lbpImage.cols/grid.width;
10 int height=lbpImage.rows/grid.height;
11 int cnt=0;
13 for(int i=0;i<grid.height;i++)
15 for(int j=0;j<grid.width;j++)
17 Mat cell=lbpImage(Rect(j*width,i*height,width,height));
18 Mat cell_hist=computeHistogram(cell);
19 Mat tmp_feature;
22 float * ptr=tmp_feature.ptr<float>(0);
23 for(int k=0;k<tmp_feature.cols;k++)
35 return histogram;
- The LBP spatial histogram can be used as a texture descriptor.
- However the LBP image is a gradient image in some sense, it encodes information about different types of gradients.
- The LBP pattern can be used to identify isolated corners or flat region (all 0 or 1).
- The LBP pattern can be used to identify corner a continuous run of 0 or 1 of length (5-8 and its rotated version).
- The LBP pattern can be used to identify a edge a continuous run of 0 or 1(length 4 and rotated version).
- The LBP pattern can be used to identify horizontal or vertical edge (vertical/horizontal run of 0 and 1).
- The LBP pattern can be used to identify a line end (1000000 and its rotated version).
- The LBP pattern with 2 continuous 1'2 can be considered as a horizontal or vertical line.
- Description of interest regions with local binary patterns by Heikkil, Pietik? & Schmid
- Rotation invariant image description with local binary pattern histogram fourier features. by Ahonen T. and Matas J. and He C & Pietik?inen M.
- Multiresolution gray-scale and rotation invariant texture classification with Local Binary Patterns by Ojala