Amazon

Saturday, December 8, 2007

MATLAB Mex File for Const Time Median Filtering

Step1:
Download the C code from http://nomis80.org/ctmf.html
Step2:
Write the Mex function below, for example, with
name ctmf_mex.c
Step3:
In Matlab, run "mex ctmf_mex.c ctmf.c -output ctmf"

#include "ctmf.h"
/*
void ctmf(
const unsigned char* const src, unsigned char* const dst,
const int width, const int height,
const int src_step, const int dst_step,
const int r, const int cn, const long unsigned int memsize
);
*/
#include "mex.h"
#include "matrix.h"

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

unsigned char *I0, *I;
int height, width, r;
mwSize dims[2];
/* Check for proper number of arguments */
if (nrhs != 2) {
mexErrMsgTxt("Two input arguments required.");
} else if (nlhs > 1) {
mexErrMsgTxt("Too many output arguments.");
}
I0 = (unsigned char*)mxGetPr(prhs[0]);
/* Check the dimensions of Y. Y can be 4 X 1 or 1 X 4. */
height = mxGetM(prhs[0]);
width = mxGetN(prhs[0]);
r = (int)mxGetScalar(prhs[1]);

if (!mxIsClass(prhs[0],"uint8")) {
mexErrMsgTxt("I0 has be a matrix of type uint8.");
}
dims[0] = height;
dims[1] = width;
/* Create a matrix for the return argument */
plhs[0] = mxCreateNumericArray(2, dims, mxUINT8_CLASS, 0);
I = (unsigned char*)mxGetPr(plhs[0]);
ctmf(I0, I, height, width,
height, height,
r, 1, 512*1024
);
}

No comments: