I have a cv::Mat
which hase to following size 480 x 640 x 32
.
Can you please show me how can I access the data within this structure? Let's say that I want to access the element from position [2, 2, 2]
. How can I do this?
EDIT 1:
I have tried using this template<typename T> const T& Mat::at(int i, int j, int k) const
but I receive the following runtime error :
EDIT 2:
Here is how I am using the code :
cv::Mat f(382,510,32);
f=Utils::toMat(features);
cout<<f.at<double>(1,1,1);
where toMat
is detailed bellow.
cv::Mat Utils::toMat( mxArray* p_)
{
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;
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;
}