Matlab与C++混合编程,添加OpenCV库

 

 

  最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算、数值计算能力强大,但速度让人难以忍受。软件立刻移植到C++上又不太实际,故采用联合编程的方式,速度难以容忍的算法交给C++实现,C++在实现代码的过程中某些数值计算及图像处理算法调opencv库函数。

  在网上有很多matlab编写mex函数调用opencv库的方法,但都不能直接拿来。经过一步步试验,修改,最终完成,现将过程及内容记录下来留给后来人参考。

   第一步: (参考参考资料1,但其写的不够详细,对于初学者难懂)实现matlab、c++、opencv混合编程的编译器配置以及链接库的自动配置。参考资料2的方法虽然可行,但每次程序开启都要手动重新配置,很麻烦。

  其实就是写了一个cppmake.m文件,它的功能类似于Makefile,实际上就实现了mex编译这个工程时候的编译规则。具体可以看后面的代码,然后就知道在里面做了什么了。

 1 % This cppmake.m is for MATLAB
 2 % Function: compile c++ files which rely on OpenCV for Matlab using mex
 3 % Modified by Jessica
 4 % Date    : 2014-9-10
 5 % 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 this program): use "mex -setup" to choose your c/c++ compiler
10 clear all;
11 
12 % Get the architecture of this computer
13 is_64bit = strcmp(computer,'MACI64') || strcmp(computer,'GLNXA64') || strcmp(computer,'PCWIN64');
14 
15 
16 %----------------------------------------------------------------------------------------------  
17 %% The configuration of compiler
18 % You need to modify this configuration according to your own path of OpenCV  
19 % Notice: if your system is 64bit, your OpenCV must be 64bit! 
20 out_dir='./';  
21 CPPFLAGS = ' -O -DNDEBUG -I.\ -IF:\opencv\build\include -IF:\opencv\build\include\opencv -IF:\opencv\build\include\opencv2'; % your OpenCV "include" path  
22 LDFLAGS = ' -LF:\opencv\build\x86\vc10\lib';                       % your OpenCV "lib" path  
23 %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';
24 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';
25 if is_64bit  
26     CPPFLAGS = [CPPFLAGS ' -largeArrayDims'];  
27 end  
28 
29 % add your files here!!
30 compile_files = {
31     %the list of your code files which need to be compiled
32     'ImageCalibration.cpp'
33     };
34 %---------------------------------------------------------------------------------------------- 
35 
36 %---------------------------------------------------------------------------------------------- 
37 %% compiling
38 for k = 1 : length(compile_files)  
39     str = compile_files{k};  
40     fprintf('compilation of: %s\n', str);  
41     str = [str ' -outdir ' out_dir CPPFLAGS LDFLAGS LIBS];  
42     args = regexp(str, '\s+', 'split');  
43     mex(args{:});  
44 end 
45 fprintf('Congratulations, compilation successful!!!\n');
46 %---------------------------------------------------------------------------------------------- 
View Code

   注意:在调用此cppmake.m文件前要先选择编译器,使用mex -setup指令。

   第21行为配置opencv的include path,对于高版本的opencv有3个include,-I后面填入你电脑所装的opencv的include路径,多个路径之间使用空格符即' '各开。例如我电脑opencv装在F盘,故我的路径是F:\opencv\build\include。。。。。

  第22行为配置opencv的lib库的路径使用-L开头。第23、24行为添加opencv dll库的名字,-l开头,不同库之间用空格隔开。

  第25-27行,64位系统需要。

  在32行处加入你所要编译的mex函数的文件名称。ok,第一步完成,直接运行此m文件即可完成配置、编译。

  第二步:参考其他Matlab与C++混合编程的编写mex函数的方法即可。

   赶时间,先写到这,后面再补充。

四、参考资料

[1] Matlab与C++混合编程(依赖OpenCV库)

[2] matlab 调用opencv函数方法及matlab编译c++程序

[3] matlab用mex编译cpp文件

转载于:https://www.cnblogs.com/lukylu/p/3966871.html

你可能感兴趣的:(Matlab与C++混合编程,添加OpenCV库)