Matlab MEX file - index correct, incorrect UINT32 values
4 views (last 30 days)
Show older comments
I am getting incorrect answers from my mex function. I generate a test file with this code:
%Create a 4D array 2x,2y, rgb(3), framenumber(from 1 to 5)
%Framenumber from 1 to 5
testmatrix(1,1,1,1) = 0;
testmatrix(1,1,1,2) = 1;
testmatrix(1,1,1,3) = 2;
testmatrix(1,1,1,4) = 3;
testmatrix(1,1,1,5) = 4;
testmatrix(1,1,2,1) = 5;
testmatrix(1,1,2,2) = 6;
testmatrix(1,1,2,3) = 7;
testmatrix(1,1,2,4) = 8;
testmatrix(1,1,2,5) = 9;
testmatrix(1,1,3,1) = 10;
testmatrix(1,1,3,2) = 11;
testmatrix(1,1,3,3) = 12;
testmatrix(1,1,3,4) = 13;
testmatrix(1,1,3,5) = 14;
testmatrix(1,2,1,1) = 15;
testmatrix(1,2,1,2) = 16;
testmatrix(1,2,1,3) = 17;
testmatrix(1,2,1,4) = 18;
testmatrix(1,2,1,5) = 19;
testmatrix(1,2,2,1) = 20;
testmatrix(1,2,2,2) = 21;
testmatrix(1,2,2,3) = 22;
testmatrix(1,2,2,4) = 23;
testmatrix(1,2,2,5) = 24;
testmatrix(1,2,3,1) = 25;
testmatrix(1,2,3,2) = 26;
testmatrix(1,2,3,3) = 27;
testmatrix(1,2,3,4) = 28;
testmatrix(1,2,3,5) = 29;
testmatrix(2,1,1,1) = 30;
testmatrix(2,1,1,2) = 31;
testmatrix(2,1,1,3) = 32;
testmatrix(2,1,1,4) = 33;
testmatrix(2,1,1,5) = 34;
testmatrix(2,1,2,1) = 35;
testmatrix(2,1,2,2) = 36;
testmatrix(2,1,2,3) = 37;
testmatrix(2,1,2,4) = 38;
testmatrix(2,1,2,5) = 39;
testmatrix(2,1,3,1) = 40;
testmatrix(2,1,3,2) = 41;
testmatrix(2,1,3,3) = 42;
testmatrix(2,1,3,4) = 43;
testmatrix(2,1,3,5) = 44;
testmatrix(2,2,1,1) = 45;
testmatrix(2,2,1,2) = 46;
testmatrix(2,2,1,3) = 47;
testmatrix(2,2,1,4) = 48;
testmatrix(2,2,1,5) = 49;
testmatrix(2,2,2,1) = 50;
testmatrix(2,2,2,2) = 51;
testmatrix(2,2,2,3) = 52;
testmatrix(2,2,2,4) = 53;
testmatrix(2,2,2,5) = 54;
testmatrix(2,2,3,1) = 55;
testmatrix(2,2,3,2) = 56;
testmatrix(2,2,3,3) = 57;
testmatrix(2,2,3,4) = 58;
testmatrix(2,2,3,5) = 59;
testmatrix = uint8(testmatrix);
I created the following mex function:
#include <stdint.h>
#include "mex.h"
void
mexFunction(int nlhs,mxArray *plhs[],int nrhs,const mxArray *prhs[])
{
//Check - expect 1 input, expect 1 outputs
if (nrhs != 1) {
mexErrMsgIdAndTxt( "MATLAB:24bit_pixel_from_uint8_rgb:invalidNumInputs",
"One input argument required.");
}
if (nlhs > 1) {
mexErrMsgIdAndTxt( "MATLAB:24bit_pixel_from_uint8_rgb:maxlhs",
"Too many output arguments.");
}
//declare variables
mwSize *dim_array, dims;
mxArray *a_in_m, *b_out_m;
uint32_t *a,b;
int X,Y,RGB,FRAMENUMBER;
int X_INDX,Y_INDX,RGB_INDX,FRAMENUMBER_INDX;
dims = mxGetNumberOfDimensions(prhs[0]);
dim_array = mxGetDimensions(prhs[0]);
X = dim_array[0];
Y = dim_array[1];
RGB = dim_array[2];
FRAMENUMBER = dim_array[3];
mexPrintf("dims: %d\n",dims);
mexPrintf("X: %d\n",X);
mexPrintf("Y: %d\n",Y);
mexPrintf("RGB: %d\n",RGB);
mexPrintf("FRAMENUMBER: %d\n",FRAMENUMBER);
a_in_m = mxDuplicateArray(prhs[0]);
a = mxGetData(a_in_m);
mexPrintf("a is: %d\n",a[0]); //SHOULD BE 0!
b_out_m = plhs[0] = mxCreateNumericArray(dims, dim_array, mxUINT32_CLASS, 0);
b = mxGetPr(b_out_m);
int linear_index = 0;
for(X_INDX=0;X_INDX<X;X_INDX++)
{
for(Y_INDX=0;Y_INDX<Y;Y_INDX++)
{
for(RGB_INDX=0;RGB_INDX<RGB;RGB_INDX++)
{
for(FRAMENUMBER_INDX=0;FRAMENUMBER_INDX<FRAMENUMBER;FRAMENUMBER_INDX++)
{
mexPrintf("Linear Index: %d\n",linear_index);
mexPrintf("Value[%d]: %d\n",FRAMENUMBER_INDX + (RGB_INDX*FRAMENUMBER)+(Y_INDX*RGB*FRAMENUMBER)+(X_INDX*Y*RGB*FRAMENUMBER),a[FRAMENUMBER_INDX + (RGB_INDX*FRAMENUMBER)+(Y_INDX*RGB*FRAMENUMBER)+(X_INDX*Y*RGB*FRAMENUMBER)]);
linear_index += 1;
}
}
}
}
}
When I call it I get the following:
>> rgb_to_uint8(testmatrix)
dims: 4
X: 2
Y: 2
RGB: 3
FRAMENUMBER: 5
a is: 755965440
Linear Index: 0
Value[0]: 755965440
Linear Index: 1
Value[1]: 840180485
Linear Index: 2
Value[2]: 924395530
Linear Index: 3
Value[3]: 772808449
...etc
Can somebody help me figure out what I've done wrong?
0 Comments
Answers (1)
James Tursa
on 27 Jul 2015
The input is a uint8 array, but you are using a uint32_t pointer to access the data. So there is a mismatch. Change this:
uint32_t *a,b;
To this:
unsigned char *a;
Also, your use of the variable b is all wrong. As you have it written, b it a uint32_t type ... it is NOT a pointer! And yet you assume it is a pointer in the following bit of code:
b_out_m = plhs[0] = mxCreateNumericArray(dims, dim_array, mxUINT32_CLASS, 0);
b = mxGetPr(b_out_m);
That bit of code is also screwy because you create a mxArray of class uint32, but then access the pointer with mxGetPr which returns a (double *) type. If you really want to use a uint32 class here, then so something like this instead:
uint32_t *b;
:
b_out_m = plhs[0] = mxCreateNumericArray(dims, dim_array, mxUINT32_CLASS, 0);
b = (uint32_t *)mxGetData(b_out_m);
0 Comments
See Also
Categories
Find more on Matrox Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!