OpenCV C++ Mat 类行和列 - 它们是成员变量(和相关问题)吗?

OpenCV C++ Mat class rows and cols - are they member variables (and related questions)?

本文关键字:变量 问题 成员 Mat C++ OpenCV      更新时间:2023-10-16

我对如何在OpenCV的Mat类中实现行和列感到困惑,希望有人可以提供一些澄清。

使用 Mat 类时,行和列后面不能有 ((,即:

cv::Mat imgSomeImage;
imgSomeImage = cv::imread("some_image.png");
// this line works
std::cout << "num rows = " << imgSomeImage.rows << "n";
// this line does not compile, only difference is the () after rows
std::cout << "num rows = " << imgSomeImage.rows() << "n";

熟悉 .NET 后,我起初认为行和列必须是属性,但在阅读本文后:

C++11 是否具有 C# 样式的属性?

似乎C++没有等价物,至少没有添加一个类来模仿 .NET 属性,据我所知,OpenCV 没有这样做。

所以,我认为 Mat 行和 cols 必须是成员变量,并转到 OpenCV 源代码进行确认。

检查mat.hpp:

https://github.com/opencv/opencv/blob/master/modules/core/include/opencv2/core/mat.hpp

第 217 和 218 行:

int cols(int i=-1) const;
int rows(int i=-1) const;

是我不清楚的地方。 我已经看过很多次了:

// declare a member variable with a default value of -1
int cols = -1;

或者这个:

const int SOME_CONSTANT = 123;

如果 cols 应该对外界只读,我会想到这样的事情:

// member variable
private:
int _cols;
// getter
public:
int cols() { return _cols; }

查看矩阵中行和列的用法.cpp:

https://github.com/opencv/opencv/blob/master/modules/core/src/matrix.cpp

例如,第 395 行:

if( d == 2 && rows == _sizes[0] && cols == _sizes[1] )

或第 498 行:

cols = _colRange.size();

还有许多类似的例子,似乎这些确实是成员变量,但我仍然不清楚 217 和 218 行的语法:

int cols(int i=-1) const;
int rows(int i=-1) const;

有人可以澄清这些是否是成员变量以及声明行上的语法方面发生了什么吗?

你正在查看_InputArray.

如果您查看Mat,您将在第 2047 行看到rowscols实际上是成员变量:

int rows, cols;