基于局部统一模式LBP及MB-LBP的特征提取

来源:互联网 发布:mac的邮箱怎么设置 编辑:程序博客网 时间:2024/05/18 22:41

参考:http://blog.csdn.net/u014568921/article/details/45788523

1、IsUniform()

##检查bit是否为统一化模式(0、1转换不多于2个),把统一化模式的放在直方图收集箱bin,其他放置于公用收集箱。
function bUni = IsUniform(bits)  % 判断某一个位串模式 bits 是否是 uniform 模式  %  % 输入:bits --- 二进制LBP模式串  %  % 返回值:bUni --- =1,if bits 是uniform模式串;=2,if bits 不是uniform模式串  n = length(bits);  nJmp = 0; % 位跳变数(0->1 or 1->0for ii = 1 : (n-1)      if( bits(ii) ~= bits(ii+1) )          nJmp = nJmp+1;      end  end  if bits(n) ~= bits(1)      nJmp = nJmp+1;  end  if nJmp > 2      bUni = false;  else      bUni = true;  end  

2、makeLBPMap()

获取映射表vecLBPMap,将灰度gray落入第vecLBPMap(gray+1)号收集箱。

function vecLBPMap = makeLBPMap  % 生成(8,2)临域uniform LBP直方图的映射关系,即将256个灰度值映射到59个收集箱中,  % 所有的非 uniform 放入一个收集箱中  vecLBPMap = zeros(1, 256); %初始化映射表  bits = zeros(1, 8); %8位二进模式串  nCurBin = 1;  for ii = 0:255      num = ii;      nCnt = 0;      % 获得灰度num的二进制表示bits      while (num)          bits(8-nCnt) = mod(num, 2);          num = floor( num / 2 );          nCnt = nCnt + 1;      end      if IsUniform(bits) % 判断bits是不是uniform模式          vecLBPMap(ii+1) = nCurBin;% 每个uniform模式分配一个收集箱          nCurBin = nCurBin + 1;      else          vecLBPMap(ii+1) = 59;%所有非uniform模式都放入第59号收集箱              end  end  % 保存映射表  save('MatLBPMap.mat', 'vecLBPMap');  

3、getLBPFea()

获得LBP统计直方图特征

%getLBPFea.m  function [histLBP, MatLBP] = getLBPFea(I)  % 计算分区图像 I 的LBP特征,(8,2),uniform  %  % 输入:I --- 分区图像  %  % 返回值: MatLBP --- LBP响应矩阵  %               histLBP --- 1维行向量,LBP直方图  % 获得分块图像I的大小  [m n] = size(I);  rad = 2;  if (m <= 2*rad) || (n <= 2*rad)      error('I is too small to compute LBP feature!');  end  MatLBP = zeros(m-2*rad, n-2*rad);  % 读入 LBP 映射(像素灰度与直方图收集箱索引的映射)  load MatLBPMap.mat;  for ii = 1+rad : m-rad      for jj = 1+rad : n-rad          nCnt = 1;          % 计算(8,2)邻域的像素值,不在像素中心的点通过双线性插值获得其值          nbPT(nCnt) = I(ii, jj-rad);          nCnt = nCnt + 1;          horInterp1 = I(ii-2, jj-2) + 0.5858*( I(ii-2, jj-1) - I(ii-2, jj-2) ); % 水平方向插值          horInterp2 = I(ii-1, jj-2) + 0.5858*( I(ii-1, jj-1) - I(ii-1, jj-2) ); % 水平方向插值          verInterp = horInterp1 + 0.5858*( horInterp2 - horInterp1 ); % 竖直方向插值          nbPT(nCnt) = verInterp;          nCnt = nCnt + 1;          nbPT(nCnt) = I(ii-2, jj);          nCnt = nCnt + 1;          horInterp1 = I(ii-2, jj+1) + 0.4142*( I(ii-2, jj+2) - I(ii-2, jj+1) );          horInterp2 = I(ii-1, jj+1) + 0.4142*( I(ii-1, jj+2) - I(ii-1, jj+1) );          verInterp = horInterp1 + 0.5858*( horInterp2 - horInterp1 );          nbPT(nCnt) = verInterp;          nCnt = nCnt + 1;          nbPT(nCnt) = I(ii, jj+2);          nCnt = nCnt + 1;          horInterp1 = I(ii+1, jj+1) + 0.4142*( I(ii+1, jj+2) - I(ii+1, jj+1) );          horInterp2 = I(ii+2, jj+1) + 0.4142*( I(ii+2, jj+2) - I(ii+2, jj+1) );          verInterp = horInterp1 + 0.4142*( horInterp2 - horInterp1 );          nbPT(nCnt) = verInterp;          nCnt = nCnt + 1;          nbPT(nCnt) = I(ii+2, jj);          nCnt = nCnt + 1;          horInterp1 = I(ii+1, jj-2) + 0.5858*( I(ii+1, jj-1) - I(ii+1, jj-2) );          horInterp2 = I(ii+2, jj-2) + 0.5858*( I(ii+2, jj-1) - I(ii+2, jj-1) );          verInterp = horInterp1 + 0.4142*( horInterp2 - horInterp1 );          nbPT(nCnt) = verInterp;          for iCnt = 1:nCnt              if( nbPT(iCnt) >= I(ii, jj) )                  MatLBP(ii-rad, jj-rad) = MatLBP(ii-rad, jj-rad) + 2^(nCnt-iCnt);              end          end      end  end  % 计算LBP直方图  histLBP = zeros(1, 59); % 对于(8,2)的uniform直方图共有59个收集箱  for ii = 1:m-2*rad      for jj = 1:n-2*rad          histLBP( vecLBPMap( MatLBP(ii, jj)+1 ) ) = histLBP( vecLBPMap( MatLBP(ii, jj)+1 ) ) + 1;      end  end  

4特征显示

[hist,I_LBP]=getMBLBPFea(I);imshow(I_LBP,[]);