Advertisement
Guest User

Untitled

a guest
Sep 23rd, 2011
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.88 KB | None | 0 0
  1. #include <math.h>
  2. #include <stdio.h>
  3. #include "mex.h"
  4.  
  5. // unit vectors used to compute gradient orientation
  6. double uu[9] = {1.0000,
  7.         0.9397,
  8.         0.7660,
  9.         0.500,
  10.         0.1736,
  11.         -0.1736,
  12.         -0.5000,
  13.         -0.7660,
  14.         -0.9397};
  15. double vv[9] = {0.0000,
  16.         0.3420,
  17.         0.6428,
  18.         0.8660,
  19.         0.9848,
  20.         0.9848,
  21.         0.8660,
  22.         0.6428,
  23.         0.3420};
  24.  
  25. static inline double minP(double x, double y) { return (x <= y ? x : y); }
  26. static inline double maxP(double x, double y) { return (x <= y ? y : x); }
  27.  
  28. static inline int minP(int x, int y) { return (x <= y ? x : y); }
  29. static inline int maxP(int x, int y) { return (x <= y ? y : x); }
  30.  
  31. int roundP (double x) {
  32.   int i = (int) x;
  33.   if (x >= 0.0) {
  34.     return ((x-i) >= 0.5) ? (i + 1) : (i);
  35.   } else {
  36.     return (-x+i >= 0.5) ? (i - 1) : (i);
  37.   }
  38.  
  39. }
  40.  
  41. mxArray *process(const mxArray *mximage, const mxArray *mxsbin) {
  42.   double *im = (double *)mxGetPr(mximage);
  43.   double dd;
  44.   const int *dims = mxGetDimensions(mximage);
  45.   if (mxGetNumberOfDimensions(mximage) != 2 ||
  46.       mxGetClassID(mximage) != mxDOUBLE_CLASS)
  47.     mexErrMsgTxt("Invalid input");
  48.  
  49.   int sbin = (int)mxGetScalar(mxsbin);
  50.   int blocks[2];
  51.   blocks[0] = (int)roundP((double)dims[0]/(double)sbin);
  52.   blocks[1] = (int)roundP((double)dims[1]/(double)sbin);
  53.  
  54.   int visible[2];
  55.   visible[0] = blocks[0]*sbin;
  56.   visible[1] = blocks[1]*sbin;
  57.  
  58.   int out[3] = {visible[0]-2, visible[1]-2, 2};
  59.  
  60.   mxArray *mxGradient = mxCreateNumericArray(3, out, mxDOUBLE_CLASS, mxREAL);
  61.   double *gradient = (double *)mxGetPr(mxGradient);
  62.  
  63.   double *s;
  64.   double dot, o;
  65.   double dx, dy, v;
  66.  
  67.   double *tempGradientOBase   = gradient;
  68.   double *tempGradientVBase   = gradient + ( out[0] * out[1]);
  69.  
  70.   double *tempGradientDot;
  71.   double *tempGradientO;
  72.   double *tempGradientV;
  73.  
  74.   int mul = dims[0]*dims[1];
  75.    
  76.   for (int x = 1; x < visible[1] - 1; x++) {
  77.     for (int y = 1; y < visible[0] - 1; y++) {
  78.      
  79.     // Gray channel
  80.     s = im + minP(x, dims[1]-2)*dims[0] + minP(y, dims[0]-2);
  81.     dy = *(s+1) - *(s-1);
  82.     dx = *(s+dims[0]) - *(s-dims[0]);
  83.     v = sqrt(dx*dx + dy*dy);
  84.    
  85.     double best_dot = 0;
  86.     int best_o = 0;
  87.     for (int o = 0; o < 9; o++) {
  88.       double dot = uu[o]*dx + vv[o]*dy;
  89.       if (dot > best_dot) {
  90.     best_dot = dot;
  91.     best_o = o;
  92.       } else if (-dot > best_dot) {
  93.     best_dot = -dot;
  94.     best_o = o+9;
  95.       }
  96.     }
  97.      
  98.     int val = ((x - 1) * out[0]) + (y - 1);
  99.     tempGradientO   = tempGradientOBase + val;
  100.     tempGradientV   = tempGradientVBase  + val;
  101.     tempGradientO[0]   = (double) best_o;
  102.     tempGradientV[0]   = v;
  103.     }
  104.   }
  105.   return mxGradient;
  106. }
  107.  
  108. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  109.   if (nrhs != 2)
  110.     mexErrMsgTxt("Wrong number of inputs");
  111.   if (nlhs != 1)
  112.     mexErrMsgTxt("Wrong number of outputs");
  113.   plhs[0] = process(prhs[0], prhs[1]);
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement