title: Matlab code for Local binary pattern
body: <p>I am struggling to understand Matlab implementation of LBP algorithm found <a href="http://www.cse.oulu.fi/CMV/Downloads/LBPMatlab" rel="nofollow">here</a>. I am trying to find how it calculates the binaries for every pixel? It just calculates where the neighbor pixel are greater than the actual center pixel size. I want to calculate the binaries for every pixel in order to use local histograms to calculate the features of image. </p>
  
  <pre><code>[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 &lt; bsizex || ysize &lt; 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
  % the whole process here
  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) &lt; 1e-6) &amp;&amp; (abs(y - ry) &lt; 1e-6)
      % Interpolation is not needed, use original datatypes
      N = image(ry:ry+dy,rx:rx+dx);
      D = N &gt;= C; 
    else
    % Interpolation needed, use double type images 
    ty = y - fy;
    tx = x - fx;
  
    % Calculate the interpolation weights.
    w1 = roundn((1 - tx) * (1 - ty),-6);
    w2 = roundn(tx * (1 - ty),-6);
    w3 = roundn((1 - tx) * ty,-6) ;
    % w4 = roundn(tx * ty,-6) ;
    w4 = roundn(1 - w1 - w2 - w3, -6);
  
    % 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);
    N = roundn(N,-4);
    D = N &gt;= 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)&lt;=intmax('uint8'))
       result=uint8(result);
   elseif ((bins-1)&lt;=intmax('uint16'))
       result=uint16(result);
   else
       result=uint32(result);
   end
  end
   size(result)
  end
  </code></pre>
  
  <p>Iteratively it adds some value in results for all 8 neighbors of every pixel. But how it is correlated with LBP binaries? How is it correlate with the following code for the following c++ LBP approach:</p>
  
  <pre><code> uchar lbp(const Mat_&lt;uchar&gt; &amp; img, int x, int y)
   {
    // this is pretty much the same what you already got..
    uchar v = 0;
    uchar c = img(y,x);
    v += (img(y-1,x  ) &gt; c) &lt;&lt; 0;
    v += (img(y-1,x+1) &gt; c) &lt;&lt; 1;
    v += (img(y  ,x+1) &gt; c) &lt;&lt; 2;
    v += (img(y+1,x+1) &gt; c) &lt;&lt; 3;
    v += (img(y+1,x  ) &gt; c) &lt;&lt; 4;
    v += (img(y+1,x-1) &gt; c) &lt;&lt; 5;
    v += (img(y  ,x-1) &gt; c) &lt;&lt; 6;
    v += (img(y-1,x-1) &gt; c) &lt;&lt; 7;
    return v;
  </code></pre>
  
  <p>}</p>