RotatedRect类opencv

来源:互联网 发布:知已知彼什么意思 编辑:程序博客网 时间:2024/06/14 16:33
 

RotatedRect类opencv

标签: opencv图像处理

转自:http://blog.csdn.net/u012507022/article/details/51684776



有三个属性:
  1. 矩形中心点(质心)
  2. 边长(长和宽)
  3. 旋转角度

[cpp] view plain copy
  1. class CV_EXPORTS RotatedRect  
  2. {  
  3. public:  
  4.     //构造函数  
  5.     RotatedRect();  
  6.     RotatedRect(const Point2f& center, const Size2f& size, float angle);  
  7.     RotatedRect(const CvBox2D& box);  
  8.   
  9.     //!返回矩形的4个顶点  
  10.     void points(Point2f pts[]) const;  
  11. //返回包含旋转矩形的最小矩形  
  12. Rect boundingRect() const;  
  13. //!转换到旧式的cvbox2d结构  
  14. operator CvBox2D() const;  
  15.   
  16.     Point2f center; //矩形的质心  
  17. Size2f size;    //矩形的边长  
  18. float angle;    //旋转角度,当角度为0、90、180、270等时,矩形就成了一个直立的矩形  
  19. };  
示例程序:
[cpp] view plain copy
  1. #include"iostream"  
  2. #include"opencv2/opencv.hpp"  
  3.   
  4. using namespace std;  
  5. using namespace cv;  
  6.   
  7. int main()  
  8. {  
  9.     Mat image(200, 200, CV_8UC3, Scalar(0));  
  10.     RotatedRect rRect(Point2f(100, 100), Size2f(100, 50), 30);  
  11.   
  12.     Point2f vertices[4];      //定义矩形的4个顶点  
  13.     rRect.points(vertices);   //计算矩形的4个顶点  
  14.     for (int i = 0; i < 4; i++)  
  15.         line(image, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0));  
  16.   
  17.     Rect brect = rRect.boundingRect(); //返回包含旋转矩形的最小矩形  
  18.     rectangle(image, brect, Scalar(255, 0, 0));  
  19.     imshow("rectangles", image);  
  20.     waitKey(0);  
  21. }  

运行结果:


原创粉丝点击