Opencv数据类型(二):Rect类和RotatedRect类

一、cv::Rect类对象

1、Rect类的一些别名,如Rect2i,Rect2f, Rect2d等等

typedef Rect_<int> Rect2i;
typedef Rect_<float> Rect2f;
typedef Rect_<double> Rect2d;
typedef Rect2i Rect;

2、Rect_类的构造函数,Rect类包含Point类的成员x和y(矩形左上角)和Size类的成员width和heigh。

template<typename _Tp> inline
Rect_<_Tp>::Rect_()
    : x(0), y(0), width(0), height(0) {}

template<typename _Tp> inline
Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)
    : x(_x), y(_y), width(_width), height(_height) {}

template<typename _Tp> inline
Rect_<_Tp>::Rect_(const Rect_<_Tp>& r)
    : x(r.x), y(r.y), width(r.width), height(r.height) {}

template<typename _Tp> inline
Rect_<_Tp>::Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz)
    : x(org.x), y(org.y), width(sz.width), height(sz.height) {}

template<typename _Tp> inline
Rect_<_Tp>::Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2)
{
    x = std::min(pt1.x, pt2.x);
    y = std::min(pt1.y, pt2.y);
    width = std::max(pt1.x, pt2.x) - x;
    height = std::max(pt1.y, pt2.y) - y;
}

3、Rect类函数

template<typename _Tp> class Rect_
{
public:
    typedef _Tp value_type;

    //! default constructor
    Rect_();
    Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);
    Rect_(const Rect_& 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类型
    Point_<_Tp> tl() const;
    //! the bottom-right corner
    // 返回右下点的坐标,Pont类型
    Point_<_Tp> br() const;

    //! size (width, height) of the rectangle
    //返回Size(width,height)
    Size_<_Tp> size() const;
    //! area (width*height) of the rectangle
    _Tp area() const;
    //! true if empty
    // 是否是空,空返回1,非空返回0
    //template<typename _Tp> inline
	//bool Rect_<_Tp>::empty() const
	//{
	//    return width <= 0 || height <= 0;
	//}
    bool empty() const;

    //! conversion to another data type
    template<typename _Tp2> operator Rect_<_Tp2>() const;

    //! checks whether the rectangle contains the point
    //Point点是否在Rect范围类 是返回1,否返回0
    bool contains(const Point_<_Tp>& pt) const;

    _Tp x; //!< x coordinate of the top-left corner
    _Tp y; //!< y coordinate of the top-left corner
    _Tp width; //!< width of the rectangle
    _Tp height; //!< height of the rectangle
};

4、Rect对象的覆写操作符
在这里插入图片描述
5、举例

int main() {
    cv::Rect Rt(0,5,4,8);
    cv::Rect Rt2(1, 2, 5, 8);
    Rt2 &= Rt;
    std::cout << Rt.tl() << std::endl; // cout [0,5] Point
    std::cout << Rt.br() << std::endl; // cout [4,13]
    std::cout << Rt.size().width << " " <<Rt.size().height << std::endl; // cout 4 8
    std::cout << Rt.area() << std::endl; //cout 32
    std::cout << Rt.empty() << std::endl; //cout 0

    cv::Point p1(1, 6);
    std::cout << Rt.contains(p1) << std::endl; //cout 1

    cv::Point p2(100, 100);
    std::cout << Rt.contains(p2) << std::endl; //cout 0
    std::cout << "hello world" << std::endl;
    return 0;
}

二、cv::RotatedRect类

1、cv::RotatedRect 类是少数没有使用模板的C++接口类
包含 一个中心点 cv::Point2f,一个大小cv::Size2f和一个额外的角度float的容器,其中float的角度代表图形绕中心店旋转的角度。和cv::Rect不同的是,RotatedRect是以中心为原点的,Rect是以左上角为原点的。

class CV_EXPORTS RotatedRect
{
public:
    //! default constructor
    //构造函数
    RotatedRect();
    /** full constructor
    @param center The rectangle mass center.
    @param size Width and height of the rectangle.
    @param angle The rotation angle in a clockwise direction. When the angle is 0, 90, 180, 270 etc.,
    the rectangle becomes an up-right rectangle.
    */
    RotatedRect(const Point2f& center, const Size2f& size, float angle);
    /**
    Any 3 end points of the RotatedRect. They must be given in order (either clockwise or
    anticlockwise).
     */
    RotatedRect(const Point2f& point1, const Point2f& point2, const Point2f& point3);

    /** returns 4 vertices of the rectangle
    @param pts The points array for storing rectangle vertices. The order is bottomLeft, topLeft, topRight, bottomRight.
    */
    // 返回矩形的四个顶点
    void points(Point2f pts[]) const;
    //! returns the minimal up-right integer rectangle containing the rotated rectangle
    //返回包含旋转矩形的最小矩形
    Rect boundingRect() const;
    //! returns the minimal (exact) floating point rectangle containing the rotated rectangle, not intended for use with images
    //
    Rect_<float> boundingRect2f() const;
    //! returns the rectangle mass center
    // //矩形的质心
    Point2f center;
    //! returns width and height of the rectangle
    Size2f size;
    //! returns the rotation angle. When the angle is 0, 90, 180, 270 etc., the rectangle becomes an up-right rectangle.
    //旋转角度,当角度为0、90、180、270等时,矩形就成了一个直立的矩形
    float angle;
};

2、图解RotatedRect
感谢 https://blog.csdn.net/u011574296/article/details/71405239 提供的图
在这里插入图片描述3、举例

int main() {
    cv::RotatedRect rr(cv::Point2f(2.5,2.5), cv::Size(10,10), 90);
    cv::Point2f p2[4];
    std::cout << rr.center << " " << rr.size.width << " " << rr.size.height << " " << rr.angle <<  std::endl; // [2.5, 2.5] [10 x 10] 0.5
    // cout [2.5, 2.5] 10 10 90
    rr.points(p2);
    std::cout << p2[0] << " " << p2[1] << " " << p2[2] << " " << p2[3]<< std::endl;
    //cout [-2.5, -2.5] [7.5, -2.5] [7.5, 7.5] [-2.5, 7.5]

//    cv::RotatedRect rr2(cv::Point2f(2.5,2.5), cv::Point2f(5.5,5.5));

    std::cout << "hello world" << std::endl;
    return 0;
}
  • 7
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值