-
Notifications
You must be signed in to change notification settings - Fork 0
/
sumImage.c
78 lines (53 loc) · 1.9 KB
/
sumImage.c
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
/* This MEX function takes a set of indexes into an image, and the
image, then sums all values together that have the same index and
stores it at index position in a vector. */
#include "mex.h"
#include <math.h>
#include <string.h>
/* Arguments: LHS = sumvector, RHS = image, index */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[] ) {
// Lets use double for image
int *index;
double *image;
double *sum;
int i;
int nIndexes;
int nBins;
if(nlhs != 1) {
mexErrMsgIdAndTxt("MATLAB:sumImage:invalidNumOutputs",
"One output required.");
}
if(nrhs != 3) {
mexErrMsgIdAndTxt("MATLAB:sumImage:invalidNumInputs",
"Three inputs required. First argument an image, second argument indexes (int32!!), third argument nBins (int).");
}
image = mxGetPr(prhs[0]);
index = (int*) mxGetPr(prhs[1]);
nBins = (int) mxGetScalar(prhs[2]);
if(mxGetM(prhs[0]) != mxGetM(prhs[1]) || mxGetN(prhs[0]) != mxGetN(prhs[1])) {
mexErrMsgIdAndTxt("MATLAB:sumImage:incorrectSize",
"The size of the first and second input arguments must match.");
}
if(!mxIsClass(prhs[0],"double")
|| !mxIsClass(prhs[1],"int32")
|| !mxIsClass(prhs[2],"int32")) {
mexErrMsgIdAndTxt("MATLAB:sumImage:incorrectType",
"First double matrix, second int32 matrix, third int32 scalar");
}
nIndexes = mxGetM(prhs[1]) * mxGetN(prhs[1]);
plhs[0] = mxCreateDoubleMatrix((mwSize) nBins, (mwSize) 1, mxREAL);
sum = mxGetPr(plhs[0]);
/* Do the work */
for(i = 0; i < nIndexes; i++) {
// printf("%d: %f ", i, image[i]);
if(index[i]-1 >= nBins || index[i]-1 < 0) {
// This should never happen!!
printf("MEX error: i = %d, index[i] = %d, nBins = %d\n",
i, index[i], nBins);
mexErrMsgTxt("BAD BAD BAD!!");
}
// Need to subtract 1 from index, because matlab start from 1, C from 0
sum[index[i]-1] += image[i];
}
}