%LBP returns the local binary pattern image or LBP histogram of an image.
% J = LBP(I,R,N,MAPPING,MODE) returns either a local binary pattern
% coded image or the local binary pattern histogram of an intensity
% image I. The LBP codes are computed using N sampling points on a
% circle of radius R and using mapping table defined by MAPPING.
% See the getmapping function for different mappings and use 0 for
% no mapping. Possible values for MODE are
% 'h' or 'hist' to get a histogram of LBP codes
% 'nh' to get a normalized histogram
% Otherwise an LBP code image is returned.
%
% J = LBP(I) returns the original (basic) LBP histogram of image I
%
% J = LBP(I,SP,MAPPING,MODE) computes the LBP codes using n sampling
% points defined in (n * 2) matrix SP. The sampling points should be
% defined around the origin (coordinates (0,0)).
%
% Examples
% --------
% I=imread('rice.png');
% mapping=getmapping(8,'u2');
% H1=LBP(I,1,8,mapping,'h'); %LBP histogram in (8,1) neighborhood
% %using uniform patterns
% subplot(2,1,1),stem(H1);
%
% H2=LBP(I);
% subplot(2,1,2),stem(H2);
%
% SP=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];
% I2=LBP(I,SP,0,'i'); %LBP code image using sampling points in SP
% %and no mapping. Now H2 is equal to histogram
% %of I2.
function result = lbp(varargin) % image,radius,neighbors,mapping,mode)
% Version 0.3.2
% Authors: Marko Heikkil?and Timo Ahonen
% Changelog
% Version 0.3.2: A bug fix to enable using mappings together with a
% predefined spoints array
% Version 0.3.1: Changed MAPPING input to be a struct containing the mapping
% table and the number of bins to make the function run faster with high number
% of sampling points. Lauge Sorensen is acknowledged for spotting this problem.
% Check number of input arguments.
narginchk(1,5);%输入个数
image=varargin{1};
d_image=double(image);
%==========================================||function result = lbp(varargin) % image
if nargin==1 %设第一个横坐标,第二个纵坐标 % 3 5 8
spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1]; % 2 7
neighbors=8; % 1 4 6
mapping=0;
mode='i';
end
%==========================================||function result = lbp(varargin) % image,不是sp
if (nargin == 2) && (length(varargin{2}) == 1)
error('Input arguments');
end
%===============第二个数据是半径 半径不可以为1
%==========================================||function result = lbp(varargin) % image,radius,neighbors
if (nargin > 2) && (length(varargin{2}) == 1)
radius=varargin{2}; %第二个数据是半径
neighbors=varargin{3}; %第三个数据是采样点
spoints=zeros(neighbors,2); %0矩阵
%=========if(neighbors=4)spoints=[0 0;0 0;0 0;0 0;]
% Angle step.
%=========角度设置===角度等分===一份的弧度是a
a = 2*pi/neighbors;
for i = 1:neighbors %===扫描
spoints(i,1) = -radius*sin((i-1)*a); %===采样点纵坐标 Yp=Yc(中心纵坐标)-R*sin(对应弧度)
spoints(i,2) = radius*cos((i-1)*a); %===采样点横坐标 Xp=Xc(中心纵坐标)+R*cos(对应弧度)
end %====从水平右方顺时针扫描
%================上面是没有输入sp==================================
%=========================================||function result = lbp(varargin) % image,radius,neighbors,mapping,mode)
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
%==============上面完整格式输入如右所示||function result = lbp(varargin) % image,radius,neighbors,mapping,mode)
%==============下面是有sp输入==================================
%=========================================||function result = lbp(varargin) % image,sp
if (nargin > 1) && (length(varargin{2}) > 1)
spoints=varargin{2};
neighbors=size(spoints,1);
if(nargin >= 3) %====||function result = lbp(varargin) % image,sp,mapping
mapping=varargin{3};
if(isstruct(mapping) && mapping.samples ~= neighbors)
error('Incompatible mapping');
end
else
mapping=0;
end
if(nargin >= 4) %====||function result = lbp(varargin) % image,sp,mapping,mode
mode=varargin{4};
else
mode='h';
end
end
%第一种完整||function result = lbp(varargin) % image,radius,neighbors,mapping,mode)
%第二种完整||function result = lbp(varargin) % image,sp,mapping,mode
%===============上面都在判断输入数据是否合格=======
% Determine the dimensions 规模 of the input image.
[ysize, xsize] = size(image);
%取长宽
miny=min(spoints(:,1));
%扫描相对方位的y最小
maxy=max(spoints(:,1));
%扫描相对方位的y最大
minx=min(spoints(:,2));
%扫描相对方位的x最小
maxx=max(spoints(:,2));
%扫描相对方位的x最大
% Block size, each LBP code is computed within a block of size bsizey*bsizex
bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
% 最大的向上圆整(YP最大和0比最大)-最小的向下圆整(YP最小和0比最小)->这边取出来的是相对距离>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));
%确` 定开头??(在块中的坐标原点)????2?2?
% 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
%最小输入尺寸测试 输入照片要比LBP范围大
% 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;
%LBP最大位数
% Initialize the result matrix with zeros.
result=zeros(dy+1,dx+1);
%创建一个和中心像素集等大的矩阵
%Compute the LBP code image
for i = 1:neighbors %neighbor=8->循环8次
y = spoints(i,1)+origy;
x = spoints(i,2)+origx;
%这边的X和Y是Xp和Yp->是所有的Xp和Yp
% Calculate floors, ceils and rounds for the x and y.
fy = floor(y); cy = ceil(y); ry = round(y);
%floor向下圆整 ceil向上圆整 round最近圆整
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)%1e-6->1*10的-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
% % result是十进制值
% ==============================================================
% 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
%LBP模式选择——————