问题描述
我有一个 cv :: Mat
,它符合以下尺寸 480 x 640 x 32
。
I have a cv::Mat
which hase to following size 480 x 640 x 32
.
您能告诉我如何访问此结构中的数据?让我们说,我要访问 [2,2,2]
的元素。我如何做到这一点?
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?
编辑1:
此模板< typename T> const T& Mat :: at(int i,int j,int k)const
但我收到以下运行时错误:
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 :
编辑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);
其中 toMat
p>
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;
}
推荐答案
> cv::Mat f(382,510,32);
首先,你错误的构造函数 cv :: Mat
:您的值32用作类型,导致一些未定义的行为。
To start with, you got wrong the constructor of cv::Mat
http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-mat : your value 32 is used as type, leading to some undefined behaviour.
您有使用此
Mat::Mat(int ndims, const int* sizes, int type)
const int[] mySizes = {382,510,32}; //I hope I have written this correctly...
cv::Mat f(3,mySizes,CV_64F). // You find CV_64 in the same documentation page.
然后,您的 Utils :: toMat
函数看起来真的很复杂的调试...我建议你阅读一些关于文档,可能你重新实现矩阵的初始化(填充)使用在
方法:
Then, your Utils::toMat
functions looks really complicated to debug... I suggest that you read a little bit more about the documentation, and possibly that you re-implement the initialization (filling) of your matrix using the at
method:
f.at<double>(x,y,z) = ...
这篇关于如何从cv :: Mat访问数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!