Matlab和C++混合编程
来源:互联网 发布:一战沙俄 知乎 编辑:程序博客网 时间:2024/06/07 00:02
1 利用 mexopencv 实现各种数据的转换
int i = MxArray(prhs[0]).toInt();double d = MxArray(prhs[0]).toDouble();bool b = MxArray(prhs[0]).toBool();std::string s = MxArray(prhs[0]).toString();cv::Mat mat = MxArray(prhs[0]).toMat(); // For pixelscv::Mat ndmat = MxArray(prhs[0]).toMatND(); // For N-D arraycv::Point pt = MxArray(prhs[0]).toPoint();cv::Size siz = MxArray(prhs[0]).toSize();cv::Rect rct = MxArray(prhs[0]).toRect();cv::Scalar sc = MxArray(prhs[0]).toScalar();cv::SparseMat s = MxArray(prhs[0]).toSparseMat(); // Only double to floatplhs[0] = MxArray(i);plhs[0] = MxArray(d);plhs[0] = MxArray(b);plhs[0] = MxArray(s);plhs[0] = MxArray(mat);plhs[0] = MxArray(ndmat);plhs[0] = MxArray(pt);plhs[0] = MxArray(siz);plhs[0] = MxArray(rct);plhs[0] = MxArray(sc);plhs[0] = MxArray(sp); // Only 2D float to double
配置,进入mexopencv-2.4根目录下,运行:
mexopencv.make('opencv_path','C:\OpenCV2.4\build','debug',true)
具体可以参考+mexopencv文件下的:make.m文件
2 Matlab struct/cell to C++
例1 :for struct:
matlab.m 文件中:
options.name=’xlh’;options.age=25;options.arr=[10 20 30;5 6 7];
调用函数:
res=mex_function(options);
mex_function.cpp 文件中:
/** pm [index].fieldname,因此,options 是一个 1*1 的结构体,故下面的函数中第二个参数为 0* 其中 pm 是 mxArray *类型。*/mxArray *mx_name=mxGetField(prhs[0],0,’name’);//下面用两个方法获得 name 中的’xlh’://for mexopencvstring str_name=MxArray(mx_name).toString();//for mxchar * str_name= mxArrayToString(mx_name);
例2:for cell:
matlab.m 文件中:
opt_param.table{1,1}=[1 2 3 4 5];opt_param.table{1,2}=[6 7 8 9 10];opt_param.table{2,1}=[11 12 13 14 15];opt_param.table{2,2}=[16 17 18 19 20];
调用函数:
res=mex_function(options);
mex_function.cpp 文件中:
mxArray *table=mxGetField(prhs[0],0,’table’); //获取 table 元胞。mxArray *elem=mxGetCell(table,1);//获取 table 元胞中的第二个元素,因为 matlab 是按列存储的,mxGetCell 函数中的 1,表示索引, 多维数组的索引计算参考 matlab 帮助文档中。Mat mat=MxArray(elem).toMat();
因此:mat 的结果为:[11 12 13 14 15]。注意输出数据安装 mat.at输出,因为 matlab 中
的数字的存储默认为 double 类型。
3 C++ to Matlab struct/cell
matlab.m 文件中:
table=mex_function(img);//其中 img 是一个图像.
mex_cpp 文件中;
我们的目的是想实现 C++中的 plhs[0]( 即输出参数) 传到 matlab 有如下的形式(即元胞的
形式):
table{1,1}=feature_vector
table{1,2}= feature_vector
…
table{n,n}=feature_vector;
其中 feature_vector 是一个列矢量,也可以是一个图像矩阵。
mex_cpp 文件中:
mxArray * mx_cell=mxCreateCellMatrix(2,2);//创建2*2大小的cellplhs[0]=mx_cell;//使输出指向这个cellMat feature1=(Mat_<double>(1,5)<<1,2,3,4,5);Mat feature2=(Mat_<double>(1,5)<<6,7,8,9,10);Mat feature3=(Mat_<double>(1,5)<<11,12,13,14,15);Mat feature4=(Mat_<double>(1,5)<<16,17,18,19,20);mxArray * mx_feat1=MxArray(feature1);//使用mexopencv实现,也可以使用mx函数实现。mxArray * mx_feat2=MxArray(feature2);mxArray * mx_feat3=MxArray(feature3);mxArray * mx_feat4=MxArray(feature4);mxSetCell(plhs[0],0,mx_feat1);mxSetCell(plhs[0],1,mx_feat2);mxSetCell(plhs[0],2,mx_feat3);mxSetCell(plhs[0],3,mx_feat4);
注释:
Matlab和C++的接口信息都可以通过下面的方式查询:
matlab->Advanced Software Development->MATLAB API for Other Languages->C/C++ Matrix Libray API
0 0
- Matlab和C混合编程
- c和matlab混合编程
- C和MATLAB混合编程
- matlab 和c混合编程-------------------基础
- Matlab 和 C/C++混合编程
- matlab和C/C++混合编程--Mex
- matlab和C/C++混合编程--Mex
- Matlab和C混合编程举例
- 小记Matlab和C混合编程
- matlab和C/C++混合编程--Mex
- matlab和C/C++混合编程--Mex
- MATLAB和C语言混合编程问题
- Matlab C混合编程
- matlab C混合编程
- Matlab C混合编程
- matlab/C 混合编程
- matlab与C混合编程
- matlab与c混合编程
- 解决react-native 创建工程、init过程太慢的问题
- iOS中使用blend改变图片颜色
- 青年图摘0714!唯美背后
- Map输出方式二:foreach输出Map
- MySQL存储过程中的3种循环
- Matlab和C++混合编程
- POJ 1679 The Unique MST
- DataTable跳转到指定页
- 帝国cms会员查看权限设置方法
- 设计模式--原型模式
- 如何启动、关闭和设置ubuntu防火墙
- 【牛腩新闻发布系统】-HTTP错误 403.14 –Forbidden Web服务器被配置为不列出此目录的内容
- EXT4 form 表单内控件输入无效时不出现提示的原因
- scp命令的用法详解