Matlab与C++混合编程(依赖OpenCV)
一、初级
mexAdd.cpp
- #include
- using
namespace std; -
- double
add(double x, double y) - {
-
return x + y; - }
1、修改代码文件
1)添加头文件mex.h
#include"mex.h"
2)添加接口函数mexFunction()
void mexFunction(int nlhs, mxArray *plhs[],int nrhs, const mxArray *prhs[])
{
}
mexAdd.cpp
- #include
"opencv2/opencv.hpp" - #include
"mex.h" -
- double
add(double x, double y) - {
-
return x + y; - }
-
-
- void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) - {
-
double *a; -
double b, c; -
plhs[0] = mxCreateDoubleMatrix(1, 1, mxREAL); -
a = mxGetPr(plhs[0]); -
b = *(mxGetPr(prhs[0])); -
c = *(mxGetPr(prhs[1])); -
*a = add(b, c); - }
2、编译修改后的c++文件
>> mex XXX.cpp
>> mex X1.cpp X2.cpp X3.cpp %多个cpp文件,且有依赖。生成的库名字叫X1
>> mex -O X1.cpp
>> mex -largeArrayDims X1.cpp %对64位系统,通过这个选项来指定使用处理大容量数组的API。因为Matlab与C++之间的接口是以32位系统作为标准的,这就导致了人们在处理大容量数据时没办法利用C和C++语言的速度优势。但对64位系统来说,系统资源一般都比32位系统要充足,所以指定该接口,让它对大容量数据处理更游刃有余。
二、进阶
- //
Interface: convert an image to gray and return to Matlab - //
Author : zouxy - //
Date : 2014-03-05 - //
HomePage : http://blog.csdn.net/zouxy09 - //
Email : zouxy09@qq.com -
- #include
"opencv2/opencv.hpp" - #include
"mex.h" -
- using
namespace cv; -
-
-
-
- void
exit_with_help() - {
-
mexPrintf( -
"Usage: [imageMatrix] = DenseTrack('imageFile.jpg');\n" -
); - }
-
- static
void fake_answer(mxArray *plhs[]) - {
-
plhs[0] = mxCreateDoubleMatrix(0, 0, mxREAL); - }
-
- void
RGB2Gray(char *filename, mxArray *plhs[]) - {
-
// read the image -
Mat image = imread(filename); -
if(image.empty()) { -
mexPrintf("can't open input file %s\n", filename); -
fake_answer(plhs); -
return; -
} -
-
// convert it to gray format -
Mat gray; -
if (image.channels() == 3) -
cvtColor(image, gray, CV_RGB2GRAY); -
else -
image.copyTo(gray); -
-
// convert the result to Matlab-supported format for returning -
int rows = gray.rows; -
int cols = gray.cols; -
plhs[0] = mxCreateDoubleMatrix(rows, cols, mxREAL); -
double *imgMat; -
imgMat = mxGetPr(plhs[0]); -
for (int i = 0; i < rows; i++) -
for (int j = 0; j < cols; j++) -
*(imgMat + i + j * rows) = (double)gray.at(i, j); -
-
return; - }
-
- void
mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) - {
-
if(nrhs == 1) -
{ -
char filename[256]; -
mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1); -
if(filename == NULL) -
{ -
mexPrintf("Error: filename is NULL\n"); -
exit_with_help(); -
return; -
} -
-
RGB2Gray(filename, plhs); -
} -
else -
{ -
exit_with_help(); -
fake_answer(plhs); -
return; -
} - }
- %//
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_64\include'; % your OpenCV "include" path - LDFLAGS
= ' -LD:\OpenCV_64\lib'; % your OpenCV "lib" path - LIBS
= ' -lopencv_core240 -lopencv_highgui240 -lopencv_video240 -lopencv_imgproc240'; - 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、编译
注:以上Matlab的说明都是在你的cpp文件所在目录下。