How to access data from a cv::Mat converted from mxArray
I have a function which converts an mxArray mat to cv::Mat. The body of the method is listed bellow :
cv::Mat Utils::mxArray2cvMat( mxArray* p_)
{
// Create cv::Mat object.
mwSize ndims_= mxGetNumberOfDimensions(p_);
const mwSize* dims=mxGetDimensions(p_);
std::vector<int> d(dims, dims+ndims_);
int ndims = (d.size()>2) ? d.size()-1 : d.size();
int nchannels = (d.size()>2) ? *(d.end()-1) : 1;
//depth = (depth==CV_USRTYPE1) ? DepthOf[classID()] : depth;
int depth=CV_64F;
std::swap(d[0], d[1]);
cv::Mat mat(ndims, &d[0], CV_MAKETYPE(depth, nchannels));
// Copy each channel.
std::vector<cv::Mat> channels(nchannels);
std::vector<mwSize> si(d.size(), 0); // subscript index
int type = CV_MAKETYPE(depth, 1); // Source type
for (int i = 0; i<nchannels; ++i)
{
si[d.size()-1] = i;
void *pd = reinterpret_cast<void*>(
reinterpret_cast<size_t>(mxGetData(p_))+
mxGetElementSize(p_)*mxCalcSingleSubscript(p_, si.size(), &si[0]));
cv::Mat m(ndims, &d[0], type, pd);
// Read from mxArray through m
m.convertTo(channels[i], CV_MAKETYPE(depth, 1));
}
cv::merge(channels, mat);
return mat;
}
Now I want to access the values within the returned array. I have an array whose dimensions are 300 x 500 x 32
. How can I access the values within this array?
I have the following code:
cv::Mat f=Utils::mxArray2cvMat(features);
cout<<f.at<double>(1,1,1);
However, it crashes giving some assertion failed
error at runtime.
I have also run the following command in order to identify the data type within the f
variable ( cout<<f.type()
) and it returned the value 254
. What type is that? And how can I access the values within this type of Mat
?
Can you please give me any help regarding this matter?