Convert OpenCV Mat file to Matlab matrix

2.6k Views Asked by At

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);

    }

Original Image Result shows in Matlab

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));
2

There are 2 best solutions below

5
On

When copying data from cv::Mat to Matlab's matrix, you should pay attention to:

  1. In OpenCV, all matrix are row-major, while col-major for Matlab. You should transpose before copying.

  2. For copying color images, you should transfer data channel by channel.


As you're going to copying a color image cv::Mat to Matlab, you should do as follows:

mwSize dims[] = {rows, cols, 3};
mxArray *T = mxCreateNumericArray(3, dims, mxUINT8_CLASS, mxREAL);
UINT8 *ptr = (UINT8 *) mxGetData(T);

std::vector<cv::Mat> channels; // B, G, R channels
cv::split(m, channels);

// remember to transpose first because MATLAB is col-major!!!
cv::transpose(channels[0], channels[0]);
cv::transpose(channels[1], channels[1]);
cv::transpose(channels[2], channels[2]);

memcpy(ptr, channels[2].ptr(), rows*cols*sizeof(UINT8));
memcpy(ptr+rows*cols, channels[1].ptr(), rows*cols*sizeof(UINT8));
memcpy(ptr+2*rows*cols, channels[0].ptr(), rows*cols*sizeof(UINT8));

engPutVariable(m_pEngine, name.c_str(), T); // put into matlab

Updated: If you're copying a gray-scale image, it should be like

mwSize dims[] = {rows, cols};
mxArray *T = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL);
UINT8 *ptr = (UINT8 *) mxGetData(T);

cv::transpose(m, m); // remember to tranpose first because MATLAB is col-major!!!
memcpy(ptr, m.ptr(), rows*cols*sizeof(UINT8));

engPutVariable(m_pEngine, name.c_str(), T); // put into matlab

Note that, UINT8 here is to be consistent with Matlab's uint8 type used for images.

0
On

Okay. I use for loop to copy data and finally it works.

int rows=m.rows;
        int cols=m.cols;  

        mwSize dims[] = {rows, cols};
        mxArray *T = mxCreateNumericArray(2, dims, mxUINT8_CLASS, mxREAL);
        char *ptr = (char *) mxGetData(T);

         for (int i = 0; i < rows; i++)  
        {  
            for (int j = 0; j < cols; j++)  
            {    
            ptr[j*rows + i] =  (* m.row(i).col(j).data);  
             }  
        }  

        engPutVariable(m_pEngine, name.c_str(), T); // put into matlab

For the answer posted above, I am still working on it. Thanks the help from @herohuyongtao to mention transpose issue.