函数名: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 }
原图:
填充后:
时间: 2024-10-12 22:48:05