c - MATLAB mexCallMatlab 导致崩溃

标签 c matlab mex sliding-window

我正尝试在 mex 中开发一个函数。该功能的目的应该是将窗口滑动到 3D 图像上并在窗口内应用一些功能。目前我被卡住了,因为当算法到达 padarray 函数的调用时,MATLAB 崩溃了。

到目前为止我写了这几行代码

#include <iostream>
#include "mex.h"
#include "matrix.h"       

using namespace std;

void mexFunction(int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
    #define O_IMAGE       plhs[0]
    #define I_IMAGE       prhs[0]
    #define I_PADSIZE     prhs[1]
    #define I_FUN         prhs[2]
    #define I_OPTION      prhs[3]

  // Argument Checking:
  if(nrhs < 1 || nrhs > 4) /* Check the number of arguments */
    mexErrMsgTxt("Wrong number of input arguments.");
  else if(nlhs > 1)
    mexErrMsgTxt("Too many output arguments.");

  // Variable declarations
  static const char padding[] = "symmetric";
  const char *windowShape;
  const mwSize *dim;
  mwSize *dimPad;
  mwSize ndim;
  double *pad, *windowSize, *thetaStep, *phiStep;
  mxArray *inputFun[4], *inputPadFun[3], *input_image, *imagePad, *output_image;

  /*Get dimensions of input image*/
  ndim = mxGetNumberOfDimensions(I_IMAGE);
  dim = mxGetDimensions(I_IMAGE);

  /* Create dimensions of padded image*/
  dimPad = (mwSize *) mxCalloc(ndim, sizeof(mwSize));
  dimPad = (mwSize *) memcpy((void *) dimPad,(void *) dim, ndim*sizeof(mwSize));
  pad = mxGetPr(I_PADSIZE);
  for (int i=0;i<ndim;++i)
  {
      dimPad[i] += 2*pad[i];
  }

  /*Get pointer to the input image*/
  input_image = (mxArray *) mxGetData(I_IMAGE);

  /* Create output image*/
  O_IMAGE = mxCreateNumericArray(ndim, dim, mxDOUBLE_CLASS, mxREAL);
  output_image = (mxArray *) mxGetData(O_IMAGE);

  /* Create padded image*/
  imagePad = mxCreateNumericArray(ndim, dimPad, mxDOUBLE_CLASS, mxREAL);

  // Padding input matrix
  inputPadFun[0] = input_image;
  inputPadFun[1] = (mxArray *)(pad);
  inputPadFun[2] = (mxArray *)(padding);
  mexCallMATLAB(1, &imagePad, 3, inputPadFun, "padarray");

  // Clean UP
  mxFree(dimPad);
  mxDestroyArray(imagePad);
}

我在 mexCallMATLAB 中检查了输入和输出图像的尺寸,它们似乎是正确的。我真的不明白我做错了什么。任何帮助是极大的赞赏!

最佳答案

有几个有问题的转换。

因为 pad 是一个 double*,你在这里做了一个无意义的转换:

(mxArray *)(pad)

对于 padding 类似,这是一个 const char[],你不能这样做

(mxArray *)(padding)

在下一行中,mxGetDatavoid* 返回到缓冲区,其中包含原始类型(doublesingle 等),但您正在将其转换为 mxArray*:

(mxArray *) mxGetData(prhs[0]);

请记住,mxArray 是由 MathWorks 定义的不透明对象类型,您必须使用它们的函数来创建这些对象。此外,您只能处理指向它们的指针。

还有其他问题...


代码

这是一个关于如何更改代码的 C++ 示例:

// padarray_BugsFree.cpp
#include <iostream>
#include "mex.h"
#include "matrix.h"       

void mexFunction(int nlhs,mxArray *plhs[], int nrhs, const mxArray *prhs[]) {
  if(nrhs != 2)
    mexErrMsgTxt("Wrong number of input arguments:\n\t"
            "Iout = padarray_BugsFree(I,pad)");
  if(nlhs > 1)
    mexErrMsgTxt("Too many output arguments:\n\t"
            "Iout = padarray_BugsFree(I,pad)");

  if(!(mxIsDouble(prhs[0]) && mxIsDouble(prhs[1])))
      mexErrMsgTxt("Inputs must be double");

  mwSize ndim = mxGetNumberOfDimensions(prhs[0]);
  const mwSize *dim = mxGetDimensions(prhs[0]);

  if (ndim != mxGetNumberOfElements(prhs[1]))
      mexErrMsgTxt("pad must be equal to ndims(I)");

  const double *pad = mxGetPr(prhs[1]);
  mwSize *dimPad = (mwSize *) mxCalloc(ndim, sizeof(mwSize));
  for (int i=0; i<ndim; ++i) {
      dimPad[i] = dim[i] + 2*pad[i];
  }

  mxArray *imagePad = mxCreateNumericArray(ndim, dimPad, mxDOUBLE_CLASS, mxREAL);
  mxFree(dimPad);

  // Padding input matrix
  mxArray *padding = mxCreateString("symmetric");
  mxArray *inputPadFun[3];
  inputPadFun[0] = const_cast<mxArray*>(prhs[0]);
  inputPadFun[1] = const_cast<mxArray*>(prhs[1]);
  inputPadFun[2] = padding;
  mexCallMATLAB(1, &imagePad, 3, inputPadFun, "padarray");
  mxDestroyArray(padding);

  // do something with the padded image and generate output
  plhs[0] = mxDuplicateArray(imagePad); // in place of useful operations
  mxDestroyArray(imagePad);
}

演示

>> I = magic(3)
I =
     8     1     6
     3     5     7
     4     9     2
>> pad = [2 2]
pad =
     2     2
>> padarray_BugsFree
Error using padarray_BugsFree
Wrong number of input arguments:
    Iout = padarray_BugsFree(I,pad) 
>> Iout = padarray_BugsFree(I,pad)
Iout =
     5     3     3     5     7     7     5
     1     8     8     1     6     6     1
     1     8     8     1     6     6     1
     5     3     3     5     7     7     5
     9     4     4     9     2     2     9
     9     4     4     9     2     2     9
     5     3     3     5     7     7     5

注意事项

使用以下帖子作为如何使用 mexCallMATLAB 的引用:

您还可以按如下方式构建 RHS 输入数组:

mxArray *inputPadFun[] = {const_cast<mxArray*>(prhs[0]),
                          const_cast<mxArray*>(prhs[1]), padding};
mexCallMATLAB(1, &imagePad, 3, inputPadFun, "padarray");

关于c - MATLAB mexCallMatlab 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32484956/

相关文章:

c - 服务器端输出奇怪的字符

c - 如何从Assembly中调用C函数,以及如何将其静态链接?

c - 如何在 C/C++ 中读取文件时搜索换行符

matlab - 使用 MATLAB 的标准差

class - 你如何在matlab中创建以对象作为成员变量的类?

c++ - 如何为matlab设置C/C++编译器?

c - 从 C 中的原始图像获取每个像素的 RGB 值

matlab - 检查矩阵中的所有特定值是否按特定顺序存在

Matlab mex 文件在 Windows 上崩溃 - 无法调试

c++ - 链接来自 MATLAB Mex 库的错误