我是靠谱客的博主 喜悦镜子,最近开发中收集的这篇文章主要介绍matlab调用opencv函数的配置,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

环境:

VS2010 活动解决方案平台x64

WIN 8.1

Opencv 2.4.3

Matlab 2012a

 

 

1、  首先保证vs2010能正确调用opencv函数,

2、  Matlab中选择编译器,操作如下:

打开matlab2012,输入mex –setup,出现如下提示:

Welcome to mex -setup.  This utility will help you set up 

a default compiler.  For a list of supported compilers, see 

http://www.mathworks.com/support/compilers/R2012a/win32.html

Please choose your compiler for building MEX-files:

Would you like mex to locate installed compilers [y]/n?

输入y,出现如下提示:

Select a compiler:

[1] Lcc-win32 C2.4.1 in C:PROGRA~1MATLABR2012asyslcc

[2] Microsoft Visual C++ 2010 in C:Program FilesMicrosoft Visual Studio 2010

[0] None

Compiler:

输入2,出现如下提示:

Please verify your choices:

Compiler: Microsoft Visual C++ 2010 

Location: C:Program FilesMicrosoft Visual Studio 2010

Are these correct [y]/n?

输入y

出现如下提示,表明成功:

***************************************************************************

  Warning: MEX-files generated using Microsoft Visual C++ 2010 require

           that Microsoft Visual Studio 2010 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:Documents and SettingszhangjingApplication DataMathWorksMATLABR2012amexopts.bat

From template:              C:PROGRA~1MATLABR2012abinwin32mexoptsmsvc100opts.bat

 

Done . . .

 

**************************************************************************

  Warning: The MATLAB C and Fortran API has changed to support MATLAB

           variables with more than 2^32-1 elements.  In the near future

           you will be required to update your code to utilize the new

           API. You can find more information about this at:

           http://www.mathworks.com/help/techdoc/matlab_external/bsflnue-1.html

           Building with the -largeArrayDims option enables the new API.

**************************************************************************

3、  配置一些目录

在matlab中,输入mex –v

下面会有显示:

-> Default options filename found in C:Documents and SettingszhangjingApplication DataMathWorksMATLABR2012a

----------------------------

->    Options file           = C:Documents and SettingszhangjingApplication DataMathWorksMATLABR2012amexopts.bat

      MATLAB                 = C:PROGRA~1MATLABR2012A

->    COMPILER               = cl

->    Compiler flags:

         COMPFLAGS           = /c /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD

         OPTIMFLAGS          = /O2 /Oy- /DNDEBUG

         DEBUGFLAGS          = /Z7

         arguments           = 

         Name switch         = /Fo

。。。。。。。。。(此处省略一些)

选中上面Options file=后面的部分,右键->Open Selection,打开mexopts.bat

找到下面部分,增加红色的部分

set MATLAB=%MATLAB%

set VSINSTALLDIR=C:Program FilesMicrosoft Visual Studio 2010

set VCINSTALLDIR=%VSINSTALLDIR%VC

rem In this case, LINKERDIR is being used to specify the location of the SDK

set LINKERDIR=C:Program FilesMicrosoft SDKsWindowsv7.0A

set PATH=%VCINSTALLDIR%bin;%VCINSTALLDIR%VCPackages;%VSINSTALLDIR%Common7IDE;%VSINSTALLDIR%Common7Tools;%LINKERDIR%bin;%MATLAB_BIN%;%PATH%

set INCLUDE=%VCINSTALLDIR%INCLUDE;%VCINSTALLDIR%ATLMFCINCLUDE;%LINKERDIR%include;%INCLUDE%;D:opencvinclude;D:opencvincludeopencv;D:opencvincludeopencv2

PS:本人导入的包含如下

;D:Program Filesopencvmodulesworldinclude;D:Program Filesopencvmodulescoreinclude;D:Program Filesopencvmodulesvideostabinclude;D:Program Filesopencvmodulesvideoinclude;D:Program Filesopencvmodulestsinclude;D:Program Filesopencvmodulesstitchinginclude;D:Program Filesopencvmodulesphotoinclude;D:Program Filesopencvmodulesoclinclude;D:Program Filesopencvmodulesobjdetectinclude;D:Program;Filesopencvmodulesnonfreeinclude;D:Program Filesopencvincludeopencv2;D:Program Filesopencvincludeopencv;D:Program Filesopencvmodulesmlinclude;D:Program Filesopencvmoduleslegacyinclude;D:Program Filesopencvmodulesimgprocinclude;D:Program Filesopencvmoduleshighguiinclude;D:Program Filesopencvmodulesgpuinclude;D:Program Filesopencvmodulesflanninclude;D:Program Filesopencvmodulesfeatures2dinclude;D:Program Filesopencvinclude;D:Program Filesopencvmodulescontribinclude;D:Program Filesopencvmodulescalib3dinclude;D:Program Filesopencvmodulesandroidcamerainclude

 

