Opencv Rect_模板类是一个比较重要的类,可以设置图像ROI区域,截取图像等。
Rect_类的成员变量有x,y,width,height,分别为左上角座标和矩形宽高。常用函数有Size()返回Size;
area()返回矩形面积;
contains(Point)判断点是否在矩形内;
inside(Rect)判断矩形是否在该矩形内;
tl()返回左上角点座标;
br()返回右下角点座标;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
//////////////////////////////// Rect_ //////////////////////////////// /** @brief Template class for 2D rectangles described by the following parameters: - Coordinates of the top-left corner. This is a default interpretation of Rect_::x and Rect_::y in OpenCV. Though, in your algorithms you may count x and y from the bottom-left corner. - Rectangle width and height. OpenCV typically assumes that the top and left boundary of the rectangle are inclusive, while the right and bottom boundaries are not. For example, the method Rect_::contains returns true if \f[x \leq pt.x < x+width, y \leq pt.y < y+height\f] Virtually every loop over an image ROI in OpenCV (where ROI is specified by Rect_\<int\> ) is implemented as: @code for(int y = roi.y; y < roi.y + roi.height; y++) for(int x = roi.x; x < roi.x + roi.width; x++) { // ... } @endcode In addition to the class members, the following operations on rectangles are implemented: - \f$\texttt{rect} = \texttt{rect} \pm \texttt{point}\f$ (shifting a rectangle by a certain offset)ect - \f$\texttt{rect} = \texttt{rect} \pm \texttt{size}\f$ (expanding or shrinking a rectangle by a certain amount) - rect += point, rect -= point, rect += size, rect -= size (augmenting operations) - rect = rect1 & rect2 (rectangle intersection) - rect = rect1 | rect2 (minimum area rectangle containing rect1 and rect2 ) - rect &= rect1, rect |= rect1 (and the corresponding augmenting operations) - rect == rect1, rect != rect1 (rectangle comparison) This is an example how the partial ordering on rectangles can be established (rect1 \f$\subseteq\f$ rect2): @code template<typename _Tp> inline bool operator <= (const Rect_<_Tp>& r1, const Rect_<_Tp>& r2) { return (r1 & r2) == r1; } @endcode For your convenience, the Rect_\<\> alias is available: cv::Rect */ template<typename _Tp> class Rect_ { public: typedef _Tp value_type; //! default constructor Rect_(); //默认构造函数 Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height);//原点x,y,尺寸w,h Rect_(const Rect_& r); //拷贝构造函数 Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz);//原点org,尺寸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; //! true if empty bool empty() const; //! conversion to another data type template<typename _Tp2> operator Rect_<_Tp2>() const; //! checks whether the rectangle contains the point 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 }; |
Opencv Rect_类具体定义,再取别名:
1 2 3 4 |
typedef Rect_<int> Rect2i; typedef Rect_<float> Rect2f; typedef Rect_<double> Rect2d; typedef Rect2i Rect; |
Opencv Rect_类运用例子:
1 2 3 4 5 6 7 8 9 10 11 12 |
cv::Rect rect1(10, 10, 30, 40); cv::Rect rect2(10, 20, 60, 70); cv::Rect r1 = rect1 & rect2; //交集 cv::Rect r2 = rect1 | rect2; //并集 cv::Rect r3 = rect1 + Point(10,20); //平移 cv::Rect r4 = rect1 + Size(2,2); //矩形框加大 printf("矩形r1:%d %d %d %d \n", r1.x,r1.y,r1.width,r1.height); printf("矩形r2:%d %d %d %d \n", r2.x, r2.y, r2.width, r2.height); printf("矩形r3:%d %d %d %d \n", r3.x, r3.y, r3.width, r3.height); printf("矩形r4:%d %d %d %d \n", r4.x, r4.y, r4.width, r4.height); |
Opencv Rect_类运用测试结果:
1 2 3 4 |
矩形r1:10 20 30 30 矩形r2:10 10 60 80 矩形r3:20 30 30 40 矩形r4:10 10 32 42 |