matlab调用opencv的函数

来源:互联网 发布:mv播放器软件 编辑:程序博客网 时间:2024/05/01 22:46
    由于工作需要,需要使用Matlab 中的mexFunction调用C++程序, 该 mex function里又要用到opencv中的lib,所以在编译mex的时候,需要将opencCv的lib加到matlab的环境中。
系统环境: win XP
matalb: matalb 2010a
c++ compiler: visual studio 2008

首先先设定matlab所选定的compiler:

1. matlab command: mex -setup

(C:\Users\xxx\AppData\Roaming\MathWorks\MATLAB\R2010a\mexopts.bat 为预设的的mex option file)

Please choose your compiler for building external interface (MEX) files:
Would you like mex to locate installed compilers [y]/n? y
Select a compiler:
[1] Microsoft Visual C++ 2008 SP1 in C:\Program Files (x86)\Microsoft Visual Studio 9.0
[0] None
Compiler: 1
Please verify your choices:
Compiler: Microsoft Visual C++ 2008 SP1
Location: C:\Program Files (x86)\Microsoft Visual Studio 9.0
Are these correct [y]/n? y
***************************************************************************
Warning: MEX-files generated using Microsoft Visual C++ 2008 require
that Microsoft Visual Studio 2008 run-time libraries be
available on the computer they are run on.
If you plan to redistribute your MEX-files to other MATLAB
users, be sure that they have the run-time libraries.
***************************************************************************

Trying to update options file: C:\Users\xxx\AppData\Roaming\MathWorks\MATLAB\R2010a\mexopts.bat
From template: C:\PROGRA~1\MATLAB\R2010a\bin\win64\mexopts\msvc90opts.bat
Done . . .
**************************************************************************

2. 修改mexopt.bat

        由于opencv所放置的路径为: D:\Program Files\OpenCV2.1\vc2008所以修改mexopts.bat(路径可以通过在Matlab中输入mex -v 得到),请参考,红色字体为添加和修改的部分:
set OPENCVDIR=D:\Program Files\OpenCV2.1\vc2008
set MATLAB=%MATLAB%
set VSINSTALLDIR=C:\Program Files (x86)\Microsoft Visual Studio 9.0
set VCINSTALLDIR=%VSINSTALLDIR%\VC
rem In this case, LINKERDIR is being used to specify the location of the SDK
set LINKERDIR=C:\Program Files\\Microsoft SDKs\Windows\v6.0A\
set PATH=%VCINSTALLDIR%\bin\amd64;%LINKERDIR%\bin\x64;%VCINSTALLDIR%\BIN;%VSINSTALLDIR%\Common7\Tools;%VSINSTALLDIR%\Common7\Tools\bin;%MATLAB_BIN%;%PATH%
set INCLUDE=%LINKERDIR%\include;%VCINSTALLDIR%\ATLMFC\INCLUDE;%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\VCPackages;%INCLUDE%;%OPENCVDIR%\include\opencv
set LIB=%LINKERDIR%\LIB\x64;%VCINSTALLDIR%\LIB\amd64;%VCINSTALLDIR%\ATLMFC\LIB\amd64;%MATLAB%\extern\lib\win64;%LIB%;%OPENCVDIR%\lib;%OPENCVDIR%\bin
...略...
set LINKFLAGS=/dll /export:%ENTRYPOINT% /LIBPATH:"%LIBLOC%" libmx.lib libmex.lib libmat.lib /MACHINE:X64 kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /incremental:NO /implib:"%LIB_NAME%.x" /MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map" cv210.lib HighGUI210.lib cxcore210.lib cv210d.lib HighGUI210d.lib cxcore210d.lib

3. build mex: mex xxx.cpp
 
转自:http://hi.baidu.com/ykaitao/item/4f2d4ad0b31760332b35c786,略有修改。