原文地址 峰回璐转
最近在做运动医学软件优化工作,此款软件框架及算法语言全由matlab实现,虽然matlab矩阵运算、数值计算能力强大,但速度让人难以忍 受。软件立刻移植到C++上又不太实际,故采用联合编程的方式,速度难以容忍的算法交给C++实现,C++在实现代码的过程中某些数值计算及图像处理算法 调opencv库函数。
在网上有很多matlab编写mex函数调用opencv库的方法,但都不能直接拿来。经过一步步试验,修改,最终完成,现将过程及内容记录下来留给后来人参考。
第一步: (参考参考资料1,但其写的不够详细,对于初学者难懂)实现matlab、c++、opencv混合编程的编译器配置以及链接库的自动配置。参考资料2的方法虽然可行,但每次程序开启都要手动重新配置,很麻烦。
其实就是写了一个cppmake.m文件,它的功能类似于Makefile,实际上就实现了mex编译这个工程时候的编译规则。具体可以看后面的代码,然后就知道在里面做了什么了。
% This cppmake.m is for MATLAB % Function: compile c++ files which rely on OpenCV for Matlab using mex % Modified by Jessica % Date : 2014-9-10 % HomePage: http://www.cnblogs.com/lukylu/ % Email : wanglu@innomotion.biz % Matlab and C++ mixed programming(dependent on opencv library) % First step(before exeuting this program): use "mex -setup" to choose your c/c++ compiler clear all; % Get the architecture of this computer is_64bit = strcmp(computer,'MACI64') || strcmp(computer,'GLNXA64') || strcmp(computer,'PCWIN64'); %---------------------------------------------------------------------------------------------- %% The configuration of compiler % You need to modify this configuration according to your own path of OpenCV % Notice: if your system is 64bit, your OpenCV must be 64bit! out_dir='./'; CPPFLAGS = ' -O -DNDEBUG -I. -IF:opencvuildinclude -IF:opencvuildincludeopencv -IF:opencvuildincludeopencv2'; % your OpenCV "include" path LDFLAGS = ' -LF:opencvuildx86vc10lib'; % your OpenCV "lib" path %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'; if is_64bit CPPFLAGS = [CPPFLAGS ' -largeArrayDims']; end % add your files here!! compile_files = { %the list of your code files which need to be compiled 'ImageCalibration.cpp' }; %---------------------------------------------------------------------------------------------- %---------------------------------------------------------------------------------------------- %% compiling for k = 1 : length(compile_files) str = compile_files{k}; fprintf('compilation of: %s ', str); str = [str ' -outdir ' out_dir CPPFLAGS LDFLAGS LIBS]; args = regexp(str, 's+', 'split'); mex(args{:}); end fprintf('Congratulations, compilation successful!!! '); %----------------------------------------------------------------------------------------------
注意:在调用此cppmake.m文件前要先选择编译器,使用mex -setup指令。
第21行为配置opencv的include path,对于高版本的opencv有3个include,-I后面填入你电脑所装的opencv的include路径,多个路径之间使用空格符即' '各开。例如我电脑opencv装在F盘,故我的路径是F:opencvuildinclude。。。。。
第22行为配置opencv的lib库的路径使用-L开头。第23、24行为添加opencv dll库的名字,-l开头,不同库之间用空格隔开。
第25-27行,64位系统需要。
在32行处加入你所要编译的mex函数的文件名称。ok,第一步完成,直接运行此m文件即可完成配置、编译。
第二步:参考其他Matlab与C++混合编程的编写mex函数的方法即可。
赶时间,先写到这,后面再补充。
四、参考资料