Original address Baidu account aleasa123
1, first ensure that vs2010 can correctly call the OpenCV function,
2, matlab Select the compiler, the operation is as follows:
Open matlab2012, enter Mex–setup, the following prompt appears:
Welcome to Mex-setup. This utility the set up a default compiler. For a list of supported compilers, see Http://www.mathworks.com/support/compilers/R2012a/win32.htmlPlease Choose your Compiler for building mex-files:would I like MEX to locate installed compilers [y]/n? Enter Y, the following prompt appears: Select a compiler:[1] Lc C-win32 C 2.4.1 in c:\progra~1\matlab\r2012a\sys\lcc[2] Microsoft Visual C + + in C:\Program Files\Microsoft Visual Stu Dio 2010[0] Nonecompiler: Enter 2, the following prompt appears: Please verify your choices:Compiler:Microsoft Visual C + + Location:c:\program Fi Les\microsoft Visual Studio 2010Are These correct [y]/n? input y appears following a prompt indicating success: ********************************************* Warning:mex-files generated using Microsoft Visual C + + require that Micros OFT Visual Studio run-time libraries is available on the computer they is run on. If you plan to redistribute your mex-files to other MATLAB users, being sure that THey has the run-time libraries.*************************************************************************** Trying To update options file:c:\documents and Settings\zhangjing\application Data\mathworks\matlab\r2012a\mexopts.batfrom Template:c:\progra~1\matlab\r2012a\bin\win32\mexopts\msvc100opts.bat done ... **************************** warning:the matlab C and Fortran API have changed to support matlab Variables with more than 2^32-1 elements. In the the-the-future, you'll 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.htm L Building with THE-LARGEARRAYDIMS option enables the new api.************************************************* *************************
3. Configure Some directories
In MATLAB, enter MEX–V
The following will be shown:
-Default options filename found in C:\Documents and Settings\zhangjing\application data\mathworks\matlab\ R2012A-----------------------------> Options file = C:\Documents and settings\zhangjing\application data\m Athworks\matlab\r2012a\mexopts.bat MATLAB = c:\progra~1\matlab\r2012a-> COMPILER = cl-> Compiler flags:compflags =/C/GR/W3/EHS/D_CRT_SECURE_NO_DEPRECATE/D_SCL_SECURE_NO_DEP RECATE/D_SECURE_SCL=0/DMATLAB_MEX_FILE/NOLOGO/MD optimflags =/o2/oy-/dndebug debugflags =/z7 arguments = Name switch =/fo ..... (omitted here) Select the section above options file=, right-->open Selection, open Mexopts.bat Find the following section, add red part set Matlab=%matlab%set Vsinstalldir=c:\program Files\Microsoft Visual Studio 2010set Vcinstalldir=%vsinstalldir%\vcrem in the case, Linkerdir is being used to specify the location of the Sdkset linkerdir=c:\program Files\Microsoft SDKs\windows\v7.0aset path=%vcinstalldir%\bin;%vcinstalldir%\vcpackages;%vsinstalldir%\common7\ide;%vsinstalldir%\ Common7\tools;%linkerdir%\bin;%matlab_bin%;%path%set include=%vcinstalldir%\include;%vcinstalldir%\atlmfc\ Include;%linkerdir%\include;%i Nclude%;D: \opencv\include;d:\opencv\include\opencv;d:\opencv\include\opencv2set LIB=%VCINSTALLDIR%\LIB;% vcinstalldir%\atlmfc\lib;%linkerdir%\lib;%matlab%\extern\lib\win32;%lib%;D: \opencv\build\x86\vc10\libset MW_ Target_arch=win32 rem ********************************************************************rem Compiler Parametersrem ********************************************************************set COMPILER=clset COMPFLAGS=/c /gr/w3/ehs/d_crt_secure_no_deprecate/d_scl_secure_no_deprecate/d_secure_scl=0/dmatlab_mex_file/nologo/mdset optimflags=/o2/oy-/dndebugset debugflags=/z7set Name_object=/forem ********************************************** REM Linker Parametersrem ***********************************************Set Libloc=%matlab%\extern\lib\win32\microsoftset Linker=linkset linkflags=/dll/export:% entrypoint%/libpath: "%libloc%" Opencv_core231d.lib opencv_highgui231d.lib opencv_video231d.lib opencv_ml231d.lib Opencv_legacy231d.lib opencv_imgproc231d.lib opencv_flann231d.lib opencv_features2d231d.lib opencv_calib3d231d.lib Opencv_objdetect231d.lib opencv_contrib231d.lib libmx.lib libmex.lib libmat.lib/machine:x86 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" set LINKOPTIMFLAGS= (note the above include Lib to add a semicolon, linkflags at each Lib before the go home lattice)
In the current directory of MATLAB to build CPP file (Simple method, txt renamed to CPP), vs Open typing code, the code needs to be the C language interface of MATLAB, function inside call OpenCV function demonstration function, Simplified code: The program is named UseOpenCV.cpp save.
#include"mex.h"#include"cv.h"#include"highgui.h"usingNamespacecv;voidmexfunction (intNLHS, Mxarray *plhs[],//number of output parameters, and output parameter group intNRHS,ConstMxarray *prhs[])//number of input parameters and array of input parameters{ Charname[ the]; intBuflens =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;}
5 Enter the Mex useOpenCV.cpp compilation in MATLAB.
6 Enter a command in MATLAB to invoke the compiled file: Useopencv (' lena.bmp ');
[go] matlab call OPENCV function configuration