[轉] Matlab與C++混合編程,添加OpenCV庫

原文地址 峯迴璐轉html

最近在作運動醫學軟件優化工做,此款軟件框架及算法語言全由matlab實現,雖然matlab矩陣運算、數值計算能力強大,但速度讓人難以忍 受。軟件馬上移植到C++上又不太實際,故採用聯合編程的方式,速度難以容忍的算法交給C++實現,C++在實現代碼的過程當中某些數值計算及圖像處理算法 調opencv庫函數。c++

  在網上有不少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:\opencv\build\include -IF:\opencv\build\include\opencv -IF:\opencv\build\include\opencv2'; % your OpenCV "include" path  
LDFLAGS = ' -LF:\opencv\build\x86\vc10\lib';                       % 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\n', str);  
    str = [str ' -outdir ' out_dir CPPFLAGS LDFLAGS LIBS];  
    args = regexp(str, '\s+', 'split');  
    mex(args{:});  
end 
fprintf('Congratulations, compilation successful!!!\n');
%----------------------------------------------------------------------------------------------

 

   注意:在調用此cppmake.m文件前要先選擇編譯器,使用mex -setup指令。ide

   第21行爲配置opencv的include path,對於高版本的opencv有3個include,-I後面填入你電腦所裝的opencv的include路徑,多個路徑之間使用空格符即' '各開。例如我電腦opencv裝在F盤,故個人路徑是F:\opencv\build\include。。。。。函數

  第22行爲配置opencv的lib庫的路徑使用-L開頭。第2三、24行爲添加opencv dll庫的名字,-l開頭,不一樣庫之間用空格隔開。優化

  第25-27行,64位系統須要。ui

  在32行處加入你所要編譯的mex函數的文件名稱。ok,第一步完成,直接運行此m文件便可完成配置、編譯。this

  第二步:參考其餘Matlab與C++混合編程的編寫mex函數的方法便可。

   趕時間,先寫到這,後面再補充。

4、參考資料

[1] Matlab與C++混合編程(依賴OpenCV庫)

[2] matlab 調用opencv函數方法及matlab編譯c++程序

[3] matlab用mex編譯cpp文件

相關文章
相關標籤/搜索