基于OpenCV的mex文件及其编译

6 篇文章 0 订阅
3 篇文章 0 订阅

之前的文章《mex文件及其编译》只是普通c的mex文件,如果需要OpenCV,怎么编译mex文件呢?

好在csdn有参考代码:http://blog.csdn.net/zouxy09/article/details/20553007

按照上述的参考代码可以完成工程的建立,更改cppmake.m如下:

%// This make.m is for MATLAB
%// Function: compile c++ files which rely on OpenCV for Matlab using mex
%// Author : zouxy
%// Date   : 2014-03-05
%// HomePage : http://blog.csdn.net/zouxy09
%// Email  : zouxy09@qq.com

%% Please modify your path of OpenCV
%% If your have any question, please contact Zou Xiaoyi

% Notice: first 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.\ -ID:\opencv\build\include\opencv -ID:\opencv\build\include'; % your OpenCV "include" path
LDFLAGS = ' -LD:\opencv\build\x64\vc12\lib';					   % your OpenCV "lib" path
LIBS = ' -lopencv_core249d -lopencv_highgui249d -lopencv_video249d -lopencv_imgproc249d';
if is_64bit
	CPPFLAGS = [CPPFLAGS ' -largeArrayDims'];
end
%% add your files here!
compile_files = { 
	% the list of your code files which need to be compiled
	'RGB2Gray.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');

错误1:运行cppmake.m,得到错误消息如下:

D:\opencv\build\include\opencv\cv.h(63) : fatal error C1083: 无法打开包括文件: “opencv2/core/core_c.h”: No such file or directory

原因是cv.h中包含了core_c.h:

#include "opencv2/core/core_c.h"

而core_c.h的位置是D:\opencv\build\include\opencv2\core,但我们在cppmake.m中设置的是D:\opencv\build\include\opencv,于是编译器找不到它。在cppmake.m中增加-ID:\opencv\build\include(而不是-ID:\opencv\build\include\opencv2,因为cv.h中已经包含“opencv2/”了)。


错误2:

重新执行cppmake.m,得到错误消息如下:

RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "void __cdecl cv::fastFree(void *)" (?fastFree@cv@@YAXPEAX@Z),该符号在函数 "public: __cdecl
cv::Mat::~Mat(void)" (??1Mat@cv@@QEAA@XZ) 中被引用
RGB2Gray.obj : error LNK2019: 无法解析的外部符号 "public: __cdecl cv::_InputArray::_InputArray(class cv::Mat const &)"
(??0_InputArray@cv@@QEAA@AEBVMat@1@@Z),该符号在函数 "void __cdecl RGB2Gray(char *,struct mxArray_tag * * const)"
(?RGB2Gray@@YAXPEADQEAPEAUmxArray_tag@@@Z) 中被引用
....
分析原因,错误发生在LINK阶段,因此一定是链接库的问题。考虑到系统是64位系统,将LDFLAGS语句中的"x86"改为“ x64":

LDFLAGS = ' -LD:\opencv\build\x64\vc12\lib';					   % your OpenCV "lib" path

其中的vc12就表示mex -setup中的编译器VS2013。

重新执行cppmake.m,成功!

compilation of: RGB2Gray.cpp
使用 'Microsoft Visual C++ 2013 Professional' 编译。
MEX 已成功完成。
Congratulations, compilation successful!!!






  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值