【转】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

转载于:https://www.cnblogs.com/codingmengmeng/p/6684553.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值