转载:LBP代码详细注释

%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(‘test1.bmp‘);
% 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.% %% 检查参数的个数nargin,使其大于1小于5。如果不在此区间,就报错
error(nargchk(1,5,nargin));

image=varargin{1};% % %% 把第一个参数赋值给image
d_image=double(image);% % % 把图像从uint8转成double类型,以便以后计算

% % % 只有给出待处理的图像(一个参数)时,使用默认的设置。
% % % sp定义了中心点与它的近邻的相对位置
% % % neighbors定义近邻个数
% % % mapping定义的映射
% % % mode区别直方图的类型,‘h‘ or ‘hist‘是直方图,nh是规一化的直方图
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

% % % 给出两个参数,并且第二个参数(代表近邻半径)的长度为1时
% % % 只给出了近邻的半径,没给出近邻的个数,报错
if (nargin == 2) && (length(varargin{2}) == 1)
error(‘Input arguments‘);
end
% % % 如果给出两个以上的参数,并且第二个参数(代表近邻半径)的长度为1
% % % 半径设为第二个参数
% % % 近邻个数设为第三个参数
if (nargin > 2) && (length(varargin{2}) == 1)
radius=varargin{2};
neighbors=varargin{3};

spoints=zeros(neighbors,2);

% % % 把360度均匀分成neighbors分,以计算近邻点与中心的相对坐标
% Angle step.
a = 2*pi/neighbors;

% % % 计算坐标,每一维代表y,第二维代表x
% % % spoints的第i行代表第i个近邻
for i = 1:neighbors
spoints(i,1) = -radius*sin((i-1)*a);
spoints(i,2) = radius*cos((i-1)*a);
end
% % % 如果参数个数大于等于4,第四个参数赋值给映射mapping;否则,无映射。
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
% % % 如果参数个数大于1,并且第二个参数的长度大于1。则第二个参数给出近邻点与中心点的相对位置
if (nargin > 1) && (length(varargin{2}) > 1)
spoints=varargin{2};
neighbors=size(spoints,1);
% % % 如果还有第三个参数,把它赋值给映射mapping
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.图像的大小,第一维是y,第二维是x
[ysize xsize] = size(image);

% % % 确定block的左上和右下两个点
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
% % % block的大小
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
% % % 在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.
% % % 检查block和img的大小
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 这一段写得很漂亮!!!!
% % % 对于每一个neighbor,先使要比较的点与中心点对齐,然后利用D = N >= C比较它们的大小。
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
% % % 如果mapping已经存在,那么利用这个mapping.
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

生成mapping的函数

%GETMAPPING returns a structure containing a mapping table for LBP codes.
% MAPPING = GETMAPPING(SAMPLES,MAPPINGTYPE) returns a
% structure containing a mapping table for
% LBP codes in a neighbourhood of SAMPLES sampling
% points. Possible values for MAPPINGTYPE are
% ‘u2‘ for uniform LBP
% ‘ri‘ for rotation-invariant LBP
% ‘riu2‘ for uniform rotation-invariant LBP.
%
% Example:
% I=imread(‘rice.tif‘);
% MAPPING=getmapping(16,‘riu2‘);
% LBPHIST=lbp(I,2,16,MAPPING,‘hist‘);
% Now LBPHIST contains a rotation-invariant uniform LBP
% histogram in a (16,2) neighbourhood.
%

function mapping = getmapping(samples,mappingtype)
% Version 0.1.1
% Authors: Marko Heikkil?and Timo Ahonen

% Changelog
% 0.1.1 Changed output to be a structure
% Fixed a bug causing out of memory errors when generating rotation
% invariant mappings with high number of sampling points.
% Lauge Sorensen is acknowledged for spotting this problem.

table = 0:2^samples-1;
newMax = 0; %number of patterns in the resulting LBP code
index = 0;

if strcmp(mappingtype,‘u2‘) %Uniform 2
newMax = samples*(samples-1) + 3;
for i = 0:2^samples-1
% % % j是i循环左移的结果
j = bitset(bitshift(i,1,‘uint8‘),1,bitget(i,samples)); %rotate left
% % % 计算跳变的次数
numt = sum(bitget(bitxor(i,j),1:samples)); %number of 1->0 and
%0->1 transitions
%in binary string
%x is equal to the
%number of 1-bits in
%XOR(x,Rotate left(x))
% % % 如果跳变次数不大于2,那么新建一个标记index;否则放入最后一类
if numt <= 2
table(i+1) = index;
index = index + 1;
else
table(i+1) = newMax - 1;
end
end
end

if strcmp(mappingtype,‘ri‘) %Rotation invariant
tmpMap = zeros(2^samples,1) - 1;
for i = 0:2^samples-1
rm = i;
r = i;
% % % 计算所有rotate中最小的一个
for j = 1:samples-1
r = bitset(bitshift(r,1,‘uint8‘),1,bitget(r,samples)); %rotate
%left
if r < rm
rm = r;
end
end
% % % 同上
if tmpMap(rm+1) < 0
tmpMap(rm+1) = newMax;
newMax = newMax + 1;
end
table(i+1) = tmpMap(rm+1);
end
end

if strcmp(mappingtype,‘riu2‘) %Uniform & Rotation invariant
newMax = samples + 2;
for i = 0:2^samples - 1
j = bitset(bitshift(i,1,‘uint8‘),1,bitget(i,samples)); %rotate left
numt = sum(bitget(bitxor(i,j),1:samples));
if numt <= 2
table(i+1) = sum(bitget(i,1:samples));
else
table(i+1) = samples+1;
end
end
end

