OpenCV Rect_< _Tp > 模版类是一个2维矩形模板类,其英文全称为Rect_< _Tp > Class Template Reference,其公有成员函数有以下几个:
其公有属性有:
Rect_< _Tp > 模版类以左上角点tl,坐标_Tp x,Tp y及矩形宽度及矩形高度来描述一个矩形。有了矩形的基本属性,就可获得其他属性。如右下角点br(用两点构造一个矩形,默认这两个点是其对角点),矩形的size,矩形的面积等,也可判定一个点是否在矩形内。另外矩形还可以执行一下操作:
rect = rect ± point;//将矩形移动一定的偏移量
rect = rect ±s ize;//将矩形扩大或缩小一定量
rect += point;
rect -= point;
rect += size;
rect -= size ;
rect = rect1 & rect2;//求矩形相交部分(即相互叠加部分)
rect = rect1 | rect2; //同时包含 rect1 和 rect2 的最小面积矩形
rect &= rect1,;
rect |= rect1;
bool b1 = (rect1 == rect2); //比较判断
bool b2 = (rect1 != rect2); //比较判断
因C++的基本数据类型有:int,flot,double等,OpenCV中有定义相应的Rect,如下:
typedef Rect_<int> cv::Rect2i
typedef Rect2i cv::Rect
typedef Rect_<double> cv::Rect2d
typedef Rect_<float> cv::Rect2f
常用的Rect实际上是矩形模板类实例化int的类。
示例程序
下面新建一个控制台应用程序,来演示矩形创建及其他成员函数的用法。其代码如下:
#include <iostream>
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
int main()
{
Rect rec[8];
Point pts[3];
pts[0].x = 30;
pts[0].y = 30;
pts[1].x = 230;
pts[1].y = 130;
pts[2].x = 80;
pts[2].y = 80;
rec[1] = Rect(pts[0], Size(150, 100));//test constructor and "= "operatror
rec[2] = Rect(80,80, 150,80); //test constructor and" = "operatror
rec[3] = Rect(pts[2], pts[1]); //ttest constructor and" = "operatror
cout << "rec[1] width = " << rec[1].width << endl;
cout << "rec[1] height = " << rec[1].height << endl;
cout << "rec[1] area = " << rec[1].area() << endl;
cout << "rec[1] top left corner: " << rec[1].tl() << endl;
cout << "rec[1] bottom left corner: " << rec[1].br() << endl;
if (rec[1].contains(pts[2])) //test contiains function
cout << "rec[1] contins pts[2] " << endl;
else
{
cout << "rec[1] does not contin pts[2] " << endl;
}
rec[4] = rec[1] & rec[2]; //test" &" operater
if (rec[4].empty())
cout << " There is not intersection between rec[1] and rec[2]" << endl;
else
{
cout << "rec[4] width = " << rec[4].width << endl;
cout << "rec[4] height = " << rec[4].height << endl;
cout << "rec[4] area = " << rec[4].area() << endl;
cout << "rec[4] top left corner: " << rec[4].tl() << endl;
cout << "rec[4] bottom left corner: " << rec[4].br() << endl;
}
rec[5] = rec[1] | rec[2]; // test "|" operater
if (rec[5].empty())
cout << "rec[5] is empty" << endl;
else
{
cout << "rec[5] width = " << rec[5].width << endl;
cout << "rec[5] height = " << rec[5].height << endl;
cout << "rec[5] area = " << rec[5].area() << endl;
cout << "rec[5] top left corner: " << rec[5].tl() << endl;
cout << "rec[5] bottom left corner: " << rec[5].br() << endl;
}
rec[6] = rec[1] + Size(100, 100);
cout << "rec[6] width = " << rec[6].width << endl;
cout << "rec[6] height = " << rec[6].height << endl;
cout << "rec[6] area = " << rec[6].area() << endl;
cout << "rec[6] top left corner: " << rec[6].tl() << endl;
cout << "rec[6] bottom left corner: " << rec[6].br() << endl;
rec[7] = rec[1] + Point(50,50);
cout << "rec[7] width = " << rec[7].width << endl;
cout << "rec[7] height = " << rec[7].height << endl;
cout << "rec[7] area = " << rec[7].area() << endl;
cout << "rec[7] top left corner: " << rec[7].tl() << endl;
cout << "rec[7] bottom left corner: " << rec[7].br() << endl;
waitKey(0);
}
本博文到此结束。