cdahms
cdahms

Reputation: 3760

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

I'm experiencing some confusion on how rows and cols are implemented in OpenCV's Mat class, hopefully somebody can provide some clarification.

When using the Mat class, rows and cols can't have () after them, i.e.:

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";

Having familiarity with .NET, I at first figured rows and cols must be properties, but after reading this:

Does C++11 have C#-style properties?

it seems C++ does not have an equivalent, at least not without adding a class to mimic .NET properties, which as far as I could find, OpenCV does not do.

So, I figured Mat rows and cols must be member variables, and went to the OpenCV source for confirmation.

Checking mat.hpp:

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

line 217 and 218:

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

is where I get unclear on things. I've seen this many times:

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

or this:

const int SOME_CONSTANT = 123;

and if cols should be read-only to the outside world, I would have figured something like this:

// member variable
private:
    int _cols;

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

Looking at the usage of rows and cols in matrix.cpp:

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

for example, line 395:

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

or line 498:

    cols = _colRange.size();

and many similar examples, it seems these are indeed member variables, but I still am unclear on the line 217 & 218 syntax:

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

Can somebody clarify if these are member variables and what is going on syntax-wise on the declaration line?

Upvotes: 0

Views: 2052

Answers (1)

Miki
Miki

Reputation: 41775

You're looking at _InputArray.

If you look at the Mat, you'll see at line 2047 that rows and cols are in fact member variables:

int rows, cols;

Upvotes: 2

Related Questions