mapping.table=table;
mapping.samples=samples;
mapping.num=newMax;

主函数:

clear;clc;close all
I=imread(‘test1.bmp‘);
mapping=getmapping(8,‘u2‘);

H1=lbp(I,1,8,mapping,‘a‘);%%%如果想以图像形式显示lbp特征,第五个参数随便设一个值,但不能不设。H1=lbp(I,1,8,mapping,1);
figure;imshow(H1)

H1=lbp(I,1,8,mapping,‘h‘); %LBP histogram in (8,1) neighborhood
%using uniform patterns
figure
subplot(2,1,1),stem(H1);
H2=lbp(I);
subplot(2,1,2),stem(H2);

时间: 2024-10-07 00:38:17

转载:LBP代码详细注释的相关文章

ABP+Zero+Metronic+Redis的完美结合快速启动模板(超级代码详细注释版本)

微信扫一扫并支付成功,联系QQ:770628656获取所有源码(超级代码详细注释版本) 原文地址:https://www.cnblogs.com/abpbasic/p/8124792.html

SAP CRM BOL编程基础,代码+详细注释

网络上可以找到一些使用BOL查询.维护数据的DEMO,但几乎都是单纯的代码,缺乏说明,难以理解.本文除了代码外,还给出了详细的注释,有助于理解BOL编程中的一些基本概念. 这是一篇翻译的文章,你可能会发现部分内容不是很好理解,这时可以直接阅读原文. 原文所在的sapcrmwebui.com是一个不错的博客,然而网站不是很稳定,偶尔会连接不上,建议使用Internet Archive访问. 如果你访问不了Internet Archive,说明你需要一点过墙的手段. 本文链接:http://www.

POJ 1035 代码+详细注释

Spell checker Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 19319 Accepted: 7060 Description You, as a member of a development team for a new spell checking program, are to write a module that will check the correctness of given words us

linux 守护进程(daemon process)代码-详细注释

1.进程组 组长不能创建新的 会话 2.会话首领可以重新打开控制终端 1 #include <stdio.h> 2 #include <unistd.h> 3 #include <stdlib.h> 4 #include <sys/param.h> 5 #include <sys/stat.h> 6 #include <sys/types.h> 7 #include <fcntl.h> 8 #include <sig

压缩跟踪(CT)代码详细学习_模块1(样本的采集和扩充)

本章主要详解的是compressive tracking框架中的第一部分:样本的采集和扩充部分. 在开始代码学习的前面,你需要知道的理论知识参见论文:Real-time Compressive Tracking.理论理解可以参见我的博客:http://blog.csdn.net/ikerpeng/article/details/19826409 . 这个模块中你需要知道一个基本的概念:代码里面几个变量指的是什么.上一张图: 也许你现在还不知道他们是什么,直接贴代码了.相信有我的注释你一定会懂的.

Qt5_简易画板_详细注释

代码下载链接:  http://pan.baidu.com/s/1hsc41Ek 密码: 5hdg 显示效果如下: 代码附有详细注释(代码如下) 1 /*** 2 * 先新建QMainWindow, 项目名称: DrawWidget 基类选择: QMainWindow, 3 * 类名默认, 然后在DrawWidget项目名上新建c++class文件, 选择基类: QWidget 4 */ 5 //先完成绘图区的实现 6 //如下为: drawwidget.h 7 #ifndef DRAWWIDG

codevs 2924 数独挑战 x(三种做法+超详细注释~)

2924 数独挑战 时间限制: 1 s 空间限制: 1000 KB 题目等级 : 钻石 Diamond 题目描述 Description “芬兰数学家因卡拉,花费3个月时间设计出了世界上迄今难度最大的数独游戏,而且它只有一个答案.因卡拉说只有思考能力最快.头脑最聪明的人才能破解这个游戏.”这是英国<每日邮报>2012年6月30日的一篇报道.这个号称“世界最难数独”的“超级游戏”,却被扬州一位69岁的农民花三天时间解了出来. 看到这个新闻后,我激动不已,证明我们OI的实力的机会来了,我们虽然不是

【甘道夫】官网MapReduce实例代码详细批注

引言 1.本文不描述MapReduce入门知识,这类知识网上很多,请自行查阅 2.本文的实例代码来自官网 http://hadoop.apache.org/docs/current/hadoop-mapreduce-client/hadoop-mapreduce-client-core/MapReduceTutorial.html 最后的WordCount v2.0,该代码相比源码中的org.apache.hadoop.examples.WordCount要复杂和完整,更适合作为MapReduc

多线程实现生产者消费者问题 详细注释 事件+临界区 信号量+临界区2种方法

生产者消费者问题:  该问题描述了两个共享固定大小缓冲区的线程--即所谓的"生产者"和"消费者"--在实际运行时会发生的问题.生产者的主要作用是生成一定量的数据放到缓冲区中,然后重复此过程.与此同时,消费者也在缓冲区消耗这些数据.该问题的关键就是要保证生产者不会在缓冲区满时加入数据,消费者也不会在缓冲区中空时消耗数据.具体我就不解释了   应该都懂 不懂请百度一下 我是用事件实现生产者消费者问题的同步  用临界区实现互斥    我的这个做法与经典做法不同 即用信号量