我如何得到一个多维cv :: Mat的大小? (Mat或MatND) [英] How do i get the size of a multi-dimensional cv::Mat? (Mat, or MatND)
问题描述
我创建了一个多维的MAT对象,并想获得对象的大小 - 例如
I am creating a multi-dimensional MAT object, and would like to get the size of the object - e.g.,
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;
我相信结果MAT为10x10x9,我想确认,但COUT语句给出:
I believe the resulting MAT to be 10x10x9, and I'd like to confirm, but the COUT statement gives:
temp.dims = 3 temp.size = [10 x 10] temp.channels = 1
temp.dims = 3 temp.size = [10 x 10] temp.channels = 1
我希望看到:
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
或:
temp.dims = 3 temp.size = [10 x 10] temp.channels = 9
temp.dims = 3 temp.size = [10 x 10] temp.channels = 9
如何获得这个Mat对象的维度?我在Mat :: Mat或MatND中没有看到任何方法
How can I get the dimensionality of this Mat object? I didn't see any methods in Mat::Mat or MatND
推荐答案
你刚刚发现自己是OpenCV C ++ API。
You just found yourself one of the many flaws of the OpenCV C++ API.
如果你看看OpenCV的源代码版本2.4.6.1,你会实现 cv :: Mat: :size
是类型 cv :: Mat :: MSize
的成员对象,其定义为
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
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;
};
因此 cv :: Mat :: size()
实际上是指 cv :: Mat :: MSize :: operator()()
,其返回类型 Size
定义为
Thus cv::Mat::size()
actually refers to cv::Mat::MSize::operator ()()
, whose return type Size
is defined as
typedef Size_<int> Size2i;
typedef Size2i Size;
引用 OpenCV手册,大小
是
用于指定图片或矩形大小的模板类别该类包含两个成员,名称为width和height。
换句话说, Size
只适用于2D矩阵。
In other words, Size
is only suitable for 2D matrices.
幸运的是,所有希望都不会丢失,因为可以使用 cv :: Mat :: MSize :: operator [](int i)
以获取矩阵沿其第i个维度的大小。
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.
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
这篇关于我如何得到一个多维cv :: Mat的大小? (Mat或MatND)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!