[Opencv Rect class presentation]

Rect class presentation

Opencv Rect class is a very popular class, so here tidied usage Rect class

1. Rect created

Matrix creating requires four parameters, namely, the top left coordinates (x, y) and width and height of the matrix, so as to create time wording

Rect r(x,y,width,height);

2. Common Functions

2.1 size()和area()

This set of functions is used to describe the size and area of ​​a rectangle. size () function returns the value is a type of variable size, including the width and height of the rectangle, the area () function is returned to the area of ​​a rectangle.

   Rect rect1(0,0,300,300);
	
	cout << rect1.size() << endl;
	cout << rect1.area() << endl;

2.2 br () and tl ()

This set of functions is used to describe a rectangular coordinate point. br () returns the lower right corner coordinates (BottomRight), while TL () Returns the rectangle is the upper-left coordinates (TopLeft). These two functions return value types are Point

2.3 contains()和inside()

This function returns bool type group, and is used to describe the relative positions of a rectangle point.
Usage is as follows:

Rect rect1(0,0,300,300);
Point x(1, 1);
cout << rect1.contains(x) << endl; //用来判断点x是否包含在矩形rect1中
cout<<x.inside(rect1)<<endl;//用来判断点x是否包含在矩形rect1中

From the results, the code is running, functions, and contains Inside functions are determined point relative relationship and rectangular, rather than "opencv3 programming entry" written in, Inside function used to determine whether the rectangle in the rectangle.

3. rectangular conversion

Rect class and simply adding a Point type variable rectangular achieved by translation, and with the addition to achieve a variable rectangular type Size of zoom.
Tests are as follows:

//平移
	Mat m = imread("1.jpg");
	Rect rect1(0,0,300,300);
	Point x(100, 100);
	Scalar s(255, 255, 0);
	rectangle(m, rect1, s);
	rect1 += x;
	rectangle(m, rect1, s);
	imshow("a", m);
	waitKey(0);
	

Here Insert Picture Description

//放缩
	Mat m = imread("1.jpg");
	Rect rect1(0,0,300,300);
    Size x(100, 100);
	Scalar s(255, 255, 0);
	rectangle(m, rect1, s);
	rect1 += x;
	rectangle(m, rect1, s);
	imshow("a", m);
	waitKey(0);

Here Insert Picture Description

4. rectangle calculation

Operators can be achieved by taking & rectangle and set the operator | take rectangular intersection may be implemented.

	Mat m1 = imread("1.jpg");
	Mat m2 = imread("1.jpg");
	Rect rect1(0,0,300,300);
	Rect rect2(0, 0, 100, 100);
	Scalar s(255, 255, 0);

	Rect rt1 = rect1 & rect2; //取并集
	rectangle(m1, rt1, s);
	Rect rt2 = rect1 | rect2; //取交集
	rectangle(m2, rt2, s);
	imshow("交集", m1);
	imshow("并集", m2);

Here Insert Picture Description

Published 14 original articles · won praise 1 · views 503

Guess you like

Origin blog.csdn.net/qq_41741344/article/details/104344554