Code covered by the BSD License  

Highlights from
Local binary patterns


4.3 | 18 ratings Rate this file 274 Downloads (last 30 days) File Size: 25.2 KB File ID: #36484
image thumbnail

Local binary patterns



01 May 2012 (Updated )

Calculates image LBP (Local binary patterns).

| Watch this File

File Information

The LBP tests the relation between pixel and it's neighbors, encoding this relation into a binary word. This allows detection of patterns/features, while being immune to contrast changes.
Current submission presents pixel-wise implementation, and filtering based implementation, achieving much shorter run-times. Both implementations achieve same results, while running at different passes. Do not use ‘pixelwiseLBP’ unless for educational or debugging purposes.
Current implementation is aligned with "Gray Scale and Rotation Invariant Texture Classification with Local Binary Patterns" from
The differences:
This implementation supports multi-color inputs (RGB).
Rotation invariance can be same for all channels, or channel wise manner.


This file inspired Opposite Color Local Binary Patterns (Oc Lbp).

Required Products Image Processing Toolbox
Signal Processing Toolbox
MATLAB release MATLAB 7.10 (R2010a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (48)
23 Oct 2014 hamad mahmood

Thank you Nikolay.

23 Oct 2014 hamad mahmood  
22 Oct 2014 Nikolay S.

Hi hamad mahmood.
I have not used LBP for face detection, but the topic is highly researched. Here is one of most referenced papers:
I've also participated in a project where Vaioa Jones OpenCV LBP implementtation was used:,<FaceRecognizer> createLBPHFaceRecognizer(int radius, int neighbors, int grid_x, int grid_y, double threshold). You should give it a glance too.
Best regards,

22 Oct 2014 hamad mahmood

Hi Nikolay, i'm trying to extract the features from faces using matlab, but could not be able to get good results, kindly help me in this using LBP.

16 Oct 2014 Nikolay S.

Hi mahdi.
thank for the high grading. I've replied to your email.
Best regards,

16 Oct 2014 mahdi

Hi Nikolay,Thanks a lot. Would you be able to email me your nonparametric classifier codes described in your paper at ? I need it for texture recognition and compare with my results. my email address is Thanks a lot

08 Oct 2014 saurabh agarwal  
24 Sep 2014 anin keyen  
17 Sep 2014 Kathrin

Thank you! I have got your idea.

15 Sep 2014 Nikolay S.

Hi Kathrin.
Well, the use of the term bi-linear interpolation was a bit misleading. I'm actually doing a calculation we can refer to as bi-linear decomposition. That is, for a real number, I'm trying to find his nearest integer neighbors and their distances to him.
The bi-linear interpolation of those neighbors should result in the number we had in the first place. From my calculation the above statements is true for my formula.
Would you agree with me?
Best regards.

15 Sep 2014 Kathrin

Hi Nikolay,
I have calculated the bilinear interpolation referring to expressions on the page (
As you can see, the weight is calculated by the multiplication of two differentials. Would you agree with me?
Best regards.

13 Sep 2014 Nikolay S.

Hi Kathrin.
Thanks for your comment- it seems you've gone intensively through my code.
The filter elements is composed of an average between "ceil" (larger neighbor) and "floor" (smaller neighbor) value of the x and y components, aimed to calculate weight of nearest neighbors from a fractional number. As you have noticed, it is indeed a linear interpolation, implying I could used build in functions, rather than implementing it myself.
Anyhow, I do not see a reason to use multiplication here, and unless I'm being mistaken here the proposed equation seems to be fine.
Best regards, Nikolay.

12 Sep 2014 Kathrin

Hi Nikolay,
Thanks for the code.I'm comfused about an equation in generateRadialFilterLBP.m.That is ' radInterpFilt( rowsFloor(iP), colsFloor(iP), iP )= radInterpFilt( rowsFloor(iP), colsFloor(iP), iP )+rowsDistFloor(iP)+colsDistFloor(iP);'. If it is a bilinear interpolation process. I think it should be a multiplication.That is to say, 'radInterpFilt( rowsFloor(iP), colsFloor(iP), iP )= radInterpFilt( rowsFloor(iP), colsFloor(iP), iP )+rowsDistFloor(iP)*colsDistFloor(iP);'. Would you agree with me?
Best regards!

08 Sep 2014 Nikolay S.

Michael, thank you for:
1) Finding the bug.
2) Helping me finding the root cause.
3) The high rating!
Best regards!

