简体   繁体   中英

Calling MATLAB function “imread” from MEX

I am trying to use mexCallMATLAB() to read an image. Following is the code:

#include "mex.h"
#include <matrix.h>
#include <string.h>
#include <stdio.h>

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    mxChar *string, *img;

    int dims[2] = {1, 100};
    char *str = "D:\\Acads\\NUS\\CBIS\\TEST\\SampleImages\\set1139R0stack1000Color0St0.tiff.tif"; // Path of the image file

    string = mxCreateCharArray(2, (const int *)dims);
    memcpy(mxGetPr(string), str, sizeof(char) * (strlen(str) + 1));

    mexCallMATLAB(1, img, 1, &string, "imread");
}

The code compiles without error but on execution throws the following error message:

    ??? Error using ==> imread at 315
    File "%^&*$#@! (Some special character string)" does not exist.

When I print the value of variable string , I get the correct path, but I don't understand what is happening while calling the MATLAB function.

In Matlab MEX the behaviour of strings differs in different versions.

In current versions I think that strings are represented as 16-bit Strings (UNICODE) instead of ASCII. The size of the character array must be exactly as long as the length of the string not containing a terminating NUL (this is true for all MATLAB versions).

To avoid problems you should use the special C-string-to-mxArray function (I think it is named mxCreateString or so).

By the way: "string" and "img" are of the type "mxArray *", not of the type "mxChar *".

The second argument of mexCallMATLAB must be a pointer to "img", not "img" itself!

You need to send your inputs as mxArrays to mexCallMATLAB. As per the doc at http://www.mathworks.com/help/matlab/apiref/mexcallmatlab.html the syntax is

int mexCallMATLAB(int nlhs, mxArray *plhs[], int nrhs,
  mxArray *prhs[], const char *functionName);

mxChar is not mxArray. I do not know how your code compiled. Did you see any warnings about this type mismatch?

You need to declare string and img as

mxArray *string, *img;

As Martin has suggested use mxCreateString instead of manually creating char array and copying it.

Thanks for pointing out the different representation of strings. I used the mxCreateString() to convert a C string to MATLAB string and it works fine.

mxChar is a special type of mxArray which stores characters as 2-byte Unicode characters, so we can use any of the two.

Following is the code which works.

#include "mex.h"

void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{   
    mxChar *string;
    mxArray *img[1];

    string = mxCreateString("D:\\Acads\\NUS\\CBIS\\TEST\\SampleImages\\set1139R0stack1000Color0St0.tiff.tif");
    mexCallMATLAB(1, img, 1, &string, "imread");
}

Please note that declaring string as mxArray * also works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM