C++ OpenCv将圆转换为多边形
我想知道OpenCv中是否有一种方法可以将给定半径的圆转换为多边形,例如五边形或六边形或类似的东西 圆圈很简单:C++ OpenCv将圆转换为多边形,c++,opencv,geometry,polygon,C++,Opencv,Geometry,Polygon,我想知道OpenCv中是否有一种方法可以将给定半径的圆转换为多边形,例如五边形或六边形或类似的东西 圆圈很简单: 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
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);
这是可行的,但我想知道是否有更聪明的方法可以做到这一点?当然,更聪明的方法确实存在——使用数组和循环
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));
}