Rect数据结构经常是在OpenCV中被用来表示为一个矩形尺寸,其成员包括x,y, width,height,其中x和y分别表示矩形框的左上角的起始点坐标,width和height分别表示宽和高。
Rect
OpenCV中预定义好的几种Rect,用来支持不同的数据类型:
Rect2i:整型int
Rect2f: float
Rect2d: double
源代码定义如下:
其中Rect与Rect2i相同都是整型,实现主要依靠的是Rect_类 和Size,Point类似
Rect_类
Rect_类主要如下:
总结如下:
Method | Description |
Rect_() | 默认构造函数 cv::Rect r; cv:Rect2f rf; cv::Rect2d rd |
Rect_(const Rect_& r) | copy构造函数 |
Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height) | 带参数构造函数 x,y为矩形的左上角坐标点 _width,_height分别为宽和高 |
Rect_(const Point_<_Tp>& org, const Size_<_Tp>& sz) | 根据一个点和Size构造Rect org:为矩形左上角 sz:为矩形宽和高 |
Rect_(const Point_<_Tp>& pt1, const Point_<_Tp>& pt2) | 利用两个对角点 构造Rect x = std::min(pt1.x, pt2.x); |
_Tp x; _Tp y; _Tp width; _Tp height; | x,y为矩形的左上角坐标点 width,height分别为宽和高 保存到类中的值 |
_Tp area() | 矩形面积:(width*height) |
Point_<_Tp> tl() | 返回矩形的左上边角点坐标 |
Point_<_Tp> br() | 返回右下边角点坐标 |
Size_<_Tp> size() | size形式返回矩形的width, height |
bool empty() | true: width <= 0或 height <= 0 false: width>0 且height>0 |
bool contains(const Point_<_Tp>& pt) | 检查 pt是否在矩形内: true:在矩形框内 false:不在矩形框内 |
使用用例如下:
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
void main()
{
Rect r;
Rect2f rf;
Rect2d rd;
cout << "r.empty=:" << r.empty() << endl;
cout << "f.empty=:" << rf.empty() << endl;
cout << "rd.empty=:" << rd.empty() << endl;
Rect r2(0,0,300,400);
Rect2f rf2(0, 0, 300.1, 400.4);
Rect2d rd2(2, 2, 2324, 2142424);
cout << "r2=:" << r2 <<endl;
cout << "r2.tl=:" << r2.tl() << endl;
cout << "r2.br=:" << r2.br() << endl;
cout << "r2.size=:" << r2.size() << endl;
cout << "r2.area=:" << r2.area() << endl;
cout << "r2.empty=:" << r2.empty() << endl;
cout << "**********************" << endl;
cout << "rf2=:" << rf2 << endl;
cout << "rf2.tl=:" << rf2.tl() << endl;
cout << "rf2.br=:" << rf2.br() << endl;
cout << "rf2.size=:" << rf2.size() << endl;
cout << "rf2.area=:" << rf2.area() << endl;
cout << "rf2.empty=:" << rf2.empty() << endl;
cout << "rd2.contains(Point(200,200))=:" << rd2.contains(Point(200, 200)) << endl;
cout << "**********************" << endl;
cout << "rd2=:" << rd2 << endl;
cout << "rd2.tl=:" << rd2.tl() << endl;
cout << "rd2.br=:" << rd2.br() << endl;
cout << "rd2.size=:" << rd2.size() << endl;
cout << "rd2.area=:" << rd2.area() << endl;
cout << "rd2.empty=:" << rd2.empty() << endl;
Rect2f rf3(rf2);
rf3.x = 500;
rf3.y = 600;
cout << "rf3=:" << rf3 << endl;
}
运行结果:
Rect_类operator重构
Rect_类通过operator重构的操作:
operator | Method |
+=: | template<typename _Tp> static inline
左上角点按照b偏移相当于平移矩阵: a.x += b.x; |
template<typename _Tp> static inline
width和heigh按照b扩大,扩展矩形区域: a.width += b.width; a.height += b.height | |
-=: | template<typename _Tp> static inline 相当于平移矩阵 |
template<typename _Tp> static inline 缩小矩形区域 | |
&=:
| template<typename _Tp> static inline 取两个矩形框交集部分 |
|= | template<typename _Tp> static inline 尽量取两个矩形框并集即所覆盖的全部面积 |
== | template<typename _Tp> static inline
两个矩阵完全相等(包括x,y,width,height)返回true |
!= | template<typename _Tp> static inline
两个矩阵不相等返回true |
+ | template<typename _Tp> static inline
左上角x和y的位置右偏移b大小,相当于平移矩阵 |
template<typename _Tp> static inline 长和宽扩大b大小,扩展矩形区域 | |
- | template<typename _Tp> static inline
左上角x和y的位置左偏移b大小,相当于平移矩阵 |
template<typename _Tp> static inline
长和宽缩短b大小,缩小矩形区域 | |
& | template<typename _Tp> static inline 取两个矩形框交集部分 |
| | template<typename _Tp> static inline 取两个矩形框并集即所覆盖的全部面积 |
+、- Point平移:
+、-按照Point平移如下(宽度和高度不变):
假设
Rect a
Point b
Rect c= a+b
+、-Size
+、- Size只修改宽度和高度,x和y不变:
假设
Rect a
Size b
Rect c= a+b
如下图所示:
Point和Size偏移用例
分别按照Point和Size进行右偏移用例:
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
void main()
{
Rect r(0,0,300,400);
cout << "r=: " << r << endl;
cout << "r+Point(2,3)=: " << r + Point(2, 3)<< endl;
cout << "r+Size(200,300)=: " << r + Size(200, 300) << endl;
}
运行结果:
r=: [300 x 400 from (0, 0)]
r+Point(2,3)=: [300 x 400 from (2, 3)]
r+Size(200,300)=: [500 x 700 from (0, 0)]
&和&=操作
&和&= 操作为取两个矩阵交集:
假设
Rect a
Rect b
Rect c= a&b
C为a和b两个矩阵的交集,如下图:
图中红框的部分为c
,两个矩阵的交集部分。
|和|=操作
|和|=操作为取两个矩阵最大并集:
假设
Rect a
Rect b
Rect c= a|b
则c矩阵的大小为:
&和| 用例
两个矩形框 &和| 用例:
#include <stdio.h>
#include "opencv2/opencv.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
void main()
{
Rect r1(0,0,300,400);
Rect r2(5, 5, 500, 600);
cout << "r1&r2=: " << (r1&r2) << endl;
cout << "r1|r2" << (r1|r2) << endl;
Rect r3(0, 0, 100, 200);
Rect r4(300, 400, 100, 200);
cout << "r3&r4=: " << (r3&r4) << endl;
cout << "r3|r4" << (r3 | r4) << endl;
}
运行结果:
Rect结合Mat截取图片
可以利用Rect来截取Mat中感兴趣中ROI部分,下面用例用用来截取图片中感兴趣中的一部分:
#include <stdio.h>
#include "opencv2/opencv.hpp"
using namespace cv;
void main()
{
Mat srcImage = imread("len_top.jpg");
printf("Image height:%d, width:%d\n", srcImage.rows, srcImage.cols);
imshow("Image", srcImage);
waitKey(0);
Mat dstImage(srcImage, Rect(50, 50, 250, 150));
imshow("Rect Image", dstImage);
waitKey(0);
}
运行结果:
原始加载的图片为:
截取的感兴趣中的一部分结果为: