[转] matlab调用opencv函数的配置
原文地址百度账户 aleasa123
方式1
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 C 2.4.1 in C:\PROGRA~1\MATLAB\R2012a\sys\lcc
[2] Microsoft Visual C++ 2010 in C:\Program Files\Microsoft Visual Studio 2010
[0] None Compiler:
输入2,出现如下提示:
Please verify your choices:
Compiler: Microsoft Visual C++ 2010
Location: C:\Program Files\Microsoft 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 Settings\zhangjing\Application Data\MathWorks\MATLAB\R2012a\mexopts.bat
From template: C:\PROGRA~1\MATLAB\R2012a\bin\win32\mexopts\msvc100opts.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 Settings\zhangjing\Application Data\MathWorks\MATLAB\R2012a
----------------------------
-> Options file = C:\Documents and Settings\zhangjing\Application Data\MathWorks\MATLAB\R2012a\mexopts.bat
MATLAB = C:\PROGRA~1\MATLAB\R2012A
-> 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 Files\Microsoft 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 Files\Microsoft SDKs\Windows\v7.0A\
set PATH=%VCINSTALLDIR%\bin;%VCINSTALLDIR%\VCPackages;%VSINSTALLDIR%\Common7\IDE;%VSINSTALLDIR%\Common7\Tools;%LINKERDIR%\bin;%MATLAB_BIN%;%PATH%
set INCLUDE=%VCINSTALLDIR%\INCLUDE;%VCINSTALLDIR%\ATLMFC\INCLUDE;%LINKERDIR%\include;%INCLUDE%;D:\opencv\include;D:\opencv\include\opencv;D:\opencv\include\opencv2
set LIB=%VCINSTALLDIR%\LIB;%VCINSTALLDIR%\ATLMFC\LIB;%LINKERDIR%\lib;%MATLAB%\extern\lib\win32;%LIB%;D:\opencv\build\x86\vc10\lib
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%\extern\lib\win32\microsoft
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之前要回空格)
在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[];
int buflens =mxGetNumberOfElements(prhs[]);
mxGetString(prhs[], name, buflens+);
if(!mxIsChar(prhs[]))
{
mexErrMsgTxt("First parameter must be string/n");
}
mexPrintf(name);
IplImage * img = cvLoadImage(name, );
if(img->imageData == NULL)
{
mexErrMsgTxt("Error in image/n");
}
cvNamedWindow("",);
//imshow("1",mat);
cvShowImage("",img);
cvWaitKey();
return; }
5 在matlab里输入 mex useOpenCV.cpp编译。
6 在matlab里输入命令调用编译好的文件:useOpenCV(‘lena.bmp’);即可调用。
方式2
利用Opencv2.4.3中的头文件和库文件
在matlab中利用mex命令编译C/C++文件时,可以利用Opencv2.4.3的头文件和库文件中已经定义的函数和类等模块。
(1) 对于头文件中已经定义的函数和类等模块时,需要在Opencv2.4.3的安装目录下找到对
应的头文件(*.h或*.hpp),并将其拷贝至mex命令所要编译的文件相同的目录下,并在C/C++文件中“#include”这个头文件,并将这个头文件中所涉及到的
“F:\opencv\build\include\opencv2”目录下的所有文件也拷贝至mex命令所要编译的文件相同的目录下。“ F:\opencv”为Opencv2.4.3的安装目录。
(2) 对于库文件中已经定义的函数和类等模块时,在C/C++文件中包含这个库文件,语句为
#pragma comment(lib, "opencv_calib3d243d.lib")
#pragma comment(lib, "opencv_contrib243d.lib")
#pragma comment(lib, "opencv_core243d.lib")
#pragma comment(lib, "opencv_features2d243d.lib")
#pragma comment(lib, "opencv_flann243d.lib")
#pragma comment(lib, "opencv_gpu243d.lib")
#pragma comment(lib, "opencv_highgui243d.lib")
#pragma comment(lib, "opencv_imgproc243d.lib")
#pragma comment(lib, "opencv_legacy243d.lib")
#pragma comment(lib, "opencv_ml243d.lib")
#pragma comment(lib, "opencv_objdetect243d.lib")
#pragma comment(lib, "opencv_ts243d.lib")
#pragma comment(lib, "opencv_video243d.lib")
并将上述所有的库文件从opencv的安装目录下找到拷贝至mex命令所要编译的文件相同的目
录下。
(3) 利用mex命令编译“*.cpp”文件就可获得“*.mexw64”文件。
(4) 例子
#include "mex.h" #include "cv.h"
//#include "highgui.h" #include "highgui.h"
#pragma comment(lib, "opencv_calib3d243d.lib")
#pragma comment(lib, "opencv_contrib243d.lib")
#pragma comment(lib, "opencv_core243d.lib")
#pragma comment(lib, "opencv_features2d243d.lib")
#pragma comment(lib, "opencv_flann243d.lib")
#pragma comment(lib, "opencv_gpu243d.lib")
#pragma comment(lib, "opencv_highgui243d.lib")
#pragma comment(lib, "opencv_imgproc243d.lib")
#pragma comment(lib, "opencv_legacy243d.lib")
#pragma comment(lib, "opencv_ml243d.lib")
#pragma comment(lib, "opencv_objdetect243d.lib")
#pragma comment(lib, "opencv_ts243d.lib")
#pragma comment(lib, "opencv_video243d.lib") IplImage *grey1 = , *grey0 = , *pyramid1 = , *pyramid0 = ;
int win_size = ;
CvPoint2D32f* points[] = {,}; void loadImageFromMatlab(const mxArray *mxImage, IplImage *image)
{
unsigned char *values = (unsigned char *) mxGetPr(mxImage); int widthStep = image‐>widthStep; int N = mxGetN(mxImage); // width
int M = mxGetM(mxImage); // height for(int i=;i<N;i++)
for(int j=;j<M;j++)
image‐>imageData[j*widthStep+i] = values[j+i*M];
} void mexFunction(int plhs_size, mxArray *plhs[], int prhs_size, const mxArray *prhs[])
{
// Load images if (prhs_size ==)
{
win_size = *mxGetPr(prhs[]);
} int N = mxGetN(prhs[]);
int M = mxGetM(prhs[]);
grey0 = cvCreateImage( cvSize(N, M), , );
grey1 = cvCreateImage( cvSize(N, M), , ); loadImageFromMatlab(prhs[],grey0);
loadImageFromMatlab(prhs[],grey1); // Load feature points
double *fp = mxGetPr(prhs[]);
int num_pts = mxGetN(prhs[]);
points[] = (CvPoint2D32f*)cvAlloc(num_pts*sizeof(points[][]));
points[] = (CvPoint2D32f*)cvAlloc(num_pts*sizeof(points[][])); char *status = (char*)cvAlloc(num_pts);
float *error = (float*) cvAlloc(num_pts*sizeof(float)); for (int i = ; i < num_pts; i++)
{
points[][i].x = fp[*i];
points[][i].y = fp[*i+];
} // neni treba, urychleni z fpt 40 ‐> fps 200
//cvFindCornerSubPix( grey0, points[0], num_pts, cvSize(win_size,win_size), cvSize(‐1,‐1), cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03));
pyramid1 = cvCreateImage( cvGetSize(grey1), , );
pyramid0 = cvCreateImage( cvGetSize(grey1), , ); cvCalcOpticalFlowPyrLK( grey0, grey1, pyramid0, pyramid1, points[], points[], num_pts, cvSize(win_size,win_size), , status, error, cvTermCriteria(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,,0.03), );
// Output plhs[] = mxCreateDoubleMatrix(, num_pts, mxREAL); double *output = mxGetPr(plhs[]);
for (int i = ; i < num_pts; i++)
{
output[*i] = (double) points[][i].x;
output[*i+] = (double) points[][i].y;
output[*i+] = (double) points[][i].x;
output[*i+] = (double) points[][i].y;
output[*i+] = (double) error[i];
output[*i+] = (double) status[i];
//output[5*i+5] = (double) error[i];
} // Tidy up
cvReleaseImage( &pyramid0 );
cvReleaseImage( &pyramid1 );
cvReleaseImage( &grey0 );
cvReleaseImage( &grey1 ); return; }
[转] matlab调用opencv函数的配置的更多相关文章
- matlab调用opencv函数的配置
环境: VS2010 活动解决方案平台x64 WIN 8.1 Opencv 2.4.3 Matlab 2012a 1. 首先保证vs2010能正确调用opencv函数, 2. Matlab中选择编 ...
- 【kate总结】matlab调用opencv总结
正常情况下,编写好matlab调用opencv的代码. 1.输入 MEX XX.CPP(所有的mex都要编译) 2.将生成的.mexw64 放到要调用的文件夹下即可 出错总结: 本人写的matla ...
- csharp通过dll调用opencv函数,图片作为参数
[blog 项目实战派]csharp通过dll调用opencv函数,图片作为参数 一直想做着方面的研究,但是因为这个方面的知识过于小众,也是由于自己找资料的能力比较弱,知道今天才找 ...
- OpenCV安装与配置
本文使用OpenCV2.48在win10平台下操作. 一,关于OpenCV OpenCV是开源C++计算机可视化处理库,它集成了很多计算机图形图像处理的功能.还有机器学习,字符识别,人脸识别,物体检测 ...
- matlab和C/C++混合编程--调用opencv
最近的我们已经将整个项目搭起来了,项目比较复杂.由于我们做的是检索系统,所以我们用asp.net(c#)做了网页,但是算法的实现是在matlab下,所以我们不得不用matlab生成动态链接库dll,然 ...
- 【原创】Matlab.NET混合编程技巧之直接调用Matlab内置函数
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 在我的上一篇文章[ ...
- 向量和矩阵的范数及MATLAB调用函数
范数就是长度的一种推广形式,数学语言叫一种度量.比如有一个平面向量,有两个分量来描述:横坐标和纵坐标.向量的二范数就是欧几里得意义下的这个向量的长度.还有一些诸如极大值范数,就是横坐标或者纵坐标的最大 ...
- Matlab.NET混合编程技巧之——直接调用Matlab内置函数(附源码)
原文:[原创]Matlab.NET混合编程技巧之--直接调用Matlab内置函数(附源码) 在我的上一篇文章[原创]Matlab.NET混编技巧之——找出Matlab内置函数中,已经大概的介绍了mat ...
- opencv的resize和matlab的imresize函数的计算
在用c++代码复现matlab代码时,遇到两者resize函数的结果不相同的问题. opencv: resize(image1, reTmp, Size(, ), , , cv::INTER_LINE ...
随机推荐
- HDU1869---(最短路+floyd)
http://acm.hdu.edu.cn/showproblem.php?pid=1869 思路:最短路+floyd 分析:1 题目是要求所有的数据能否满足“六度分离”,那么我们就想到所有点之间的最 ...
- HDU2177取(2堆)石子游戏---(威佐夫博弈)
http://acm.hdu.edu.cn/showproblem.php?pid=2177 取(2堆)石子游戏 Time Limit: 3000/1000 MS (Java/Others) M ...
- Web.xml过滤器配置及执行顺序概念
第一个过滤器 @Overridepublic void doFilter(ServletRequest request, ServletResponse response,FilterChain ch ...
- USACO_1.1_Greedy_Gift_Givers_(模拟+水题)
描述 http://train.usaco.org/usacoprob2?a=y0SKxY0Kc2q&S=gift1 给出不超过$10$个人,每个人拿出一定数量的钱平分给特定的人,求最后每个人 ...
- HDU1003MAX SUM (动态规划求最大子序列的和)
Max Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
- python基础===socket模块的讲解(转)
一.网络知识的一些介绍 socket 是网络连接端点.例如当你的Web浏览器请求www.jb51.net上的主页时,你的Web浏览器创建一个socket并命令它去连接 www.jb51.net的Web ...
- 如何在xcode中启用xib方式做应用
使用storybord的好处和缺点这里不一一说,但是很多人可能习惯xib的方式进行应用的开发. 在xcode环境下可参考如下链接进行设定. http://codefromabove.com/2014/ ...
- go语言的定时器
package main import ( "fmt" "time" ) func main(){ //创建一个定时器,时间为2s,2s过后会自动往通道里面写入 ...
- python模块之copy
提供浅拷贝和深拷贝两种模式. =>copy(x):返回x的浅拷贝 =>deepcopy(x):返回x的深拷贝 浅拷贝和深拷贝: 浅拷贝复制不变对象,引用可变对象(如列表和字典): 深拷贝复 ...
- iptables 用法及常用模块总结
iptables传输数据包的过程: 1. 当一个数据包进入网卡时,它首先进入PREROUTING链,内核根据数据包目的IP判断是否需要转送出去. 2. 如果数据包就是进入本机的,它就会沿着图向下移动, ...