fconv3D.cc 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. #include "mex.h"
  2. #include <math.h>
  3. #include <string.h>
  4. /*
  5. * This code is used for computing filter responses. It computes the
  6. * response of a set of filters with a feature map.
  7. *
  8. * Basic version, relatively slow but very compatible.
  9. */
  10. struct thread_data {
  11. double *A;
  12. double *B;
  13. double *C;
  14. mxArray *mxC;
  15. const mwSize *A_dims;
  16. const mwSize *B_dims;
  17. mwSize C_dims[2];
  18. };
  19. // convolve A and B
  20. void *process(void *thread_arg)
  21. {
  22. thread_data *args = (thread_data *)thread_arg;
  23. //input A - e.g., an image
  24. double *A = args->A;
  25. //input B - e.g., a filter
  26. double *B = args->B;
  27. //output C - the convolution response map
  28. double *C = args->C;
  29. const mwSize *A_dims = args->A_dims;
  30. const mwSize *B_dims = args->B_dims;
  31. const mwSize *C_dims = args->C_dims;
  32. int num_features = args->A_dims[2];
  33. // run over all dimensions (feature planes)
  34. for (int f = 0; f < num_features; f++)
  35. {
  36. //new pointers just for comparability with original fconv.cc
  37. double *dst = C;
  38. double *A_src = A + f*A_dims[0]*A_dims[1];
  39. double *B_src = B + f*B_dims[0]*B_dims[1];
  40. //run over possible locations on x dimension
  41. for (int x = 0; x < C_dims[1]; x++)
  42. {
  43. //run over possible locations on y dimension
  44. for (int y = 0; y < C_dims[0]; y++)
  45. {
  46. // compute convolution score for current position
  47. // loop over filter size
  48. double val = 0;
  49. for (int xp = 0; xp < B_dims[1]; xp++)
  50. {
  51. double *A_off = A_src + (x+xp)*A_dims[0] + y;
  52. double *B_off = B_src + xp*B_dims[0];
  53. //hope-to-be efficient version for up to 20 dimensions
  54. switch(B_dims[0])
  55. {
  56. case 20: val += A_off[19] * B_off[19];
  57. case 19: val += A_off[18] * B_off[18];
  58. case 18: val += A_off[17] * B_off[17];
  59. case 17: val += A_off[16] * B_off[16];
  60. case 16: val += A_off[15] * B_off[15];
  61. case 15: val += A_off[14] * B_off[14];
  62. case 14: val += A_off[13] * B_off[13];
  63. case 13: val += A_off[12] * B_off[12];
  64. case 12: val += A_off[11] * B_off[11];
  65. case 11: val += A_off[10] * B_off[10];
  66. case 10: val += A_off[9] * B_off[9];
  67. case 9: val += A_off[8] * B_off[8];
  68. case 8: val += A_off[7] * B_off[7];
  69. case 7: val += A_off[6] * B_off[6];
  70. case 6: val += A_off[5] * B_off[5];
  71. case 5: val += A_off[4] * B_off[4];
  72. case 4: val += A_off[3] * B_off[3];
  73. case 3: val += A_off[2] * B_off[2];
  74. case 2: val += A_off[1] * B_off[1];
  75. case 1: val += A_off[0] * B_off[0];
  76. break;
  77. // less efficient version with more than 20 dimensions
  78. default:
  79. for (int yp = 0; yp < B_dims[0]; yp++)
  80. {
  81. val += *(A_off++) * *(B_off++);
  82. }
  83. } // nasty switch-statement
  84. } //for-loop over filtersize
  85. //write value to current position in output data and increment output pointer
  86. *(dst++) += val;
  87. }// for-loop over y-positions
  88. }// for-loop over x-positions
  89. }// for-loop over dimensions (feature planes)
  90. }
  91. // matlab entry point
  92. // C = fconv(A, cell of B, start, end);
  93. // for convolving a multi-dim filter with a multi-dim image, call fconv3D( A, B, 1, 1)
  94. void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  95. if (nrhs != 4)
  96. mexErrMsgTxt("Wrong number of inputs");
  97. if (nlhs != 1)
  98. mexErrMsgTxt("Wrong number of outputs");
  99. // get input A, e.g., a RGB image
  100. const mxArray *mxA = prhs[0];
  101. // safety check for input A
  102. if (mxGetNumberOfDimensions(mxA) != 3 /* only feature matrices with more than 1 dimension supported here*/||
  103. mxGetClassID(mxA) != mxDOUBLE_CLASS)
  104. mexErrMsgTxt("Invalid input: A");
  105. // get input B, e.g., a cell array of filters, and start/end indices for cells to use
  106. const mxArray *cellB = prhs[1];
  107. mwSize num_bs = mxGetNumberOfElements(cellB);
  108. // with which filter to start
  109. int start = (int)mxGetScalar(prhs[2]) - 1;
  110. // with which filter to end
  111. int end = (int)mxGetScalar(prhs[3]) - 1;
  112. // safety check for start and end wrt input cell array B
  113. if ( (start < 0) || (end >= num_bs) || (start > end) )
  114. {
  115. mexErrMsgTxt("Invalid input: start/end");
  116. }
  117. int i_numOfFilters = end-start+1;
  118. // output cell
  119. plhs[0] = mxCreateCellMatrix(1, i_numOfFilters);
  120. // do convolutions
  121. thread_data td;
  122. const mwSize *A_dims = mxGetDimensions(mxA);
  123. double *A = (double *)mxGetPr(mxA);
  124. for ( int i = 0; i < i_numOfFilters; i++ )
  125. {
  126. const mxArray *mxB = mxGetCell(cellB, i+start);
  127. td.A_dims = A_dims;
  128. td.A = A;
  129. td.B_dims = mxGetDimensions(mxB);
  130. td.B = (double *)mxGetPr(mxB);
  131. // safety check for input B
  132. if (mxGetNumberOfDimensions(mxB) != 3 /* only feature matrices with more than 1 dimension supported here*/||
  133. mxGetClassID(mxB) != mxDOUBLE_CLASS ||
  134. td.A_dims[2] != td.B_dims[2])
  135. {
  136. mexErrMsgTxt("Invalid input: B");
  137. }
  138. // compute size of output
  139. int height = td.A_dims[0] - td.B_dims[0] + 1;
  140. int width = td.A_dims[1] - td.B_dims[1] + 1;
  141. if ( (height < 1) || (width < 1) )
  142. mexErrMsgTxt("Invalid input: B should be smaller than A");
  143. td.C_dims[0] = height;
  144. td.C_dims[1] = width;
  145. td.mxC = mxCreateNumericArray(2, td.C_dims, mxDOUBLE_CLASS, mxREAL);
  146. td.C = (double *)mxGetPr(td.mxC);
  147. // call the main function doing the hacky convolution
  148. process( (void *)&td );
  149. /* Assign the new value to the ith cell. */
  150. mxSetCell(plhs[0], i, td.mxC);
  151. }
  152. }