将数据从OpenCV矩阵发送到Matlab Engine,C++
Sending data from OpenCV matrix to Matlab Engine, C++
我正在使用C++和Matlab Engine将数据从OpenCV矩阵发送到matlab。我试图从列专业转换为行专业,但我对如何做到这一点感到非常困惑。我不明白如何处理 Matlab 指针 mxArray 并将数据放入引擎。
有没有人使用 OpenCV 和 matlab 来发送矩阵?我没有找到太多信息,我认为这是一个非常有趣的工具。欢迎任何帮助。
如果您创建了 matlab 引擎,我有一个函数可以工作。我所做的是为 matlab 引擎创建一个 SingleTone 模板:
我的标题如下所示:
/** Singletone class definition
*
*/
class MatlabWrapper
{
private:
static MatlabWrapper *_theInstance; ///< Private instance of the class
MatlabWrapper(){} ///< Private Constructor
static Engine *eng;
public:
static MatlabWrapper *getInstance() ///< Get Instance public method
{
if(!_theInstance) _theInstance = new MatlabWrapper(); ///< If instance=NULL, create it
return _theInstance; ///< If instance exists, return instance
}
public:
static void openEngine(); ///< Starts matlab engine.
static void cvLoadMatrixToMatlab(const Mat& m, string name);
};
我的 cpp:
#include <iostream>
using namespace std;
MatlabWrapper *MatlabWrapper::_theInstance = NULL; ///< Initialize instance as NULL
Engine *MatlabWrapper::eng=NULL;
void MatlabWrapper::openEngine()
{
if (!(eng = engOpen(NULL)))
{
cerr << "Can't start MATLAB engine" << endl;
exit(-1);
}
}
void MatlabWrapper::cvLoadMatrixToMatlab(const Mat& m, const string name)
{
int rows=m.rows;
int cols=m.cols;
string text;
mxArray *T=mxCreateDoubleMatrix(cols, rows, mxREAL);
memcpy((char*)mxGetPr(T), (char*)m.data, rows*cols*sizeof(double));
engPutVariable(eng, name.c_str(), T);
text = name + "=" + name + "'"; // Column major to row major
engEvalString(eng, text.c_str());
mxDestroyArray(T);
}
例如,当您想要发送矩阵时
Mat A = Mat::zeros(13, 1, CV_32FC1);
就这么简单:
MatlabWrapper::getInstance()->cvLoadMatrixToMatlab(A,"A");
相关文章:
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 有可能在Armadillo中复制MATLAB circshift方法吗
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- MATLAB to C++: csvread() not supported by MATLAB Coder
- 加载由 MATLAB Coder 生成的带有函数的 DLL,该函数调用外部函数
- MATLAB:跟踪imufilter对象中的状态变化
- 如何将 c++ 中的客户端 TCP 的替身列表发送到 Matlab 中的 TCP 服务器?
- 将三角函数的正确值与matlab进行比较
- 连接 MATLAB 和 Visual Studios 的问题
- 如何将C++连接到 Matlab
- 如何发送 Mat H=findHomography 返回 Matlab
- 如何在C++中编写 MATLAB fix(X) 函数?
- 提供变量作为 MATLAB 系统命令的输入参数,以便C++可执行文件
- 有没有更好的方法可以使用特征/C++实现 matlab 的逻辑索引?
- 当我运行MEX文件时,MATLAB崩溃
- 从 MATLAB 到 C++:相当于带有选项 'remove' 的 bwmorph
- 如何使用 "Matlab Data / Engine API for c++" 在 c++ 中加载 .mat
- 如何使用 C++ 和 Matlab Engine under Kdevelopwith CMakeList.
- 将数据从OpenCV矩阵发送到Matlab Engine,C++
- 矩阵引擎"Can't start MATLAB engine"