Asked by C N N
on 13 Sep 2011

I am trying to execute local binary pattern in MATLAB using the image processing toolbox. When i execute I can't get a LBP image and LBP histogram.

clear all;

close all;

clc;

I=imread('test.png');

figure,imshow(I)

%%Crop

I2 = imcrop(I);

figure, imshow(I2)

w=size(I2,1);

h=size(I2,2);

%%LBP

scale = 2.^[7 6 5; 0 -inf 4; 1 2 3];

for i=2:w-1

for j=2:h-1

J0=I2(i,j);

I3(i-1,j-1)=I2(i-1,j-1)>J0;

I3(i-1,j)=I2(i-1,j)>J0;

I3(i-1,j+1)=I2(i-1,j+1)>J0;

I3(i,j+1)=I2(i,j+1)>J0;

I3(i+1,j+1)=I2(i+1,j+1)>J0;

I3(i+1,j)=I2(i+1,j)>J0;

I3(i+1,j-1)=I2(i+1,j-1)>J0;

I3(i,j-1)=I2(i,j-1)>J0;

LBP(i,j)=I3(i-1,j-1)*2^7+I3(i-1,j)*2^6+I3(i-1,j+1)*2^5+I3(i,j+1)*2^4+I3(i+1,j+1)*2^3+I3(i+1,j)*2^2+I3(i+1,j-1)*2^1+I3(i,j-1)*2^0;

end

end

figure,imshow(LBP)

figure,imhist(LBP)

what is the issue.i am supposed to get numbers from 0 to 255 but i am not getting it. how to correct it?

Answer by madhulika jain
on 22 Sep 2011

Answer by Izzo
on 3 Dec 2011

hey i think i got your problem, because i already did LBP. First, make sure that u have converted the image into grayscale form. Second, you have to replace the test image with the new LBP image, therefore: instead of this code

LBP(i,j)=I3(i-1,j-1)*2^7+I3(i-1,j)*2^6+I3(i-1,j+1)*2^5+I3(i,j+1)*2^4+I3(i+1,j+1)*2^3+I3(i+1,j)*2^2+I3(i+1,j-1)*2^1+I3(i,j-1)*2^0;

try to change into:

I2(i-1,j-1)=I3(i-1,j-1)*2^7+I3(i-1,j)*2^6+I3(i-1,j+1)*2^5+I3(i,j+1)*2^4+I3(i+1,j+1)*2^3+I3(i+1,j)*2^2+I3(i+1,j-1)*2^1+I3(i,j-1)*2^0;

because i have problem like that too, then my supervisor solve into this for me :P

Answer by Image Analyst
on 22 Sep 2011

Change the last few lines of your code to this:

subplot(2,2,3);

imshow(LBP, []);

subplot(2,2,4);

[pixelCounts, GLs] = imhist(uint8(LBP));

bar(GLs, pixelCounts);

Image Analyst
on 28 Oct 2012

I almost always use [] because it scales the image so you can see it. For uint8 or binary (logical) images, it will automatically scale to 0-255 or 0-1 respectively. However for single or double images, you won't see anything unless you use [] OR your image has values only between 0.0 and 1.0. If you had a uint8 image that went, say, only from 0-42, then it would appear dark, but it you did [] it would map linearly from 0-42 into 0-255 so you'd be able to see it better. This is what imagesc() does except without the ridiculous colormap that imagesc() applies by default. However if you're visually comparing it to other uint8 images, you might want to use [0 255] instead of [].

By the way, I've posted full LBP demos here in the Answers forum and newsgroup before - you might want to look for it.

Aquib
on 31 Oct 2012

Thank you sir i really appreciate you help

Azinuddin Aziz
on 29 Nov 2015

Sign in to comment.

Answer by remo
on 1 Dec 2011

hi CNN,

Sorry for being a Noob. I just cant understand why you use 'scale = 2.^[7 6 5; 0 -inf 4; 1 2 3]; ' in this program. can you explain?

C N N
on 18 Jan 2012

remo, u want to create a histogram is it? use a function called hist in MATLAB.

Muhammad Amirul Hafiz Sahful Bahri
on 29 Mar 2019

hi, I would like to know how did you get those number [ 7 6 5; 0 -inf 4; 1 2 3] ?

Did you calculate it?

Image Analyst
on 29 Mar 2019

Unhide the comments above and you'll see a desciption of how those are computed.

Sign in to comment.

Answer by anusha
on 24 Sep 2014

Edited by anusha
on 24 Sep 2014

u can try this code

clear all; close all; I=imread('cameraman.tif'); I=rgb2gray(I); I1=imcrop(I); [w h]=size(I1);

for i=2:w-1 for j=2:h-1 val=I1(i,j); scale=2.^[0 1 2;7 -inf 3;6 5 4]; mat=[I1(i-1,j-1) I1(i-1,j) I1(i-1,j+1);I1(i,j-1) I1(i,j) I1(i,j+1);I1(i+1,j-1) I1(i+1,j) I1(i+1,j+1)]; mat=mat>=val; fin=mat.*scale; I1(i,j)=sum(sum(fin)); end end imshow(I1,[]);

Sign in to comment.

Answer by Nikolay S.
on 16 Mar 2015

Sign in to comment.

Answer by Ktk
on 29 Oct 2017

the code is not giving me any values. can anyone help? thanks!

Image Analyst
on 30 Oct 2017

WHAT code?

Attached is my local binary pattern demo. Adapt as needed.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 12 Comments

## Ganesh (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_58164

## C N N (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_58165

## Ganesh (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_58220

## C N N (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_58393

## Ganesh (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_58596

## Ganesh (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_58599

## Ganesh (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_60589

## Walter Roberson (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_60698

## kalai selvi (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_531452

## kalai selvi (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_531453

## Image Analyst (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_667106

## sajjad nasiri (view profile)

## Direct link to this comment

https://se.mathworks.com/matlabcentral/answers/15774-local-binary-pattern-lbp#comment_709272

Sign in to comment.