LBP image detection

2 views (last 30 days)
feras
feras on 7 May 2012
Answered: meng wang on 23 Feb 2020
Hi all , i am new in matlab, i got this function to calculate lbp value i think, but i don't know what to do next, how to compare with other saved images, should i save with the database saved images the value of it's lbp then i compare with the new image, or should i re-calculate the lbp value again for all saved images. Note: the value returned as uint8 matrix i think. any help please in this
function result = lbp(varargin)
% Check number of input arguments.
error(nargchk(1,5,nargin));
image=varargin{1};
d_image=double(image);
if nargin==1
spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];
neighbors=8;
mapping=0;
mode='h';
end
if (nargin == 2) && (length(varargin{2}) == 1)
error('Input arguments');
end
if (nargin > 2) && (length(varargin{2}) == 1)
radius=varargin{2};
neighbors=varargin{3};
spoints=zeros(neighbors,2);
% Angle step.
a = 2*pi/neighbors;
for i = 1:neighbors
spoints(i,1) = -radius*sin((i-1)*a);
spoints(i,2) = radius*cos((i-1)*a);
end
if(nargin >= 4)
mapping=varargin{4};
if(isstruct(mapping) && mapping.samples ~= neighbors)
error('Incompatible mapping');
end
else
mapping=0;
end
if(nargin >= 5)
mode=varargin{5};
else
mode='h';
end
end
if (nargin > 1) && (length(varargin{2}) > 1)
spoints=varargin{2};
neighbors=size(spoints,1);
if(nargin >= 3)
mapping=varargin{3};
if(isstruct(mapping) && mapping.samples ~= neighbors)
error('Incompatible mapping');
end
else
mapping=0;
end
if(nargin >= 4)
mode=varargin{4};
else
mode='h';
end
end
% Determine the dimensions of the input image.
[ysize xsize] = size(image);
miny=min(spoints(:,1));
maxy=max(spoints(:,1));
minx=min(spoints(:,2));
maxx=max(spoints(:,2));
% Block size, each LBP code is computed within a block of size bsizey*bsizex
bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;
% Coordinates of origin (0,0) in the block
origy=1-floor(min(miny,0));
origx=1-floor(min(minx,0));
% Minimum allowed size for the input image depends
% on the radius of the used LBP operator.
if(xsize < bsizex || ysize < bsizey)
error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)');
end
% Calculate dx and dy;
dx = xsize - bsizex;
dy = ysize - bsizey;
% Fill the center pixel matrix C.
C = image(origy:origy+dy,origx:origx+dx);
d_C = double(C);
bins = 2^neighbors;
% Initialize the result matrix with zeros.
result=zeros(dy+1,dx+1);
%Compute the LBP code image
for i = 1:neighbors
y = spoints(i,1)+origy;
x = spoints(i,2)+origx;
% Calculate floors, ceils and rounds for the x and y.
fy = floor(y); cy = ceil(y); ry = round(y);
fx = floor(x); cx = ceil(x); rx = round(x);
% Check if interpolation is needed.
if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)
% Interpolation is not needed, use original datatypes
N = image(ry:ry+dy,rx:rx+dx);
D = N >= C;
else
% Interpolation needed, use double type images
ty = y - fy;
tx = x - fx;
% Calculate the interpolation weights.
w1 = (1 - tx) * (1 - ty);
w2 = tx * (1 - ty);
w3 = (1 - tx) * ty ;
w4 = tx * ty ;
% Compute interpolated pixel values
N = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...
w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);
D = N >= d_C;
end
% Update the result matrix.
v = 2^(i-1);
result = result + v*D;
end
%Apply mapping if it is defined
if isstruct(mapping)
bins = mapping.num;
for i = 1:size(result,1)
for j = 1:size(result,2)
result(i,j) = mapping.table(result(i,j)+1);
end
end
end
if (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh'))
% Return with LBP histogram if mode equals 'hist'.
result=hist(result(:),0:(bins-1));
if (strcmp(mode,'nh'))
result=result/sum(result);
end
else
%Otherwise return a matrix of unsigned integers
if ((bins-1)<=intmax('uint8'))
result=uint8(result);
elseif ((bins-1)<=intmax('uint16'))
result=uint16(result);
else
result=uint32(result);
end
end
end

Answers (1)

meng wang
meng wang on 23 Feb 2020
I'm doing the circular LBP feature,but i have some questions,The result is always white,here's my code,please help me,thanks:
clear;
% 声明默认neighbors为8
I = imread('lena.png');
[rows,cols] = size(I);
neighbors = 8;
radius = 3;
Ibpcode = 0;
CircleLBP = zeros(size(I), 'uint8');
for i = radius+1 : rows-radius
for j = radius+1 : cols-radius
%获得中心像素点的灰度值
centerPixel = I(i, j);
for k = 1 : neighbors
%根据公式计算第k个采样点的坐标
x = i + radius * cos(2.*pi.*k./neighbors);
y = j - radius * sin(2.*pi.*k./neighbors);
%根据取整结果进行双线性插值,得到第k个采样点的灰度值
%1 分别对x,y进行上下取整
x1 = floor(x); %向下取整
x2 = ceil(x); %向上取整
y1 = floor(y); %向下取整
y2 = ceil(y); %向上取整
%2 将坐标映射到0-1之间
tx = x - x1;
ty = y - y1;
%根据0-1之间的x,y的权重计算公式计算权重
w1 = (1 - tx)*(1 - ty);
w2 = tx*(1-ty);
w3 = (1-tx)*ty;
w4 = tx*ty;
%3.根据双线性插值公式计算第k个采样点的灰度值
neighbor = I(x1,y1)*w1 + I(x1,y2)*w2 + I(x2,y1)*w3 + I(x2,y2)*w4;
if(neighbor > centerPixel)
Ibpcode = 2^(neighbors - k -1) + Ibpcode;
end
end
CircleLBP(i,j) = Ibpcode;
end
end
imshow(CircleLBP);
clear;

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!