Search code examples
cmatlabimage-processingmex

how to read image from particular location using MEX?


I have a function written in C to read an image as follows:

Image *read_Image(char *filename, int showmessages)

Now I want execute this function in MATLAB by creating a gateway function and wrapper function in MEX. I have never wrote Matlab C/Mex code. I tried my luck after going through [http://cnx.org/contents/15601bc4-3cda-4964-a7e4-5e061c8aa8b7@2/Writing-C-Functions-in-MATLAB-][1] and wrote the following code. I still need to do a lot, i am stuck in midway. Can anyone guide me???

following are the code I wrote:

#include "mex.h"
#include "CVIPtools.h"
#include "CVIPimage.h"
#include "CVIPdef.h"
#include "CVIPmap.h"
#include "limits.h"
#include "threshold.h"
#include <float.h>
#include "CVIPmatrix.h"


//Here I will write a wrapper function. 



/* main gateway function*/

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    mxArray *fnameData;
    int fnameLength;
    char *str;
    //mxArray *smData;
    int sm;
    double *outArray;
    int r,c,bands,n;
    const mwSize *dim_array;


    fnameData = prhs[0];
    //smData = prhs[1];

    fnameLength = mxGetN(fnameData)+1;
    str = mxCalloc(fnameLength, sizeof(char));
    mxGetString(fnameData, str, fnameLength);
   // sm = (int)(mxGetScaler(smData));
    sm = mxGetScaler(prhs[1]);


    if (nrhs != 2) {
        mexErrMsgIdAndTxt( "MATLAB:mxisfinite:invalidNumInputs",
                "Two input arguments required.");}
    if (nlhs != 1) {
        mexErrMsgIdAndTxt( "MATLAB:mxisfinite:invalidNumInputs",
                "One output argument required.");}

    n = mxGetNumberOfElements(plhs[0]);
    dim_array = mxGetDimensions(plhs[0]);
    r = dim_array[0];
    c = dim_array[1];
    bands = dim_array[2];

    if(bands==3){

        plhs[0] = mxCreateNumericArray(3, dim_array, mxDOUBLE_CLASS, mxREAL);

    }
    else
    { plhs[0] = mxCreateDoubleMatrix(r,c,mxREAL);
      bands=1;
       }

    outArray = mxGetData(plhs[0]);

    // Here I will call wrapper function.

 }

I just tried compiling this code using mex filename.c I got the following error.

 mex image_readCVIP.c
   Creating library D:\Users\Deepen\AppData\Local\Temp\mex_sedh1H\templib.x and object D:\Users\Deepen\AppData\Local\Temp\mex_sedh1H\templib.exp 
image_readCVIP.obj : error LNK2019: unresolved external symbol mxGetScaler referenced in function mexFunction 
image_readCVIP.mexw64 : fatal error LNK1120: 1 unresolved externals 

  D:\PROGRA~1\MATLAB\R2012A\BIN\MEX.PL: Error: Link of 'image_readCVIP.mexw64' failed
  [1]: http://cnx.org/contents/15601bc4-3cda-4964-a7e4-5e061c8aa8b7@2/Writing-C-Functions-in-MATLAB-

Solution

  • I'm not aware of a function called mxGetScaler. Try mxGetScalar. If you really mean mxGetScaler, it must be from another library (your CVIP tools?). You will have to link any pre-compiled dependencies by appending the .lib files to the mex command, or compile their source along with the mex file by appending the source files to the command.

    Note that you will have several other compilation issues with the shown code. const correctness and pointer casting errors abound.