|
Hi everybody
I am trying to make a MEX function to split a double class grey scale image into 2^N areas, which I had already done successfully in MATLAB.
The thing is that the compiled function works properly for N up to 8, and then MATLAB simply hangs, and I have to stop the process.
I guess I might be again messing up with memory management, but I find no clue how to get things to work. Any help will be very welcome!
Thanks in advance, and here it's the code:
#include "mex.h"
#include <math.h>
/*
* Median_Cut.c
* Used to speed up the Energy Split calculations
*
* [cVList, cHList] = Median_Cut(imageIn, N)
* imageIn is double, N is int
*
* This is a MEX-file for MATLAB.
*/
/* The gateway function */
void mexFunction( int nlhs, mxArray *plhs[],
int nrhs, const mxArray *prhs[])
{
/* variable declarations here */
int N, iRows, iCols, index, numTiles, sizeList, filledVals, row, col;
int *cVList, *cHList, *cVListTemp, *cHListTemp;
double energyT, energy;
double *imageIn, *energyP, *energyPtemp;
int n, k, i, j;
imageIn = mxGetPr(prhs[0]);
iRows = mxGetM(prhs[0]);
iCols = mxGetN(prhs[0]);
N = *(mxGetPr(prhs[1]));
numTiles = pow(2,N);
sizeList = 2*numTiles;
/* Allocate intermediate & output memory */
plhs[0] = mxCreateNumericMatrix(1, sizeList, mxINT16_CLASS, mxREAL);
cVList = (int *)mxGetData(plhs[0]);
plhs[1] = mxCreateNumericMatrix(1, sizeList, mxINT16_CLASS, mxREAL);
cHList = (int *)mxGetData(plhs[1]);
cVListTemp = mxCalloc(sizeList, sizeof(int));
cHListTemp = mxCalloc(sizeList, sizeof(int));
energyP = mxCalloc(numTiles, sizeof(int));
energyPtemp = mxCalloc(numTiles, sizeof(int));
cVList[0] = cHList[0] = 1;
cVList[1] = iRows;
cHList[1] = iCols;
filledVals = 2;
energyT = 0;
/* code here */
for (i = 0; i < iRows; i++)
{
for (j = 0; j < iCols; j++)
{
index = i * iCols + j;
energyT += imageIn[index];
}
}
energyP[0] = energyT;
for (n = 0; n < N; n++)
{
for (k = 0; k < filledVals; k+=2)
energyPtemp[k] = energyP[k];
for (k = 0; k < filledVals; k+=2)
{
energy = 0;
row = col = 0;
if ((cVList[k+1] - cVList[k]) > (cHList[k+1] - cHList[k]))
{
while (energy < energyP[k/2]/2)
{
for (j = cHList[k]-1; j < cHList[k+1]; j++)
{
index = (cVList[k] + row - 1) * iCols + j;
energy += imageIn[index];
}
row++;
}
cVListTemp[0 + 2*k] = cVList[k];
cVListTemp[1 + 2*k] = cVList[k] + row;
cVListTemp[2 + 2*k] = cVList[k] + row + 1;
cVListTemp[3 + 2*k] = cVList[k+1];
cHListTemp[0 + 2*k] = cHList[k];
cHListTemp[1 + 2*k] = cHList[k+1];
cHListTemp[2 + 2*k] = cHList[k];
cHListTemp[3 + 2*k] = cHList[k+1];
}
else
{
while (energy < energyP[k/2]/2)
{
for (i = cVList[k]-1; i < cVList[k+1]; i++)
{
index = i * iCols + (cHList[k] + col - 1);
energy += imageIn[index];
}
col++;
}
cHListTemp[0 + 2*k] = cHList[k];
cHListTemp[1 + 2*k] = cHList[k] + col;
cHListTemp[2 + 2*k] = cHList[k] + col + 1;
cHListTemp[3 + 2*k] = cHList[k+1];
cVListTemp[0 + 2*k] = cVList[k];
cVListTemp[1 + 2*k] = cVList[k+1];
cVListTemp[2 + 2*k] = cVList[k];
cVListTemp[3 + 2*k] = cVList[k+1];
}
energyPtemp[k+1] = energyP[k/2] - energy;
energyPtemp[k] = energy;
}
for (k = 0; k < filledVals; k++)
energyP[k] = energyPtemp[k];
filledVals *= 2;
for (k = 0; k < filledVals; k++)
{
cVList[k] = cVListTemp[k];
cHList[k] = cHListTemp[k];
}
}
mxFree(cVListTemp);
mxFree(cHListTemp);
mxFree(energyP);
mxFree(energyPtemp);
}
|