/* * $Id: spm_dilate_erode.c 4569 2011-11-23 16:11:30Z guillaume $ * Jesper Andersson */ #include #include "mex.h" #ifndef MIN #define MIN(A,B) ((A) > (B) ? (B) : (A)) #endif #ifndef MAX #define MAX(A,B) ((A) > (B) ? (A) : (B)) #endif /* Utility function that returns index into */ /* 1D array with range checking. */ mwSignedIndex get_index(mwSignedIndex i, mwSignedIndex j, mwSignedIndex k, mwSize dim[3]) { if ((i<0) || (i>(dim[0]-1)) || (j<0) || (j>(dim[1]-1)) || (k<0) || (k>(dim[2]-1))) return(-1); else return(k*dim[0]*dim[1]+j*dim[0]+i); } /* Function doing the job. */ void do_it(double *iima, mwSize dim[3], double *krnl, mwSize kdim[3], int dilate, double *oima) { mwIndex i=0, j=0, k=0; mwIndex ki=0, kj=0, kk=0; mwIndex ndx=0; mwSignedIndex kndx=0; double kv=0.0; for (i=0; i -1) { if (dilate) { if ((kv=krnl[get_index(ki,kj,kk,kdim)])) { oima[ndx] = MAX(oima[ndx],kv*iima[kndx]); } } else /* erode */ { if ((kv=krnl[get_index(ki,kj,kk,kdim)])) { oima[ndx] = MIN(oima[ndx],kv*iima[kndx]); } } } } } } } } } } /* Gateway function */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { char *fnc_str = NULL; mwSize ndim=0, krn_ndim=0; int n, i; int dilate = 0; mwSize buflen = 0; const mwSize *cdim = NULL, *krn_cdim = NULL; mwSize dim[3], kdim[3]; double *iima = NULL; double *oima = NULL; double *krnl = NULL; if (nrhs < 3) mexErrMsgTxt("Not enough input arguments."); if (nrhs > 3) mexErrMsgTxt("Too many input arguments."); if (nlhs < 1) mexErrMsgTxt("Not enough output arguments"); if (nlhs > 1) mexErrMsgTxt("Too many output arguments."); /* Get image */ if (!mxIsNumeric(prhs[0]) || mxIsComplex(prhs[0]) || mxIsSparse(prhs[0]) || !mxIsDouble(prhs[0])) { mexErrMsgTxt("spm_dilate_erode: ima must be numeric, real, full and double"); } ndim = mxGetNumberOfDimensions(prhs[0]); if ((ndim < 2) || (ndim > 3)) { mexErrMsgTxt("spm_dilate_erode: ima must be 2 or 3-dimensional"); } cdim = mxGetDimensions(prhs[0]); iima = mxGetPr(prhs[0]); /* Fix dimensions to allow for 2D and 3D data */ dim[0] = cdim[0]; dim[1] = cdim[1]; if (ndim==2) {dim[2]=1; ndim=3;} else {dim[2]=cdim[2];} for (i=0, n=1; i