7
我使用C++和Matlab Engine將數據從OpenCV矩陣發送到matlab。我試圖從列專業轉換爲行專業,但我真的很困惑如何做到這一點。我無法理解如何處理Matlab指針mxArray並將數據放入引擎。從OpenCV矩陣發送數據到Matlab引擎,C++
有沒有人與OpenCV一起使用matlab發送矩陣?我沒有找到很多信息,我認爲這是一個非常有趣的工具。任何幫助將受到歡迎。
我使用C++和Matlab Engine將數據從OpenCV矩陣發送到matlab。我試圖從列專業轉換爲行專業,但我真的很困惑如何做到這一點。我無法理解如何處理Matlab指針mxArray並將數據放入引擎。從OpenCV矩陣發送數據到Matlab引擎,C++
有沒有人與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");
值得一試[mexopencv](https://github.com/kyamagu/mexopencv),一個將OpenCV作爲MEX函數公開給MATLAB的項目 – Amro