【发布时间】:2018-03-04 01:34:09
【问题描述】:
我有一个 C++ 中的 OpenCV 程序,它接受一个大 Mat 并返回一个较小 Mats 的向量,我试图在 MatLab 中使用 mex(特别是来自这里的 mexOpenCV:https://github.com/kyamagu/mexopencv)。 例如,我可以简单地使用 plhs[0]=MxArray(theMats[0]) 将单个 Mat 返回到 plhs[0] 中,但是如何返回整个向量?
谢谢!
#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]
}
【问题讨论】:
标签: c++ matlab opencv vector mex