opencv RotatedRect的各參數詳解


RotatedRect該類表示平面上的旋轉矩形,有三個屬性:

  1. 矩形中心點(質心)
  2. 邊長(長和寬)
  3. 旋轉角度
 
        
 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     void points(Point2f pts[]) const;//!返回矩形的4個頂點
 9     Rect boundingRect() const; //返回包含旋轉矩形的最小矩形
10     operator CvBox2D() const;    //!轉換到舊式的cvbox2d結構
11     Point2f center; //矩形的質心
12     Size2f size;   //矩形的邊長
13     float angle;  //旋轉角度,當角度為0、90、180、270等時,矩形就成了一個直立的矩形
14 };
 
        

        這個類中包含了外接矩形的中心center、大小size以及角度angle。為了更好的理解這幾個參數的意義,請看下圖:在opencv中,坐標的原點在左上角,與x軸平行的方向為角度為0,逆時針旋轉角度為負,順時針旋轉角度為正。角度是水平軸(x軸)順時針旋轉,與碰到的第一個邊的夾角度數。而opencv默認把這個邊的邊長作為height。

 

 1 #include<opencv2/opencv.hpp>
 2 #include<iostream>
 3 using namespace std;
 4 using namespace cv;
 5 
 6 int main()
 7 {
 8     Mat img = imread("C:\\Users\\hsy\\Desktop\\1.jpg");
 9     Mat img_gray;
10     cvtColor(img, img_gray, COLOR_RGB2GRAY);
11     img_gray = img_gray > 30;
12     vector<vector<Point>>contours;
13     vector<Vec4i> hierarchy;
14     vector<RotatedRect>rect;
15     //【5】查找輪廓
16     findContours(img_gray, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
17     for (int i = 0; i < contours.size(); i++)
18     {
19         rect.push_back(minAreaRect(contours[i]));
20         Point2f vertices[4];      //定義矩形的4個頂點
21         rect[i].points(vertices);   //計算矩形的4個頂點
22         for (int i = 0; i < 4; i++)
23             line(img, vertices[i], vertices[(i + 1) % 4], Scalar(0, 255, 0),1);
24         cout <<"width的值:"<<rect[i].size.width << endl;
25         cout << "height的值:" << rect[i].size.height << endl;//其實只有一個外接矩形
26     }
27     imshow("img", img);
28     waitKey(0);
}

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM