I create a Matlab engine to covert OpenCV Mat file to Matlab matrix. However, I got wrong results. I attached my code so that you can directly test it.
#pragma comment (lib, "libmat.lib")
#pragma comment (lib, "libmx.lib")
#pragma comment (lib, "libmex.lib")
#pragma comment (lib, "libeng.lib")
void cvLoadMatrixToMatlab(const Mat& m, const string name, Engine *m_pEngine)
{
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(char));
engPutVariable(m_pEngine, name.c_str(), T);
text = name + "=" + name + "'"; // Column major to row major
engEvalString(m_pEngine, text.c_str());
mxDestroyArray(T);
}
int main(int argc, char **argv)
{
/*Open Matlab Engine*/
Engine *m_pEngine;
m_pEngine = engOpen("null");
cv::Mat img = imread("panda.jpg",0);
cvLoadMatrixToMatlab(img,"imgMatlab", m_pEngine);
engEvalString(m_pEngine, "imshow(imgMatlab)");
cv::waitKey(0);
}
I suspect the code below cause this problem but I don't know how to fix it.
memcpy((char*)mxGetPr(T), (char*)m.data, rows*cols*sizeof(char));