本文使用的是matlab2012b、VS2010和OpenCV249
1、打开matlab
2、输入mex -setup
Welcome to mex -setup. This utility will help you set up a default compiler. For a list of supported compilers, see http://www.mathworks.com/support/compilers/R2012b/win64.html Please choose your compiler for building MEX-files: Would you like mex to locate installed compilers [y]/n?
输入:y
Select a compiler: [1] Microsoft Visual C++ 2010 in d:\Microsoft Visual Studio 10.0 [0] None Compiler:
输入:1
Please verify your choices: Compiler: Microsoft Visual C++ 2010 Location: d:\Microsoft Visual Studio 10.0 Are these correct [y]/n?
输入:y
*************************************************************************** Warning: MEX-files generated using Microsoft Visual C++ 2010 require that Microsoft Visual Studio 2010 run-time libraries be available on the computer they are run on. If you plan to redistribute your MEX-files to other MATLAB users, be sure that they have the run-time libraries. *************************************************************************** Trying to update options file: C:\Users\Administrator\AppData\Roaming\MathWorks\MATLAB\R2012b\mexopts.bat From template: D:\MATLAB\R2012b\bin\win64\mexopts\msvc100opts.bat Done . . . ************************************************************************** Warning: The MATLAB C and Fortran API has changed to support MATLAB variables with more than 2^32-1 elements. In the near future you will be required to update your code to utilize the new API. You can find more information about this at: http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html Building with the -largeArrayDims option enables the new API. **************************************************************************
3、mex -v
-> Default options filename found in C:\Users\Administrator\AppData\Roaming\MathWorks\MATLAB\R2012b ---------------------------------------------------------------- -> Options file = C:\Users\Administrator\AppData\Roaming\MathWorks\MATLAB\R2012b\mexopts.bat MATLAB = D:\MATLAB\R2012b -> COMPILER= cl -> Compiler flags: COMPFLAGS= /c /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE
/D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD OPTIMFLAGS = /O2 /Oy- /DNDEBUG DEBUGFLAGS = /Z7 arguments = Name switch = /Fo *****
*****
*****
4、选中 C:\Users\Administrator\AppData\Roaming\MathWorks\MATLAB\R2012b\mexopts.bat 右键 选择 open selection
打开mexopts.bat
修改以后红色部分
set INCLUDE=%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\ATLMFC\INCLUDE;%LINKERDIR%\include;%INCLUDE%;D:\software\opencv249\opencv\build\include;D:\software\opencv249\opencv\build\include\opencv;D:\software\opencv249\opencv\build\include\opencv2【添加opencv的include目录】
set LIB=%VCINSTALLDIR%\LIB\amd64;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%LINKERDIR%\lib\x64;%MATLAB%\extern\lib\win64;%LIB%;D:\software\opencv249\opencv\build\x64\vc10\lib【添加opencv的lib目录】
set LINKFLAGS=/dll /export:%ENTRYPOINT% /LIBPATH:"%LIBLOC%"opencv_ml249d.lib opencv_calib3d249d.lib opencv_contrib249d.lib opencv_core249d.lib opencv_features2d249d.lib opencv_flann249d.lib opencv_gpu249d.lib opencv_highgui249d.lib opencv_imgproc249d.lib opencv_legacy249d.lib opencv_objdetect249d.lib opencv_ts249d.lib opencv_video249d.lib opencv_nonfree249d.lib opencv_ocl249d.lib opencv_photo249d.lib opencv_stitching249d.lib opencv_superres249d.lib opencv_videostab249d.lib opencv_objdetect249.lib opencv_ts249.lib opencv_video249.lib opencv_nonfree249.lib opencv_ocl249.lib opencv_photo249.lib opencv_stitching249.lib opencv_superres249.lib opencv_videostab249.lib opencv_calib3d249.lib opencv_contrib249.lib opencv_core249.lib opencv_features2d249.lib opencv_flann249.lib opencv_gpu249.lib opencv_highgui249.lib opencv_imgproc249.lib opencv_legacy249.lib opencv_ml249.liblibmx.lib libmex.lib libmat.lib /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /manifest /incremental:NO /implib:"%LIB_NAME%.x" /MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map"【添加lib文件】
5、写一个demo,测试一下
test.cpp
#include "mex.h" #include <iostream> #include <stdio.h> #include <fstream> #include <opencv/cv.h> #include "opencv2/highgui/highgui.hpp" #include <opencv2/objdetect/objdetect.hpp> #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/ml/ml.hpp" using namespace cv; using namespace std; void mexFunction (int nlhs, mxArray *plhs[], // 输出参数个数,及输出参数数组 int nrhs, const mxArray *prhs[]) // 输入参数个数,及输入参数数组 { double scale = 1.3; char name[256]; int buflens =mxGetNumberOfElements(prhs[0]); mxGetString(prhs[0], name, buflens+1); if(!mxIsChar(prhs[0])) { mexErrMsgTxt("First parameter must be string/n"); } mexPrintf(name); IplImage * img = cvLoadImage(name, 1); if(img->imageData == NULL) { mexErrMsgTxt("Error in image/n"); } cvNamedWindow("1",1); //imshow("1",mat); cvShowImage("1",img); cvWaitKey(0); return; }
运行:mex test.cpp
mex('lena.jpg');
