Recently in the Exercise Medicine Software optimization work, this software framework and algorithm language is fully implemented by MATLAB, although the MATLAB matrix operations, numerical computing ability is powerful, but the speed is unbearable. Software immediately ported to C + + is not practical, so the use of joint programming, the speed of an intolerable algorithm to C + + implementation, C + + in the implementation of code in the process of some numerical calculation and image processing algorithm OPENCV library functions.
There are many ways to write the MEX function call OpenCV Library on the internet, but none of them can be taken directly. After a step-by-step test, modification, and final completion, the process and contents are recorded and left to Yimeimei for reference.
The first step: (Refer to reference 1, but it is not written in detail, for beginners difficult to understand) to achieve MATLAB, C + +, OPENCV mixed programming compiler configuration and the automatic configuration of the link library. The method of reference 2, though feasible, is cumbersome to manually reconfigure each time the program is opened.
In fact, it is written a cppmake.m file, its function is similar to makefile, in fact, the implementation of the MEX compile this project when the compilation rules. You can look at the code in the back and know what's going on inside.
1% this CPPMAKE.M is forMATLAB2% function:compile C + + files which rely on OpenCV forMatlabusingMex3%Modified by Jessica4% Date: the-9-Ten5% Homepage:http://www.cnblogs.com/lukylu/6%Email: [Email protected]7 8% Matlab and C + +mixed programming (dependent on OPENCV library)9% first step (before exeuting ThisProgram): Use"Mex-setup"To choose your/C + +compilerTen clear All; One A% Get The architecture of ThisComputer -Is_64bit = strcmp (computer,'MACI64') || strcmp (Computer,'GLNXA64') || strcmp (Computer,'PCWIN64'); - the -%---------------------------------------------------------------------------------------------- -%%The configuration of compiler -% need to modify Thisconfiguration According to your own path of OpenCV +% Notice:ifYour system is64bit, your OpenCV must be 64bit! -Out_dir='./'; +Cppflags ='-o-dndebug-i.\-if:\opencv\build\include-if:\opencv\build\include\opencv-if:\opencv\build\include\opencv2 '; % your OpenCV"include"Path ALdflags ='-lf:\opencv\build\x86\vc10\lib'; % your OpenCV"Lib"Path at%libs ='-LOPENCV_CALIB3D249D-LOPENCV_CONTRIB249D-LOPENCV_CORE249D-LOPENCV_FEATURES2D249D-LOPENCV_FLANN249D-LOPENCV _gpu249d-lopencv_highgui249d-lopencv_imgproc249d-lopencv_legacy249d-lopencv_ml249d-lopencv_nonfree249d-lopencv _objdetect249d-lopencv_photo249d-lopencv_stitching249d-lopencv_ts249d-lopencv_video249d-lopencv_videostab249d '; -LIBS ='-lopencv_calib3d249-lopencv_contrib249-lopencv_core249-lopencv_features2d249-lopencv_flann249-lopencv_ Gpu249-lopencv_highgui249-lopencv_imgproc249-lopencv_legacy249-lopencv_ml249-lopencv_nonfree249-lopencv_ objdetect249-lopencv_photo249-lopencv_stitching249-lopencv_ts249-lopencv_video249-lopencv_videostab249'; - ifIs_64bit -Cppflags = [Cppflags'-largearraydims']; - End - in% Add your files here!! -Compile_files = { to%The list of your code files which need to be compiled + 'ImageCalibration.cpp' - }; the%---------------------------------------------------------------------------------------------- * $%----------------------------------------------------------------------------------------------Panax Notoginseng%%compiling - forK =1: Length (compile_files) thestr =Compile_files{k}; +fprintf ('Compilation of:%s\n', str); Astr = [str'-outdir'out_dir cppflags ldflags LIBS]; theargs = RegExp (str,'\s+','Split'); + Mex (args{:}); - End $fprintf ('Congratulations, compilation successful!!! \ n'); $%----------------------------------------------------------------------------------------------
View Code
Note: Before calling this CPPMAKE.M file, select the compiler and use the MEX-SETUP directive.
The 21st behavior configuration OpenCV include path, for the high version of OpenCV there are 3 include,-i to fill in the OpenCV of your computer's include path, the use of spaces between the multiple paths is "open." For example, My Computer OpenCV installed on the F drive, so the path is F:\opencv\build\include ....
The 22nd behavior configures the path of the OpenCV Lib library to start with-L. 23rd, 24 behavior Add the name of the OpenCV DLL library, the-l start, separated by a space between the different libraries.
第25-27 line, 64-bit system required.
Add the file name of the MEX function you want to compile at line 32. OK, the first step is completed, run this m file directly to complete the configuration, compilation.
The second step: reference to other MATLAB and C + + mixed programming method of the MEX function can be.
Hurry up, write this first, and add it later.
Iv. references
[1] matlab and C + + mixed programming (dependent OPENCV library)
[2] Matlab call OpenCV function method and Matlab compile C + + program
[3] MATLAB compiled CPP file with MEX
MATLAB and C + + mixed programming, add OpenCV Library