File: lbpfilter_mex.c

package info (click to toggle)
libocas 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze, wheezy
  • size: 4,392 kB
  • ctags: 298
  • sloc: ansic: 6,290; makefile: 76
file content (78 lines) | stat: -rw-r--r-- 2,430 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*=================================================================
 * LBPFILTER computes LBP for each 3x3 subwindow in input image.
 *
 * Synopsis:
 *  F = lbpfilter(I)
 * where
 *  I [h x w (double)] is input image.
 *  F [h x w (double)] is image of LBP responses;
 *   the border colums and rows, for which LBP is not defined, are set 0.
 *
 *=================================================================*/

#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <mex.h>
#include <time.h>
#include <errno.h>

#define MIN(A,B) ((A) > (B) ? (B) : (A))
#define MAX(A,B) ((A) < (B) ? (B) : (A))
#define ABS(A) ((A) < 0 ? -(A) : (A))
#define INDEX(ROW,COL,NUM_ROWS) ((COL)*(NUM_ROWS)+(ROW))


/*======================================================================
  Main code plus interface to Matlab.
========================================================================*/

void mexFunction( int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[] )
{

  double *I, *P;
  int w,h, x,y;
  double center;
  unsigned char pattern;

  if( nrhs != 1 )
    mexErrMsgTxt("One input argument required.\n\n"
                 "LBPFILTER computes LBP for each 3x3 subwindow in input image.\n"
                 "\n"
                 "Synopsis:\n"
                 "  F = lbpfilter(I)\n"
                 "where\n"
                 "  I [h x w (double)] is input image.\n"
                 "  F [h x w (double)] is image of LBP responses; \n"
                 "    the border colums and rows, for which LBP is not defined, are set 0.\n"
                 );

  I = (double*)mxGetPr(prhs[0]);
  h = mxGetM(prhs[0]);
  w = mxGetN(prhs[0]);

  plhs[0] = mxCreateDoubleMatrix(h,w,mxREAL);
  P = (double*)mxGetPr(plhs[0]);

  for(x=1; x < w-1; x++)
  {
    for(y=1; y< h-1; y++)
    {
      pattern = 0;
      center = I[INDEX(y,x,h)];
      if(I[INDEX(y-1,x-1,h)] < center) pattern = pattern | 0x01;
      if(I[INDEX(y-1,x,h)] < center)   pattern = pattern | 0x02;
      if(I[INDEX(y-1,x+1,h)] < center) pattern = pattern | 0x04;
      if(I[INDEX(y,x-1,h)] < center)   pattern = pattern | 0x08;
      if(I[INDEX(y,x+1,h)] < center)   pattern = pattern | 0x10;
      if(I[INDEX(y+1,x-1,h)] < center) pattern = pattern | 0x20;
      if(I[INDEX(y+1,x,h)] < center)   pattern = pattern | 0x40;
      if(I[INDEX(y+1,x+1,h)] < center) pattern = pattern | 0x80;

      P[INDEX(y,x,h)] = (double)pattern; 
    }
  }
  
  return;
}