dig*_*ggy 1 c++ integration matlab mex
我有一个很大的 C++ 代码,我想将它集成到 MATLAB 中,以便我可以在我的 matlab 代码中使用它。如果它是一个单独的代码来制作它的 mex 文件将是最好的选择。但是既然现在是需要编译构建才能运行的代码,我不知道如何使用这段代码中的功能。
为整个代码制作 mex 文件是唯一的选择还是有其他解决方法?另外,我想了解如何为整个代码制作 mex 文件然后构建它。
为了获得更多见解,这是我试图在 matlab http://graphics.stanford.edu/projects/drf/densecrf_v_2_2.zip 中集成的代码。谢谢你!
首先,您需要编译库(静态或动态链接)。以下是我在 Windows 机器上执行的步骤(我使用 Visual Studio 2013 作为 C++ 编译器):
densecrf.sln解决方案文件。这将产生一个静态库densecrf.lib接下来修改示例文件dense_inference.cpp以使其成为 MEX 函数。我们将把main函数替换为:
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
..
}
Run Code Online (Sandbox Code Playgroud)
而不是在argc/中接收参数argv,我们将从 input 中获取参数mxArray。所以像:
if (nrhs<3 || nlhs>0)
mexErrMsgIdAndTxt("mex:error", "Wrong number of arguments");
if (!mxIsChar(prhs[0]) || !mxIsChar(prhs[1]) || !mxIsChar(prhs[2]))
mexErrMsgIdAndTxt("mex:error", "Expects string arguments");
char *filename = mxArrayToString(prhs[0]);
unsigned char * im = readPPM(filename, W, H );
mxFree(filename);
//... same for the other input arguments
// The example receives three arguments: input image, annotation image,
// and output image, all specified as image file names.
// also replace all error message and "return" exit points
// by using "mexErrMsgIdAndTxt" to indicate an error
Run Code Online (Sandbox Code Playgroud)
最后,我们编译修改后的 MEX 文件(将编译后的 LIB 放在同一example文件夹中):
>> mex -largeArrayDims dense_inference.cpp util.cpp -I. -I../include densecrf.lib
Run Code Online (Sandbox Code Playgroud)
现在我们从 MATLAB 内部调用 MEX 函数:
>> dense_inference im1.ppm anno1.ppm out.ppm
Run Code Online (Sandbox Code Playgroud)
得到的分割图像:
