OpenCV operator 重载
OpenCV operator 重载
以Rect_为例,研究operator 的用法
template<typename _Tp> class Rect_
{
public:
typedef _Tp value_type;
//! various constructors
Rect_();
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
Rect_(const Rect_& r);
Rect_(const CvRect& r);
Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);
Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2);
Rect_& operator = ( const Rect_& r );
//! the top-left corner
Point_<_Tp> tl() const;
//! the bottom-right corner
Point_<_Tp> br() const;
//! size (width, height) of the rectangle
Size_<_Tp> size() const;
//! area (width*height) of the rectangle
_Tp area() const;
//! conversion to another data type
template<typename _Tp2> operator Rect_<_Tp2>() const;
//! conversion to the old-style CvRect
operator CvRect() const;
//! checks whether the rectangle contains the point
bool contains(const Point_<_Tp>& pt) const;
_Tp x, y, width, height; //< the top-left corner, as well as width and height of the rectangle
};
1.
Rect_& operator = ( const Rect_& r );
只是简单的重载=,不再多说。
2 .
operator CvRect() const;
这里重载了CvRect()这个操作符,这是为了便于老的C接口的转换,原来C的实现是
typedef struct CvRect
{
int x;
int y;
int width;
int height;
}
CvRect;
CV_INLINE CvRect cvRect( int x, int y, int width, int height )
{
CvRect r;
r.x = x;
r.y = y;
r.width = width;
r.height = height;
return r;
}
这样就可以调用了CvRect()了,而且使用也很方便,
只要
Rect newRect = Rect();
CvRect oldRect = CvRect(newRect);
其他的Point, Size, Mat 等C++中新定义的类,都有类似的operator重载,以便和老的接口适配。