简体   繁体   中英

How to return a vector of OpenCV C++ Mats to MatLab via mex

I have an OpenCV program in C++ which takes a large Mat and returns a vector of smaller Mats, which I am trying to use in MatLab using mex (specifically mexOpenCV from here: https://github.com/kyamagu/mexopencv ). I can get a single Mat back into the plhs[0] simply with plhs[0]=MxArray(theMats[0]) for example, but how can I return the whole vector?

Thank you!

#include "mexopencv.hpp"
void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[])
{
    // Check number of arguments
    nargchk(nlhs<=1 && nrhs==1);

    // Convert mxArray to cv::Mat
    cv::Mat mat = MxArray(prhs[0]).toMat();
    std::vector<cv::Mat> theMats;

    int ySize = 400;
    int xSize = 400;
    int yStride = ySize;
    int xStride = xSize;

    int cols = (mat.cols-xSize)/xStride + 1;
    int rows = (mat.rows-ySize)/yStride + 1;

    for (int i=0; i<cols; i++){
        for (int j=0; j<rows; j++){
            cv::Rect crop(cv::Point(i*xStride,j*yStride),
            cv::Point(i*xStride+xSize, j*yStride+ySize));
            theMats.push_back(mat(crop));
        }
    }

    // Convert cv::Mat back to mxArray
    plhs[0] = MxArray(theMats[0]); //I want theMats, not just theMats[0]

}

In the general case where cv::Mat may each be of different size, returning each cv::Mat as a matrix and returning the collection as a cell array is probably your only and best option:

https://www.mathworks.com/help/matlab/apiref/mxcreatecellarray.html

If each of the cv::Mat are of the same size, you could also entertain returning the collection of cv::Mat as an ND numeric matrix where one of the dimensions is each cv::Mat and the remaining dimensions are the dimensionality of each of the cv::Mat objects.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM