简体   繁体   English

我如何获得多维cv :: Mat的大小? (Mat或MatND)

[英]How do i get the size of a multi-dimensional cv::Mat? (Mat, or MatND)

I am creating a multi-dimensional MAT object, and would like to get the size of the object - eg, 我正在创建一个多维MAT对象,并希望得到对象的大小 - 例如,

const int sz[] = {10,10,9};
Mat temp(3,sz,CV_64F);
std::cout << "temp.dims = " << temp.dims << " temp.size = " << temp.size() << " temp.channels = " << temp.channels() << std::endl;

I believe the resulting MAT to be 10x10x9, and I'd like to confirm, but the COUT statement gives: 我相信得到的MAT是10x10x9,我想确认一下,但是COUT声明给出了:

temp.dims = 3 temp.size = [10 x 10] temp.channels = 1 temp.dims = 3 temp.size = [10 x 10] temp.channels = 1

I was hoping to see either: 我希望看到:

temp.dims = 3 temp.size = [10 x 10 x 9] temp.channels = 1 temp.dims = 3 temp.size = [10 x 10 x 9] temp.channels = 1

Or: 要么:

temp.dims = 3 temp.size = [10 x 10] temp.channels = 9 temp.dims = 3 temp.size = [10 x 10] temp.channels = 9

How can I get the dimensionality of this Mat object? 如何获得此Mat对象的维度? I didn't see any methods in Mat::Mat or MatND 我在Mat :: Mat或MatND中没有看到任何方法

You just found yourself one of the many flaws of the OpenCV C++ API. 您刚刚发现自己是OpenCV C ++ API的众多缺陷之一。

If you take a look at the source code of OpenCV, version 2.4.6.1, you will realize cv::Mat::size is a member object of type cv::Mat::MSize , which is defined as 如果你看看OpenCV 2.4.6.1版的源代码,你会发现cv::Mat::sizecv::Mat::MSize类型的成员对象,定义为

struct CV_EXPORTS MSize
{
    MSize(int* _p);
    Size operator()() const;
    const int& operator[](int i) const;
    int& operator[](int i);
    operator const int*() const;
    bool operator == (const MSize& sz) const;
    bool operator != (const MSize& sz) const;

    int* p;
};

Thus cv::Mat::size() actually refers to cv::Mat::MSize::operator ()() , whose return type Size is defined as 因此cv::Mat::size()实际上是指cv::Mat::MSize::operator ()() ,其返回类型Size定义为

typedef Size_<int> Size2i;
typedef Size2i Size;

Quoting from the OpenCV manual , Size is a 引用OpenCV手册Size是一个

"Template class for specifying the size of an image or rectangle. The class includes two members called width and height." “用于指定图像或矩形大小的模板类。该类包括两个名为width和height的成员。”

In other words, Size is only suitable for 2D matrices. 换句话说, Size仅适用于2D矩阵。

Fortunately all hope is not lost as you can use cv::Mat::MSize::operator [](int i) to get the size of the matrix along its i-th dimension . 幸运的是,所有希望都不会丢失,因为您可以使用cv::Mat::MSize::operator [](int i)来获取矩阵沿其第i维的大小

const int sz[] = {10,10,9}; 
cv::Mat temp(3,sz,CV_64F); 
std::cout << "temp.dims = " << temp.dims << "temp.size = [";
for(int i = 0; i < temp.dims; ++i) {
    if(i) std::cout << " X ";
    std::cout << temp.size[i];
}
std::cout << "] temp.channels = " << temp.channels() << std::endl;

temp.dims = 3 temp.size = [10 x 10 x 9] temp.channels = 1 temp.dims = 3 temp.size = [10 x 10 x 9] temp.channels = 1

OpenCV 2.4.9 deals with multi-dimensional sizes just fine. OpenCV 2.4.9处理多维尺寸就好了。 The struct cv::Mat::MSize can stores and return multiple dimensions. struct cv::Mat::MSize可以存储和返回多个维度。 The data member cv::Mat::size is of the type cv::Mat::MSize . 数据成员cv::Mat::size的类型为cv::Mat::MSize This code will enumerate the dimensions for you: 此代码将为您枚举尺寸:

const int sz[] = {3, 4, 3, 6};
cv::Mat bigm(4, sz, CV_8UC1);
cout << bigm.dims << '\t';
for (int i=0; i<bigm.dims; ++i)
  cout << bigm.size[i] << ',';
cout << endl;

The output is: 输出是:

4       3,4,3,6,
std::vector<size_t> getMatDims(const cv::Mat& m)
{
    std::vector<size_t> dims(m.dims);
    std::partial_sum(&m.step[0],&m.step[0]+m.dims,dims.begin(),[](size_t a,size_t b){ return a/b; });
    return dims;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM