当返回OpenCV cv :: Mat *变量(正确初始化)时,它似乎丢失了所有内容。我做错了什么?
// @called function
template<mxClassID CID>
cv::Mat* helper_2dmat_to_mat_ptr(const mxArray* mat) {
int M = mxGetM(mat), N = mxGetN(mat);
const int TYPE = mcv_traits<CID>::CV_TYPE;
cv::Mat pOutMat;
pOutMat.create(M, N, TYPE);
cv::Mat* pOutMat_ptr = &pOutMat;
void* pBegOut = static_cast<void*>(pOutMat_ptr->data);
int pitch = pOutMat_ptr->step;
typedef typename mc_traits<CID>::CT T;
void* pBeg = mxGetData(mat);
pix_iterator_2d<T, eColWise> it_src1(
static_cast<T*>(pBeg), N, M);
pix_iterator_2d<T, eColWise> it_src2(
static_cast<T*>(pBeg), N, M);
it_src2.end();
pix_iterator_2d<T, eRowWise> it_dest(
static_cast<T*>(pBegOut), N, M, pitch);
std::copy(it_src1, it_src2, it_dest);
/**
* If I check the content of 'pOutMat' here, it correctly
* contains the matrix it is supposed to contain (i.e. a [3x3] matrix
* with numbers from 1 to 9)
*/
return pOutMat_ptr;
}
// @caller function
cv::Mat* mxArr_to_new_Mat_ptr(const mxArray* mat) {
const mxClassID id = mxGetClassID(mat);
mwSize const ndim = mxGetNumberOfDimensions(mat);
if (2 == ndim) {
if (mxDOUBLE_CLASS == id) {
cv:: Mat* out = helper_2dmat_to_mat_ptr<mxDOUBLE_CLASS>(mat);
/**
* If I check the content of 'out' here it seems empty
*/
}
else if (...)
}
... other irrelevant code ...
}
我的问题很简单:为什么在“返回”之前?命令指针指向非空矩阵,而当控件返回调用函数时,这个数据似乎是消失的?
我的猜测是,它与内部函数(helper_2dmat_to_mat_ptr)具有从外部函数(mxArr_to_new_Mat_ptr)无法看到的范围这一事实有关,但我该如何解决问题?更重要的是,正如你所看到的,外部函数(mxArr_to_new_Mat_ptr)应该在cv :: Mat *上做一些操作,然后再做与内部函数基本相同的操作。