最近在做運動醫學軟體優化工作,此款軟體架構及算法語言全由matlab實作,雖然matlab矩陣運算、數值計算能力強大,但速度讓人難以忍 受。軟體立刻移植到C++上又不太實際,故采用聯合程式設計的方式,速度難以容忍的算法交給C++實作,C++在實作代碼的過程中某些數值計算及圖像處理算法 調opencv庫函數。
在網上有很多matlab編寫mex函數調用opencv庫的方法,但都不能直接拿來。經過一步步試驗,修改,最終完成,現将過程及内容記錄下來留給後來人參考。
第一步: (參考參考資料1,但其寫的不夠詳細,對于初學者難懂)實作matlab、c++、opencv混合程式設計的編譯器配置以及連結庫的自動配置。參考資料2的方法雖然可行,但每次程式開啟都要手動重新配置,很麻煩。
其實就是寫了一個cppmake.m檔案,它的功能類似于Makefile,實際上就實作了mex編譯這個工程時候的編譯規則。具體可以看後面的代碼,然後就知道在裡面做了什麼了。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<code>% This cppmake.m is for MATLAB</code>
<code>% Function: compile c++ files which rely on OpenCV for Matlab using mex</code>
<code>% Modified by Jessica</code>
<code>% Date : 2014-9-10</code>
<code>% HomePage: http://www.cnblogs.com/lukylu/</code>
<code>% Email : [email protected]</code>
<code>% Matlab and C++ mixed programming(dependent on opencv library)</code>
<code>% First step(before exeuting this program): use "mex -setup" to choose your c/c++ compiler</code>
<code>clear</code> <code>all</code><code>;</code>
<code>% Get the architecture of this computer</code>
<code>is_64bit = </code><code>strcmp</code><code>(</code><code>computer</code><code>,</code><code>'MACI64'</code><code>) || </code><code>strcmp</code><code>(</code><code>computer</code><code>,</code><code>'GLNXA64'</code><code>) || </code><code>strcmp</code><code>(</code><code>computer</code><code>,</code><code>'PCWIN64'</code><code>);</code>
<code>%---------------------------------------------------------------------------------------------- </code>
<code>%% The configuration of compiler</code>
<code>% You need to modify this configuration according to your own path of OpenCV </code>
<code>% Notice: if your system is 64bit, your OpenCV must be 64bit!</code>
<code>out_dir=</code><code>'./'</code><code>; </code>
<code>CPPFLAGS = </code><code>' -O -DNDEBUG -I.\ -IF:\opencv\build\include -IF:\opencv\build\include\opencv -IF:\opencv\build\include\opencv2'</code><code>; </code><code>% your OpenCV "include" path </code>
<code>LDFLAGS = </code><code>' -LF:\opencv\build\x86\vc10\lib'</code><code>; </code><code>% your OpenCV "lib" path </code>
<code>%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';</code>
<code>LIBS = </code><code>' -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'</code><code>;</code>
<code>if</code> <code>is_64bit </code>
<code> </code><code>CPPFLAGS = [CPPFLAGS </code><code>' -largeArrayDims'</code><code>]; </code>
<code>end</code>
<code>% add your files here!!</code>
<code>compile_files = {</code>
<code> </code><code>%the list of your code files which need to be compiled</code>
<code> </code><code>'ImageCalibration.cpp'</code>
<code> </code><code>};</code>
<code>%----------------------------------------------------------------------------------------------</code>
<code>%% compiling</code>
<code>for</code> <code>k = 1 : </code><code>length</code><code>(compile_files) </code>
<code> </code><code>str = compile_files{k}; </code>
<code> </code><code>fprintf</code><code>(</code><code>'compilation of: %s\n'</code><code>, str); </code>
<code> </code><code>str = [str </code><code>' -outdir '</code> <code>out_dir CPPFLAGS LDFLAGS LIBS]; </code>
<code> </code><code>args = </code><code>regexp</code><code>(str, </code><code>'\s+'</code><code>, </code><code>'split'</code><code>); </code>
<code> </code><code>mex</code><code>(args{:}); </code>
<code>end</code>
<code>fprintf</code><code>(</code><code>'Congratulations, compilation successful!!!\n'</code><code>);</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函數的方法即可。
趕時間,先寫到這,後面再補充。
四、參考資料
沒有整理與歸納的知識,一文不值!高度概括與梳理的知識,才是自己真正的知識與技能。 永遠不要讓自己的自由、好奇、充滿創造力的想法被現實的架構所束縛,讓創造力自由成長吧! 多花時間,關心他(她)人,正如别人所關心你的。理想的騰飛與實作,沒有别人的支援與幫助,是萬萬不能的。
本文轉自wenglabs部落格園部落格,原文連結:http://www.cnblogs.com/arxive/p/5154406.html,如需轉載請自行聯系原作者