08 Sep 2014 Michael

Thank you for the great support!

04 Sep 2014 Nikolay S.

Hi Michael.
It seems the shift value with which the circshift is called is not an integer number. I do not see this happening when I'm running the demo. Let me email you so we can figure out the problem.
Best regards,

04 Sep 2014 Michael

When I run demoEfficientLBP.m, I receive the following error message:
Error using circshift>ParseInputs (line 71)
Invalid shift type: must be a finite, nonsparse, real integer vector.

Error in circshift (line 34)
[p, sizeA, numDimsA] = ParseInputs(a,p);

Error in pixelwiseLBP>sortNeighbours (line 198)
minLBP=weigthVec*circshift(origBinWord, iCircShift);

Error in pixelwiseLBP (line 147)
[minLBP, ~]=sortNeighbours(binaryWord, weigthVec,...

Error in demoEfficentLBP (line 49)
pwLBP=pixelwiseLBP(imgRGB, 'filtR', filtR, 'isRotInv', true, 'isChanWiseRot',

Any idea what's wrong here?

28 Aug 2014 trisha  
22 Jul 2014 Faisall Achsani


18 Jul 2014 samira

I need it

03 Jul 2014 Nikolay S.


03 Jul 2014 slimane

Hi Mr .Nikolay ,
This is a very good work

02 Jul 2014 Nikolay S.

Hi Paresh.
I'd suggest to send your questions to my email, rather then commenting here.
Regarding your question, you should use more then one histogram element. This is called "feature vector". In some cases you can reduce the feature vector length via PCA, and other methods. In case of LBP it was proven that some histogram elements carry more information than others, such as Rotation Invariant and others. I suggest reading relevant publications.
Best regards.

02 Jul 2014 Paresh

Ok. Thank You.
I have one more question, after we get the histogram feature vector how should I input that into my features matrix? i.e as it is or should I reduce LBP feature vector to a single feature? Please Reply. Thank You

28 Jun 2014 Nikolay S.

Radial filter is uniform to all directions, and in most cases does the trick. Other shape filters can also be handy (especially if the problem in hand has other geometry shape preferences), but radial shape is the one commonly used in published papers.
Hope this will help a bit

28 Jun 2014 Paresh

Let me rephrase it. Why do we need radial filter at first place? Since in literature we are directly taking difference from the center pixel. Please Explain. Thank You

28 Jun 2014 Nikolay S.

Hi Paresh.
The multiplication is needed for proper weighting of the radial filter elements. In many cases the filter element is between pixels, and therefore a an averaging ought to be done.
Best regards.

28 Jun 2014 Paresh

While calculating pixel-wise LBP, what is the need to multiply it with radial filter? Because when I read it they are only taking difference of neighbouring pixels from the center pixel. Please explain. Thank You.

14 May 2014 Nikolay S.

Hi KaMu.
Channel wise rotation means that for a given pixel, each color channel rotation is calculated independently- without considering the rotation of other colors.

13 May 2014 KaMu

Hi Nikolay,
Thanks for the code. What do you mean by rotational channel wise. Do you mean that you rotate on one color channel?

23 Apr 2014 Nikolay S.

Hello naif al harbi.
Well, you can have it, using the "Download submission" button above.
From the relatively low grade I can see you've found the button, but did not like the function... I'd be happy to hear your comments and improvement proposals.
Best regards.

23 Apr 2014 Nikolay S.

Hello naif al harbi.
Well, you can have it, using the "Download submission" button above.
For the relatively low grade I can see you've found the button, but did not like the function...
Best regards.

23 Apr 2014 naif al harbi  
22 Apr 2014 naif al harbi


i wante code the LBP in matlab

02 Apr 2014 kemi  
22 Jan 2014 Nicola Franzoso  
10 Jan 2014 Nikolay S.

Good evening Nicola Franzoso.
You'r comment is in place- thank you. I plan to add relevant functionality to the submission. The histogram is easy to achieve using "imhist" or "hist" with bins 1:(2^p-1) where p is the number of filter elements, applied to the LBP matrix converted to "single". What prevents me form posting this, it the Rotation Invariant (RI) case, where the histogram becomes very sparse. I failed to find an analytic way (an equation) to predict the indexes of obsolete bins. I can find the indexes of those bins for each case by applying the LBP minimization procedure, but this is inefficient and nasty. So i'm working on it. If you're not using RI-LBP, or you're fine with sparse histogram, please use my advice above.
Best wishes.

10 Jan 2014 Nicola Franzoso

Nikolay, very good, but where i can fing the histogram, that's the template of the image ?

09 Jan 2014 shan

Hi Nikolay ,
Very great work thanks..

14 Feb 2013 BIREM Merwan  
03 Oct 2012 Nikolay S.

Hi Charles Ding.
Well LBP is pretty simple. As far as I remember I've used Wikipedia, and some Google on top of it to understand it. This should be enough...

21 Sep 2012 Charles ding

HI, Nikolay,

Could you please tell me a good literature about efficient LBP realized in the code? I want to understand its principles.

28 Aug 2012 Nikolay S.

Chris Forne, thanks for your comment. Indeed I've overlooked some issues. I hope I've resolved all- both those you've mentioned, and some others I've found myself.

15 Aug 2012 Chris Forne

Unfortunately this function does not compute the LBP correctly.
To fix it you need to change 'sign(currNieghDiff)' to 'currNieghDiff > 0'.
You should also change 'if nNeigh<=8' to 'if nNeigh<=9', as nNeigh = number of neighbours + 1.

Finally in the Primitive pixelwise solution you need to change
neighMat( ceil(nNeigh/2))=false;
neighMat( ceil(nNeigh/2)+ 1 )=false;

09 Aug 2012 Rakesh Singh


i wanted to know how to implement the basic concept of LBP operator with uniform patterns
i have implemented the basic concept of LBP operator in matlab
now i am using the concept of uniform pattern
the problem is that i am not able to understand the basic concept of LBP with uniform patterns

How exactly it has to be done

is there any documentation available which can help me

17 Jul 2012 Nikolay S.

Hello Fa Fa.
As far as I'm concerned, this comments are here for feedback, errors reporting , ratings, thanks, and code related question. Your question (and similar ones) should be be addressed via other media (directly via email, etc..).

Now, to your issue:
I don't quiet understand the question/. As far as I can see you have an interesting project ahead of you. If by "help" you mean "will I do the project for you", the answer is definitely NO.
If you wish to get some assistance with the topics of your project, that's better, but again I'm not the address in this case. I'd help you if were carrying this project under my supervision, but you aren't. I would help you nevertheless, if I was proficient in the above topics, and had some spare time, but unfortunately this is also net the case.
To conclude, it seems I can not assist you.
Best regards.

17 Jul 2012 Fa Fa

Hi Nikolay,
i need to create 511 binary Lookup table, for LBP feature and to work with adaboost with training images.
please can you help me?

28 May 2012 Iqbal Nouyed  
27 Aug 2012

Many thanks goes to Chris Forne for his sharp eye. Bugs fixed, and some modification introduced.

28 Aug 2012

A Helix/Snail indexing function was аdded to scan neighborhood pixels as spiral.

09 Jan 2014

- Support Circular filter, as in M. Pietikäinen papers.
- Works on multiple color channels (gray-scale, RGB, La*b and even spectral images).
- Support Rotation Invariant mode.
- Two LBP version- pixels wise, and efficient.

16 Jan 2014

Changed filter direction (to CCW), starting point (3 o'clock instead of 12), support pixels interpolation.

22 May 2014

The function “roundnS” was added to the submission aimed to replace the “roundn” function that is apparently part of the Matlab Mapping Toolbox, which may be missing from some of the users trying to run this code.

05 Sep 2014

Resolved the bug reported by Michael.

Contact us