OpenCv将圆形转换为多边形

时间:2017-07-15 18:38:29

标签: c++ opencv geometry polygon

我想知道OpenCv中是否有办法将给定半径的circle转换为polygon(例如五边形或六边形或类似的东西)?

圆圈非常简单:

 cv::circle(myMat, center_point, radius, colour, 2, 16);

和多边形也很容易:

 cv::polylines(myMat, points, isClosed, colour, 2, 16);

我的方法如下:

int length = 150;
Point center_point(500, 500);

Point P1;
P1.x = (int)round(center_point.x + length * cos(0 * CV_PI / 180.0));
P1.y = (int)round(center_point.y + length * sin(0 * CV_PI / 180.0));

Point P2;
P2.x = (int)round(center_point.x + length * cos(45 * CV_PI / 180.0));
P2.y = (int)round(center_point.y + length * sin(45 * CV_PI / 180.0));

Point P3;
P3.x = (int)round(center_point.x + length * cos(90 * CV_PI / 180.0));
P3.y = (int)round(center_point.y + length * sin(90 * CV_PI / 180.0));

Point P4;
P4.x = (int)round(center_point.x + length * cos(135 * CV_PI / 180.0));
P4.y = (int)round(center_point.y + length * sin(135 * CV_PI / 180.0));

Point P5;
P5.x = (int)round(center_point.x + length * cos(180 * CV_PI / 180.0));
P5.y = (int)round(center_point.y + length * sin(180 * CV_PI / 180.0));

Point P6;
P6.x = (int)round(center_point.x + length * cos(225 * CV_PI / 180.0));
P6.y = (int)round(center_point.y + length * sin(225 * CV_PI / 180.0));  

Point P7;
P7.x = (int)round(center_point.x + length * cos(270 * CV_PI / 180.0));
P7.y = (int)round(center_point.y + length * sin(270 * CV_PI / 180.0));

Point P8;
P8.x = (int)round(center_point.x + length * cos(315 * CV_PI / 180.0));
P8.y = (int)round(center_point.y + length * sin(315 * CV_PI / 180.0));

cv::polylines(myMat, {P1,P2,P3,P4,P5,P6,P7,P8}, isClosed, colour, 2, 16);

这是有效的,但我想知道是否有更聪明的方法来做到这一点?

1 个答案:

答案 0 :(得分:1)

当然,确实存在更聪明的方法 - 使用数组和循环。

for(i=0;i<N;i++) {
  P[i].x = (int)round(center_point.x + length * cos(i * 2 * CV_PI / N));
  P[i].y = (int)round(center_point.y + length * sin(i * 2 * CV_PI / N));
}