【轉】OpenCV對圖片中的RotatedRect進行填充


 

函數名:full_rotated_rect

函數參數: image輸入圖像,rect希望在圖像中填充的RotatedRect,color填充的顏色

主要的思路是:先找到RotatedRect的四個頂點,然后畫出外框。再利用四個頂點找出其中平行兩邊的所有點,對相應的兩個點進行連接。

 1 void full_rotated_rect(Mat &image, const RotatedRect &rect, const Scalar &color)  
 2 {  
 3     CvPoint2D32f point[4];  
 4     Point pt[4];  
 5     vector<Point> center1, center2;  
 6   
 7     /*畫出外框*/  
 8     cvBoxPoints(rect, point);  
 9     for (int i = 0; i<4; i++)  
10     {  
11         pt[i].x = (int)point[i].x;  
12         pt[i].y = (int)point[i].y;  
13     }  
14     line(image, pt[0], pt[1], color, 1);  
15     line(image, pt[1], pt[2], color, 1);  
16     line(image, pt[2], pt[3], color, 1);  
17     line(image, pt[3], pt[0], color, 1);  
18   
19     /*填充內部*/  
20     find_all_point(pt[0], pt[1], center1);  /*找出兩點間直線上的所有點*/  
21     find_all_point(pt[3], pt[2], center2);  
22     vector<Point>::iterator itor1 = center1.begin(), itor2 = center2.begin();  
23     while (itor1 != center1.end() && itor2 != center2.end())  
24     {  
25         line(image, *itor1, *itor2, color, 1);  /*連接對應點*/  
26         itor1++;  
27         itor2++;  
28     }  
29   
30     vector<Point>().swap(center1);  
31     vector<Point>().swap(center2);  
32 }  

 

函數名:find_all_point

函數參數:start起始點,end結束點,save保存點的容器

主要思路:遞歸查找兩點的中點,直到兩點相同。

 1 void find_all_point(Point start, Point end, vector<Point> &save)  
 2 {  
 3     if (abs(start.x - end.x) <= 1 && abs(start.y - end.y) <= 1)  
 4     {  
 5         save.push_back(start);  
 6         return; /*點重復時返回*/  
 7     }  
 8   
 9     Point point_center;  
10     point_center.x = (start.x + end.x) / 2;  
11     point_center.y = (start.y + end.y) / 2;  
12     save.push_back(point_center);   /*儲存中點*/  
13     find_all_point(start, point_center, save);  /*遞歸*/  
14     find_all_point(point_center, end, save);  
15 }  

原圖:

填充后:

 

轉自WizardtoH


免責聲明!

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



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