简体   繁体   中英

OpenCV rect.width and rect.size().width difference?

Sorry for a noob question, but can somebody tell me the difference between this

cv:: Rect rect;
int width = rect.width;
int height = rect.height;

and this

cv::Rect rect;
int width = rect.size().width;
int height = rect.size().height;

They are the same, NO difference .


OpenCV is open source, so you can always look at the source code.

You can see that width and height are public member variables:

template<typename _Tp> class Rect_
{
public: 
    ...
    _Tp width; //!< width of the rectangle
    _Tp height; //!< height of the rectangle
};

and size() returns a cv::Size initialized with their values:

template<typename _Tp> inline
Size_<_Tp> Rect_<_Tp>::size() const
{
    return Size_<_Tp>(width, height);
}

There is no difference with respect to the code you have shown.
However, width and height are public attributes of Rect and if you change them you will be changing Rect .

size() just returns the size (width, height) of Rect . You cannot change Rect through the members of size() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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