Forums

C mex problem .. .help required urgently

Started by ahma...@yahoo.com December 12, 2006
Hello everyone,

The problem which I am facing is that I've wrote a simple convolution program in C and put it in the mex file. It worked fine when I gave the input a simple matrix of 12X12, But when I give in an image as an input, it gives me error .... The program and the error is attached for clarity ...... An early reply from anyone would really be appreciated .

Regards
Ahmad
%%%%%%%%%%%%%%%%%%
%%%THE MEX FILE%%%
%%%%%%%%%%%%%%%%%%

#include "mex.h"

void convo2( double output[280][272], double arr1[280][272])

{

double SE[3][3]={ {1,1,1},
{1,1,1},
{1,1,1}
};

int sum;
int j,k,l,m;

for (j = 0; j < 280; j++){
for (k = 0; k < 272; k++){

sum = 0;

for (l = 0; l < 3; l++){
for (m = 0; m < 3; m++){
sum+=(SE[l][m])*arr1[j+l-1][k+m-1];
}
}
sum=sum/9;
if ( sum < 0 ) sum = 0;
if ( sum > 255 ) sum = 255;

output[j+1][k+1] = sum;

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

{
double *x,*z;
int mrows, ncols;

mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);

plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);

z = mxGetPr(plhs[0]);
x = mxGetPr(prhs[0]);

convo2(z,x);

}

%%%%%%%%%%%%%%%%%%
%%%%THE ERROR %%%%
%%%%%%%%%%%%%%%%%%

Segmentation violation detected at Mon Dec 11 10:51:40 2006


Configuration:
MATLAB Version: 7.0.0.19920 (R14)
Operating System: Microsoft Windows XP
Window System: Version 5.1 (Build 2600: Service Pack 2)
Processor ID: x86 Family 15 Model 1 Stepping 2, GenuineIntel
Virtual Machine: Java 1.4.2 with Sun Microsystems Inc. Java HotSpot(TM) Client VM
(mixed mode)
Default Charset: ibm-5348_P100-1997

Register State:
EAX = f1c71c72 EBX = 11580030
ECX = 00000009 EDX = fffffffe
ESI = 11614c30 EDI = 00000079
EBP = 00cde00c ESP = 00cddfa0
EIP = 0e0a1433 FLG = 00010202

Stack Trace:
[0] convo2.dll:0x0e0a1433(0x11580030, 0x0fbc0e20, 0x00cde71c, 0)
[1] convo2.dll:0x0e0a14df(0, 0x00cde71c, 1, 0x00cde77c)
[2] libmex.dll:_mexRunMexFile(0, 0x00cde71c, 1, 0x00cde77c) + 71 bytes
[3] libmex.dll:public: virtual void __thiscall Mfh_mex::dispatch_file(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cde71c, 1, 0x00cde77c) + 157 bytes
[4] m_dispatcher.dll:public: virtual void __thiscall Mfh_file::dispatch_fh(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cde71c, 1, 0x00cde77c) + 273 bytes
[5] m_interpreter.dll:_inDispatchFromStack(628, 0x012582f0 "convo2", 0, 1) + 891 bytes
[6] m_interpreter.dll:enum opcodes __cdecl inDispatchCall(char const *,int,int,int,int *,int *)(0x012582f0 "convo2", 628, 0, 1) + 111 bytes
[7] m_interpreter.dll:int __cdecl inInterp(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag volatile *)(2, 0, 0, 0) + 2411 bytes
[8] m_interpreter.dll:int __cdecl inInterPcodeSJ(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag *)(2, 0, 0, 0) + 272 bytes
[9] m_interpreter.dll:_inInterPcode(2, 0x7876f2d8, 0, 0) + 69 bytes
[10] m_interpreter.dll:enum inExecutionStatus __cdecl in_local_call_eval_function(int *,struct _pcodeheader *,int *,struct mxArray_tag * * const,enum inDebugCheck)(0x00cdf2c8, 0x00cdf3bc, 2, 0x0f6c5010 "convo2(I)\n") + 162 bytes
[11] m_interpreter.dll:$L72592(0x7876f2d8, 0x0f6c5010 "convo2(I)\n", 10, 0) + 196 bytes
[12] m_interpreter.dll:enum inExecutionStatus __cdecl inEvalCmdWithLocalReturnandtype(char const *,int *,enum inDebugCheck)(0, 2, 1, 0x00cdf44c ") + 86 bytes
[13] m_interpreter.dll:_inEvalCmdNoEnd(0x0f6c5010 "convo2(I)\n", 0x00cdf4e4, 0x00cdf4a0, 0x01422050) + 16 bytes
[14] bridge.dll:_mnParser(0x7c80b529, 0x01422050, 0, 0) + 431 bytes
[15] mcr.dll:public: void __thiscall mcrInstance::mnParser(void)(271242, 0x4d5c3a43, 0x414c5441, 0x625c3742) + 87 bytes
[16] MATLAB.exe:0x00401d2f(4194304, 0, 271242, 0x01422050)
[17] MATLAB.exe:0x00403e45(3211320, 3538994, 0x7ffd9000, 0x8054b038)
[18] kernel32.dll:0x7c816d4f(0x00403cc0 "jth(U@", 0, 0, 1245343)

This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

If it is an official MathWorks function, please
follow these steps in reporting this problem to The MathWorks so
that we have the best chance of correcting it:

1. Send this crash report to s...@mathworks.com for automated analysis.
For your convenience, this information has been recorded in:
C:\DOCUME~1\Ahmad\LOCALS~1\Temp\matlab_crash_dump.2564

2. Also, if the problem is reproducible, send the crash report to
s...@mathworks.com along with:
- A specific list of steps that will reproduce the problem
- Any M, MEX, MDL or other files required to reproduce the problem
- Any error messages displayed to the command window
A technical support engineer will contact you with further information.

Thank you for your assistance. Please save your workspace and restart
MATLAB before continuing your work.
the very first thing that comes to my mind is; an image usually has dimensions MxNx3 where dimension 3 defines RGB content. I think even in grayscale image there are 3 dimensions. But, if your image is binary, I have no clue.

a...@yahoo.com wrote: Hello everyone,

The problem which I am facing is that I've wrote a simple convolution program in C and put it in the mex file. It worked fine when I gave the input a simple matrix of 12X12, But when I give in an image as an input, it gives me error .... The program and the error is attached for clarity ...... An early reply from anyone would really be appreciated .

Regards
Ahmad
%%%%%%%%%%%%%%%%%%
%%%THE MEX FILE%%%
%%%%%%%%%%%%%%%%%%

#include "mex.h"

void convo2( double output[280][272], double arr1[280][272])

{

double SE[3][3]={ {1,1,1},
{1,1,1},
{1,1,1}
};

int sum;
int j,k,l,m;

for (j = 0; j < 280; j++){
for (k = 0; k < 272; k++){

sum = 0;

for (l = 0; l < 3; l++){
for (m = 0; m < 3; m++){
sum+=(SE[l][m])*arr1[j+l-1][k+m-1];
}
}
sum=sum/9;
if ( sum < 0 ) sum = 0;
if ( sum > 255 ) sum = 255;

output[j+1][k+1] = sum;

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

{
double *x,*z;
int mrows, ncols;

mrows = mxGetM(prhs[0]);
ncols = mxGetN(prhs[0]);

plhs[0] = mxCreateDoubleMatrix(mrows,ncols, mxREAL);

z = mxGetPr(plhs[0]);
x = mxGetPr(prhs[0]);

convo2(z,x);

}

%%%%%%%%%%%%%%%%%%
%%%%THE ERROR %%%%
%%%%%%%%%%%%%%%%%%

Segmentation violation detected at Mon Dec 11 10:51:40 2006


Configuration:
MATLAB Version: 7.0.0.19920 (R14)
Operating System: Microsoft Windows XP
Window System: Version 5.1 (Build 2600: Service Pack 2)
Processor ID: x86 Family 15 Model 1 Stepping 2, GenuineIntel
Virtual Machine: Java 1.4.2 with Sun Microsystems Inc. Java HotSpot(TM) Client VM
(mixed mode)
Default Charset: ibm-5348_P100-1997

Register State:
EAX = f1c71c72 EBX = 11580030
ECX = 00000009 EDX = fffffffe
ESI = 11614c30 EDI = 00000079
EBP = 00cde00c ESP = 00cddfa0
EIP = 0e0a1433 FLG = 00010202

Stack Trace:
[0] convo2.dll:0x0e0a1433(0x11580030, 0x0fbc0e20, 0x00cde71c, 0)
[1] convo2.dll:0x0e0a14df(0, 0x00cde71c, 1, 0x00cde77c)
[2] libmex.dll:_mexRunMexFile(0, 0x00cde71c, 1, 0x00cde77c) + 71 bytes
[3] libmex.dll:public: virtual void __thiscall Mfh_mex::dispatch_file(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cde71c, 1, 0x00cde77c) + 157 bytes
[4] m_dispatcher.dll:public: virtual void __thiscall Mfh_file::dispatch_fh(int,struct mxArray_tag * *,int,struct mxArray_tag * *)(0, 0x00cde71c, 1, 0x00cde77c) + 273 bytes
[5] m_interpreter.dll:_inDispatchFromStack(628, 0x012582f0 "convo2", 0, 1) + 891 bytes
[6] m_interpreter.dll:enum opcodes __cdecl inDispatchCall(char const *,int,int,int,int *,int *)(0x012582f0 "convo2", 628, 0, 1) + 111 bytes
[7] m_interpreter.dll:int __cdecl inInterp(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag volatile *)(2, 0, 0, 0) + 2411 bytes
[8] m_interpreter.dll:int __cdecl inInterPcodeSJ(enum inDebugCheck,int,int,enum opcodes,struct inPcodeNest_tag *)(2, 0, 0, 0) + 272 bytes
[9] m_interpreter.dll:_inInterPcode(2, 0x7876f2d8, 0, 0) + 69 bytes
[10] m_interpreter.dll:enum inExecutionStatus __cdecl in_local_call_eval_function(int *,struct _pcodeheader *,int *,struct mxArray_tag * * const,enum inDebugCheck)(0x00cdf2c8, 0x00cdf3bc, 2, 0x0f6c5010 "convo2(I)\n") + 162 bytes
[11] m_interpreter.dll:$L72592(0x7876f2d8, 0x0f6c5010 "convo2(I)\n", 10, 0) + 196 bytes
[12] m_interpreter.dll:enum inExecutionStatus __cdecl inEvalCmdWithLocalReturnandtype(char const *,int *,enum inDebugCheck)(0, 2, 1, 0x00cdf44c ") + 86 bytes
[13] m_interpreter.dll:_inEvalCmdNoEnd(0x0f6c5010 "convo2(I)\n", 0x00cdf4e4, 0x00cdf4a0, 0x01422050) + 16 bytes
[14] bridge.dll:_mnParser(0x7c80b529, 0x01422050, 0, 0) + 431 bytes
[15] mcr.dll:public: void __thiscall mcrInstance::mnParser(void)(271242, 0x4d5c3a43, 0x414c5441, 0x625c3742) + 87 bytes
[16] MATLAB.exe:0x00401d2f(4194304, 0, 271242, 0x01422050)
[17] MATLAB.exe:0x00403e45(3211320, 3538994, 0x7ffd9000, 0x8054b038)
[18] kernel32.dll:0x7c816d4f(0x00403cc0 "jth(U@", 0, 0, 1245343)

This error was detected while a MEX-file was running. If the MEX-file
is not an official MathWorks function, please examine its source code
for errors. Please consult the External Interfaces Guide for information
on debugging MEX-files.

If it is an official MathWorks function, please
follow these steps in reporting this problem to The MathWorks so
that we have the best chance of correcting it:

1. Send this crash report to s...@mathworks.com for automated analysis.
For your convenience, this information has been recorded in:
C:\DOCUME~1\Ahmad\LOCALS~1\Temp\matlab_crash_dump.2564

2. Also, if the problem is reproducible, send the crash report to
s...@mathworks.com along with:
- A specific list of steps that will reproduce the problem
- Any M, MEX, MDL or other files required to reproduce the problem
- Any error messages displayed to the command window
A technical support engineer will contact you with further information.

Thank you for your assistance. Please save your workspace and restart
MATLAB before continuing your work.