如何使用MEX从特定位置读取图像?

时间:2021-07-16 09:54:33

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

我有一个用C编写的函数来读取图像如下:

Image *read_Image(char *filename, int showmessages)

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???

现在我想在MATLAB中通过在MEX中创建网关函数和包装函数来执行此函数。我从来没有写过Matlab C / Mex代码。经过[http://cnx.org/contents/15601bc4-3cda-4964-a7e4-5e061c8aa8b7@2/Writing-C-Functions-in-MATLAB-][1]后,我试着运气,并编写了以下代码。我仍然需要做很多事情,我被困在中途。谁能引导我?

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 filename.c编译此代码。我收到以下错误。

 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-

1 个解决方案

#1


3  

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.

我不知道一个名为mxGetScaler的函数。试试mxGetScalar。如果你的意思是mxGetScaler,它必须来自另一个库(你的CVIP工具?)。您必须通过将.lib文件附加到mex命令来链接任何预编译的依赖项,或者通过将源文件附加到命令来编译它们的源以及mex文件。

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

请注意,您将在显示的代码中遇到其他几个编译问题。 const正确性和指针转换错误比比皆是。

#1


3  

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.

我不知道一个名为mxGetScaler的函数。试试mxGetScalar。如果你的意思是mxGetScaler,它必须来自另一个库(你的CVIP工具?)。您必须通过将.lib文件附加到mex命令来链接任何预编译的依赖项,或者通过将源文件附加到命令来编译它们的源以及mex文件。

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

请注意,您将在显示的代码中遇到其他几个编译问题。 const正确性和指针转换错误比比皆是。