The compiler matlab .m files into the dll steps of:
This article describes a method using only the tools compiled deploytool
- Type deploytool command, open the Tools deploytool
Select the third option "Library Compiler"
Select the top left corner of "C ++ Shared Library", next to "Add exported functions" to the right click the plus sign to add a function to be exported, you can add more
After adding a .m functions, the rightmost "Package" turns green selectable, click on it to generate a dll
2 vs method invocation
In this paper, vs2015, dll calls using the display mode (mainly troublesome, as long as a dll, do not .h and .lib files.)
- Create a new console project calls dll test
- It contains an interface sdk matlab and c ++'s
Navigate to: Right Project - Attribute -VC ++ directory - a directory, add .. \ MATLAB \ R2017a \ extern \ include
Add library directory: .. \ MATLAB \ R2017a \ extern \ lib \ win64 \ microsoft
Linker - Input - Additional Dependencies, add the following lib
mclmcr.lib
mclmcrrt.lib
libmx.lib
libmex.lib
libmat.lib
libeng.lib
测试主函数
用deploytool生成dll时,也会生成.h文件,里面有用到的函数声明
如果要使用某个函数,先调用初始化函数(.h文件里面有它的声明),再调用你的功能函数
具体见下面的测试函数,这个是一个图像处理函数,哈哈
//MeterRecognizeDLL调用测试20180201
//采用mexFunction
void FuncMeterRecognizeDLL(cv::Mat I)
{
HINSTANCE h = LoadLibraryA("MeterRecognizeDLL.dll");
//DLL初始化函数
typedef bool(*pInitialize)(void);//初始化函数
pInitialize Initialize = NULL;
Initialize = (pInitialize)GetProcAddress(h, "MeterRecognizeDLLInitialize");//函数声明在.h文件里面
//MeterRecognizeDLL功能函数
typedef bool(*pmlx)(int nlhs, mxArray *plhs[], int nrhs, mxArray *prhs[]);//mlxMeterRecognizeDLL
函数声明在.h文件里面
pmlx mlxMeterRecognizeDLL = NULL;
mlxMeterRecognizeDLL = (pmlx)GetProcAddress(h, "mlxMeterRecognizeDLL");
if (!Initialize()) //初始化,必需要有,不要怀疑哦
{
// system("pause");
cout << "Could not initialize" << endl;
exit(0);
}
// I是opencv图像数据结构
mxArray *是matlab c++形式的矩阵指针。
如何转换呢?
先定义一个
mxArray *指针
int M = I.rows; //图像的行数
int N = I.cols; //图像的列数
mxArray * pmxArray = mxCreateDoubleArray(M,N,mxREAL);
double* prmxArray = mxGetPr(
pmxArray );
//I.data是opencv图像的头指针,prmxArray是matlab矩阵的头指针,使用memcpy将opencv图像数据复制到matlab矩阵当中
memcpy((double*)I.data,prmxArray ,sizeof(double)*M*N);
mxArray * ptArrayIn0 = cvMat2pMxArrayDouble(I);//输入
mxArray* ptArrayOut0 = mxCreateDoubleMatrix(1, 1, mxREAL);//输出
double *prOut0 = mxGetPr(ptArrayOut0);
*prOut0 = 0;
int nlhs = 1; //左边参数个数,即输出参数个数
mxArray *plhs[] = { ptArrayOut0 };//输出
int nrhs = 1; //右
边参数个数,即输入参数个数
mxArray*prhs[] = { ptArrayIn0 };//输入
mlxMeterRecognizeDLL(nlhs, plhs, nrhs, prhs);// 功能函数
输出结果在
plhs中
plhs[0]:mxArray* 类型,第一个输出参数,依次类推,plhs[1],第二个
比如,查看输出结果
double* prOut0 = mxGetPr(plhs[0]);
for (int i=0;i <M*N;i++)
cout<<*prOut0(i++)<<endl;
cv::Mat BW = pMxArrayDouble2cvMat(plhs[0]); //
}
整个过程中,可能会出现一些错误: