如何从 cv::Mat 访问数据

Sim*_*mon 3 c++ opencv

我有一个cv::Mat以下尺寸480 x 640 x 32

你能告诉我如何访问这个结构中的数据吗?假设我想从 position 访问元素[2, 2, 2]。我怎样才能做到这一点?

编辑 1:

我曾尝试使用它,template<typename T> const T& Mat::at(int i, int j, int k) const但收到以下运行时错误:

在此处输入图片说明

编辑2:

这是我如何使用代码:

cv::Mat f(382,510,32); 
f=Utils::toMat(features); 
cout<<f.at<double>(1,1,1); 
Run Code Online (Sandbox Code Playgroud)

这里toMat是波纹管详述。

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;
}
Run Code Online (Sandbox Code Playgroud)

Ant*_*nio 5

> cv::Mat f(382,510,32);
Run Code Online (Sandbox Code Playgroud)

首先,你弄错了cv::Mat http://docs.opencv.org/modules/core/doc/basic_structures.html#mat-mat的构造函数:你的值 32 被用作类型,导致一些未定义的行为。

你必须使用这个

 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.
Run Code Online (Sandbox Code Playgroud)

然后,你的Utils::toMat函数看起来调试起来真的很复杂......我建议你阅读更多关于文档的内容,并且可能你使用以下at方法重新实现矩阵的初始化(填充):

 f.at<double>(x,y,z) = ...
Run Code Online (Sandbox Code Playgroud)