set LIB=%VCINSTALLDIR%LIB;%VCINSTALLDIR%ATLMFCLIB;%LINKERDIR%lib;%MATLAB%externlibwin32;%LIB%;D:opencvbuildx86vc10lib

PS:本人导入的包含如下

;D:Program Filesopencvbuildx64vc10lib

set MW_TARGET_ARCH=win32

 

rem ********************************************************************

rem Compiler parameters

rem ********************************************************************

set COMPILER=cl

set COMPFLAGS=/c /GR /W3 /EHs /D_CRT_SECURE_NO_DEPRECATE /D_SCL_SECURE_NO_DEPRECATE /D_SECURE_SCL=0 /DMATLAB_MEX_FILE /nologo /MD

set OPTIMFLAGS=/O2 /Oy- /DNDEBUG

set DEBUGFLAGS=/Z7

set NAME_OBJECT=/Fo

 

rem ********************************************************************

rem Linker parameters

rem ********************************************************************

set LIBLOC=%MATLAB%externlibwin32microsoft

set LINKER=link

set LINKFLAGS=/dll /export:%ENTRYPOINT%

/LIBPATH:"%LIBLOC%" opencv_core231d.lib opencv_highgui231d.lib opencv_video231d.lib opencv_ml231d.lib opencv_legacy231d.lib opencv_imgproc231d.lib opencv_flann231d.lib opencv_features2d231d.lib opencv_calib3d231d.lib opencv_objdetect231d.lib opencv_contrib231d.lib libmx.lib libmex.lib libmat.lib /MACHINE:X86 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 /manifest /incremental:NO /implib:"%LIB_NAME%.x" /MAP:"%OUTDIR%%MEX_NAME%%MEX_EXT%.map"

set LINKOPTIMFLAGS=

(注意上面include lib处要加分号,LINKFLAGS处各个lib之前要回空格)

PS:本人导入的lib如下:

opencv_calib3d243d.lib opencv_contrib243d.lib opencv_core243d.lib opencv_features2d243d.lib opencv_flann243d.lib opencv_gpu243d.lib opencv_haartraining_engined.lib opencv_highgui243d.lib opencv_imgproc243d.lib opencv_legacy243d.lib opencv_ml243d.lib opencv_nonfree243d.lib opencv_objdetect243d.lib opencv_photo243d.lib opencv_stitching243d.lib opencv_ts243d.lib opencv_video243d.lib opencv_videostab243d.lib

 

 

在matlab当前目录下建立cpp文件(简单方法,txt改后缀为cpp),vs打开敲入代码,代码需要是Matlab的C语言接口风格,函数内部调用OpenCV函数演示功能,简化后的代码:程序命名为useOpenCV.cpp保存。

#include"mex.h"

#include     "cv.h"

#include    "highgui.h"

using namespacecv;

voidmexFunction (int nlhs, mxArray *plhs[], // 输出参数个数,及输出参数数组

    int nrhs, const mxArray *prhs[]) // 输入参数个数,及输入参数数组

{

    char name[256];

    int buflens =mxGetNumberOfElements(prhs[0]);

    mxGetString(prhs[0], name, buflens+1);

     if(!mxIsChar(prhs[0]))

     {

        mexErrMsgTxt("First parameter must be string/n");

     }

    mexPrintf(name);

    IplImage * img = cvLoadImage(name, 1);

     if(img->imageData == NULL)

     {

        mexErrMsgTxt("Error in image/n");

     }

    cvNamedWindow("1",1);

    //imshow("1",mat);

    cvShowImage("1",img);

    cvWaitKey(0);

    return;

}

5 在matlab里输入 mexuseOpenCV.cpp编译。

6 在matlab里输入命令调用编译好的文件:useOpenCV(‘lena.bmp’);即可调用。注意,lena.bmp图片放在当前文件下。

 

转载于:https://www.cnblogs.com/realkate1/p/4736169.html

最后

以上就是喜悦镜子为你收集整理的matlab调用opencv函数的配置的全部内容,希望文章能够帮你解决matlab调用opencv函数的配置所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(66)

评论列表共有 0 条评论

立即
投稿
返回
顶部