Attachment 'lbp.m'

Download

   1 %LBP returns the local binary pattern image or LBP histogram of an image.
   2 %  J = LBP(I,R,N,MAPPING,MODE) returns either a local binary pattern
   3 %  coded image or the local binary pattern histogram of an intensity
   4 %  image I. The LBP codes are computed using N sampling points on a 
   5 %  circle of radius R and using mapping table defined by MAPPING. 
   6 %  See the getmapping function for different mappings and use 0 for
   7 %  no mapping. Possible values for MODE are
   8 %       'h' or 'hist'  to get a histogram of LBP codes
   9 %       'nh'           to get a normalized histogram
  10 %  Otherwise an LBP code image is returned.
  11 %
  12 %  J = LBP(I) returns the original (basic) LBP histogram of image I
  13 %
  14 %  J = LBP(I,SP,MAPPING,MODE) computes the LBP codes using n sampling
  15 %  points defined in (n * 2) matrix SP. The sampling points should be
  16 %  defined around the origin (coordinates (0,0)).
  17 %
  18 %  Examples
  19 %  --------
  20 %       I=imread('rice.png');
  21 %       mapping=getmapping(8,'u2'); 
  22 %       H1=LBP(I,1,8,mapping,'h'); %LBP histogram in (8,1) neighborhood
  23 %                                  %using uniform patterns
  24 %       subplot(2,1,1),stem(H1);
  25 %
  26 %       H2=LBP(I);
  27 %       subplot(2,1,2),stem(H2);
  28 %
  29 %       SP=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];
  30 %       I2=LBP(I,SP,0,'i'); %LBP code image using sampling points in SP
  31 %                           %and no mapping. Now H2 is equal to histogram
  32 %                           %of I2.
  33 
  34 function result = lbp(varargin) % image,radius,neighbors,mapping,mode)
  35 % Version 0.3.3
  36 % Authors: Marko Heikkil?and Timo Ahonen
  37 
  38 % Changelog
  39 % Version 0.3.2: A bug fix to enable using mappings together with a
  40 % predefined spoints array
  41 % Version 0.3.1: Changed MAPPING input to be a struct containing the mapping
  42 % table and the number of bins to make the function run faster with high number
  43 % of sampling points. Lauge Sorensen is acknowledged for spotting this problem.
  44 
  45 
  46 % Check number of input arguments.
  47 error(nargchk(1,5,nargin));
  48 
  49 image=varargin{1};
  50 d_image=double(image);
  51 
  52 if nargin==1
  53     spoints=[-1 -1; -1 0; -1 1; 0 -1; -0 1; 1 -1; 1 0; 1 1];
  54     neighbors=8;
  55     mapping=0;
  56     mode='h';
  57 end
  58 
  59 if (nargin == 2) && (length(varargin{2}) == 1)
  60     error('Input arguments');
  61 end
  62 
  63 if (nargin > 2) && (length(varargin{2}) == 1)
  64     radius=varargin{2};
  65     neighbors=varargin{3};
  66     
  67     spoints=zeros(neighbors,2);
  68 
  69     % Angle step.
  70     a = 2*pi/neighbors;
  71     
  72     for i = 1:neighbors
  73         spoints(i,1) = -radius*sin((i-1)*a);
  74         spoints(i,2) = radius*cos((i-1)*a);
  75     end
  76     
  77     if(nargin >= 4)
  78         mapping=varargin{4};
  79         if(isstruct(mapping) && mapping.samples ~= neighbors)
  80             error('Incompatible mapping');
  81         end
  82     else
  83         mapping=0;
  84     end
  85     
  86     if(nargin >= 5)
  87         mode=varargin{5};
  88     else
  89         mode='h';
  90     end
  91 end
  92 
  93 if (nargin > 1) && (length(varargin{2}) > 1)
  94     spoints=varargin{2};
  95     neighbors=size(spoints,1);
  96     
  97     if(nargin >= 3)
  98         mapping=varargin{3};
  99         if(isstruct(mapping) && mapping.samples ~= neighbors)
 100             error('Incompatible mapping');
 101         end
 102     else
 103         mapping=0;
 104     end
 105     
 106     if(nargin >= 4)
 107         mode=varargin{4};
 108     else
 109         mode='h';
 110     end   
 111 end
 112 
 113 % Determine the dimensions of the input image.
 114 [ysize xsize] = size(image);
 115 
 116 
 117 
 118 miny=min(spoints(:,1));
 119 maxy=max(spoints(:,1));
 120 minx=min(spoints(:,2));
 121 maxx=max(spoints(:,2));
 122 
 123 % Block size, each LBP code is computed within a block of size bsizey*bsizex
 124 bsizey=ceil(max(maxy,0))-floor(min(miny,0))+1;
 125 bsizex=ceil(max(maxx,0))-floor(min(minx,0))+1;
 126 
 127 % Coordinates of origin (0,0) in the block
 128 origy=1-floor(min(miny,0));
 129 origx=1-floor(min(minx,0));
 130 
 131 % Minimum allowed size for the input image depends
 132 % on the radius of the used LBP operator.
 133 if(xsize < bsizex || ysize < bsizey)
 134   error('Too small input image. Should be at least (2*radius+1) x (2*radius+1)');
 135 end
 136 
 137 % Calculate dx and dy;
 138 dx = xsize - bsizex;
 139 dy = ysize - bsizey;
 140 
 141 % Fill the center pixel matrix C.
 142 C = image(origy:origy+dy,origx:origx+dx);
 143 d_C = double(C);
 144 
 145 bins = 2^neighbors;
 146 
 147 % Initialize the result matrix with zeros.
 148 result=zeros(dy+1,dx+1);
 149 
 150 %Compute the LBP code image
 151 
 152 for i = 1:neighbors
 153   y = spoints(i,1)+origy;
 154   x = spoints(i,2)+origx;
 155   % Calculate floors, ceils and rounds for the x and y.
 156   fy = floor(y); cy = ceil(y); ry = round(y);
 157   fx = floor(x); cx = ceil(x); rx = round(x);
 158   % Check if interpolation is needed.
 159   if (abs(x - rx) < 1e-6) && (abs(y - ry) < 1e-6)
 160     % Interpolation is not needed, use original datatypes
 161     N = image(ry:ry+dy,rx:rx+dx);
 162     D = N >= C; 
 163   else
 164     % Interpolation needed, use double type images 
 165     ty = y - fy;
 166     tx = x - fx;
 167 
 168     % Calculate the interpolation weights.
 169     w1 = roundn((1 - tx) * (1 - ty),-6);
 170     w2 = roundn(tx * (1 - ty),-6);
 171     w3 = roundn((1 - tx) * ty,-6) ;
 172     % w4 = roundn(tx * ty,-6) ;
 173     w4 = roundn(1 - w1 - w2 - w3, -6);
 174             
 175     % Compute interpolated pixel values
 176     N = w1*d_image(fy:fy+dy,fx:fx+dx) + w2*d_image(fy:fy+dy,cx:cx+dx) + ...
 177 w3*d_image(cy:cy+dy,fx:fx+dx) + w4*d_image(cy:cy+dy,cx:cx+dx);
 178     N = roundn(N,-4);
 179     D = N >= d_C; 
 180   end  
 181   % Update the result matrix.
 182   v = 2^(i-1);
 183   result = result + v*D;
 184 end
 185 
 186 %Apply mapping if it is defined
 187 if isstruct(mapping)
 188     bins = mapping.num;
 189     for i = 1:size(result,1)
 190         for j = 1:size(result,2)
 191             result(i,j) = mapping.table(result(i,j)+1);
 192         end
 193     end
 194 end
 195 
 196 if (strcmp(mode,'h') || strcmp(mode,'hist') || strcmp(mode,'nh'))
 197     % Return with LBP histogram if mode equals 'hist'.
 198     result=hist(result(:),0:(bins-1));
 199     if (strcmp(mode,'nh'))
 200         result=result/sum(result);
 201     end
 202 else
 203     %Otherwise return a matrix of unsigned integers
 204     if ((bins-1)<=intmax('uint8'))
 205         result=uint8(result);
 206     elseif ((bins-1)<=intmax('uint16'))
 207         result=uint16(result);
 208     else
 209         result=uint32(result);
 210     end
 211 end
 212 
 213 end
 214 
 215 function x = roundn(x, n)
 216 
 217 error(nargchk(2, 2, nargin, 'struct'))
 218 validateattributes(x, {'single', 'double'}, {}, 'ROUNDN', 'X')
 219 validateattributes(n, ...
 220     {'numeric'}, {'scalar', 'real', 'integer'}, 'ROUNDN', 'N')
 221 
 222 if n < 0
 223     p = 10 ^ -n;
 224     x = round(p * x) / p;
 225 elseif n > 0
 226     p = 10 ^ n;
 227     x = p * round(x / p);
 228 else
 229     x = round(x);
 230 end
 231 
 232 end

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view | diff] (2013-11-22 16:01:40, 6.9 KB) [[attachment:LBPHF_S_M.zip]]
  • [get | view | diff] (2013-11-22 16:01:42, 3.1 KB) [[attachment:LCP.zip (2012-11-15)]]
  • [get | view | diff] (2013-11-22 16:01:46, 560.5 KB) [[attachment:STLBP_Matlab.rar]]
  • [get | view | diff] (2013-11-22 16:01:51, 643.2 KB) [[attachment:STLBP_Matlab.zip]]
  • [get | view | diff] (2013-11-22 16:02:06, 1776.9 KB) [[attachment:STLBP_VC.zip]]
  • [get | view | diff] (2013-11-22 16:02:07, 2.2 KB) [[attachment:TIM_Lipreading_CVPR2011.zip]]
  • [get | view | diff] (2013-11-22 16:02:07, 1.0 KB) [[attachment:constructhf.m]]
  • [get | view | diff] (2013-11-22 16:02:08, 4.3 KB) [[attachment:cont.m]]
  • [get | view | diff] (2013-11-22 16:02:11, 11.2 KB) [[attachment:dis(S+M).zip]]
  • [get | view | diff] (2013-11-22 16:02:11, 11.2 KB) [[attachment:dis.zip]]
  • [get | view | diff] (2013-11-22 16:02:11, 11.2 KB) [[attachment:disCLBP.zip]]
  • [get | view | diff] (2015-03-09 09:48:06, 2.8 KB) [[attachment:getmaplbphf.m]]
  • [get | view | diff] (2015-03-09 09:48:24, 5.3 KB) [[attachment:getmapping.m]]
  • [get | view | diff] (2015-03-09 09:48:40, 6.4 KB) [[attachment:lbp.m]]
 All files | Selected Files: delete move to page
Normal Sort Sort + uniq Sort + uniq + count

You are not allowed to attach a file to this page.