码迷,mamicode.com
首页 > 其他好文 > 详细

【转】OpenCV对图片中的RotatedRect进行填充

时间:2017-04-09 14:49:57      阅读:456      评论:0      收藏:0      [点我收藏+]

标签:font   log   png   函数名   vbo   ges   target   nbsp   points   

 

函数名: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

【转】OpenCV对图片中的RotatedRect进行填充

标签:font   log   png   函数名   vbo   ges   target   nbsp   points   

原文地址:http://www.cnblogs.com/codingmengmeng/p/6684553.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!