1 #include "stdafx.h"
2 #include "opencv.hpp"
3
4 using namespace std;
5 using namespace cv;
6 int main()
7 {
8 cv::Mat I;
9 I = cv::Mat(300, 400, CV_8UC3,cv::Scalar(255,255,255)); //创建一个白色画布
10 cv::Point2f center(200, 150);
11 cv::Size2f size(180, 120); //size(width, height)
12 float angle = 30.0;
13 cv::RotatedRect rRect(center, size, angle);
14 cout << "the center of rRect is: " << rRect.center << endl;
15 cout << "the size of rRect is: " << rRect.size << endl;
16 cout << "the angle of rRect is: " << rRect.angle << endl;
17 cv::Point2f vertices[4];
18 rRect.points(vertices); //获取4个角点
19 for (int i = 0; i < 4; i++) //逐条边绘制旋转矩形
20 line(I, vertices[i], vertices[(i + 1) % 4], Scalar(255, 0, 0),2);
21 cv::circle(I,center,2,cv::Scalar(0,0,0),2); //中点center
22 cv::Rect boundingRect = rRect.boundingRect(); //外接矩形
23 cv::rectangle(I, boundingRect, Scalar(0, 0, 255),1);
24 cv::imshow("rotated rectangle", I);
25 waitKey(0);
26 return 0;
27 }