From: Leyart Galli on
Hello, I have an issue I am not able to overcome.
I want to create a mex file and pass to it a 3D double matrix representing an Image,
for instance inputImage[480,640,3].
I cannot understand how to access the element of the matrix inside the mex files, I have tried everything with pointers with no results at all, it always returns me 0 or random numbers.
If someone would be so gentle to help me, I will be really glad to him.
Best Regards,
Luca
From: Jan Simon on
Dear Luca!

> I want to create a mex file and pass to it a 3D double matrix representing an Image,
> for instance inputImage[480,640,3].
> I cannot understand how to access the element of the matrix inside the mex files, I have tried everything with pointers with no results at all, it always returns me 0 or random numbers.

Just show us, what you've done and we try to fix the problems.
It would be not efficient, if I write everything I know about accessing elements of arrays in Mex files.
Another idea is looking in the FEX for Mex files. Most of them are working with elements of arrays. E.g. XSum can be used as example of how to perform operations on subvectors in any dimension.

Kind regards, Jan
From: Rune Allnor on
On 12 Mar, 12:27, "Leyart Galli" <leyart_era...(a)gmail.com> wrote:
> Hello, I have an issue I am not able to overcome.
> I want to create a mex file and pass to it a 3D double matrix representing an Image,
> for instance inputImage[480,640,3].
> I cannot understand how to access the element of the matrix inside the mex files, I have tried everything with pointers with no results at all, it always returns me 0 or random numbers.

Look up the documentation of

mxGetData

to find out how to access the data in the array.

Look up the section on multidimensional arrays
in the matlab documentation, to see the details
of the layout. In R2006a:

Matlab -> Programming -> Data Structures -> Multidimensional Arrays

Rune
From: Leyart Galli on
Hello, sorry for the delay.
The code I am trying to convert is the following:

function [masked] = skincbcr(inputImage)
processed=zeros(size(inputImage,1),size(inputImage,2));
for ii=1:size(inputImage,1)
for jj=1:size(inputImage,2)
cb=inputImage(ii,jj,2);
cr=inputImage(ii,jj,3);
eq1=1.5862*cb+20;
eq2=0.3448*cb+76.2069;
eq3=-4.5652*cb+234.5652;
eq4=-1.15*cb+301.75;
eq5=-2.2857*cb+432.85;
if(cr<=eq1&&cr>=eq2&&cr>=eq3&&cr<=eq4&&cr<=eq5)
processed(ii,jj)=1;
else
processed(ii,jj)=0;
end;
end;
end;
masked=processed;

I want to convert it because I want to achieve the best performance possible and I am not able to achieve them with just matlab code (Over 5 seconds for a 640*480 image)

The following is the code I have produced:

#include "math.h"
#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
//Declarations
mxArray *xData;
double *inputImage;
double cb,cr;
mwSize ii,jj,channel;
mwSize rowLen, colLen,total_num_of_cells,nsubs,first,second,third;
//Copy input pointer
xData = prhs[0];
//Get matrix length
rowLen = mxGetDimensions(xData)[1];
colLen = mxGetDimensions(xData)[2];
//Allocate memory and assign output pointer
plhs[0] = mxCreateDoubleMatrix(colLen, rowLen, mxREAL); //mxReal is our data-type, I want a 2d matrix as output
cb=0;
cr=0;
channel=1;
total_num_of_cells = mxGetNumberOfElements(xData);
mexPrintf("total num of cells = %d\n", total_num_of_cells);
nsubs=mxGetNumberOfDimensions(xData);
mexPrintf("number of dimensions = %d\n", nsubs);
first=mxGetDimensions(xData)[0];
second=mxGetDimensions(xData)[1];
third=mxGetDimensions(xData)[2];
mexPrintf("first: %d second: %d third: %d\n", first,second,third);
inputImage=(double *)mxGetPr(xData);
for(ii=0;ii<rowLen;ii++)
{
for(jj=0;jj<colLen;jj++)
{
channel=1;
cb=(double)inputImage[ii+640*jj+1*640*480];
channel=2;
cr=(double)inputImage[ii,jj,2];
mexPrintf("first: %f second: %f ",cb,cr);
}

}
}

It should just write to me the values of each matrix cells but it does not work, in either way (notice the difference between cb and cr).

I cannot really know where to hit my head on.
I hope someone would be so gentle to help me :)
Best Regards,
Luca
From: Leyart Galli on
I am such stupid, posting the code here I managed to figure out by myself where the problem laid: the output type of the matrix should not have been mxReal but mxDOUBLE_CLASS instead :)
I hope that this piece of code will help someone in the future.
Thank you to everyone which has replied me