c++ - OpenCv 将圆转换为多边形

标签 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);

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

最佳答案

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

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));
}

关于c++ - OpenCv 将圆转换为多边形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45121551/

相关文章:

c++ - 使用智能指针进行模板推导

c++ - 将 ComboBox 设置为另一个 ComboBox 的 ID

c++ - 使用混合无缝克隆修复 OpenCV 中的错误隐藏

image - 如何去除图像Python中的水印背景

math - 从给定点垂直于线段

c++ - 光线追踪三角形网格对象

c++ - 如何在 C++11 中使用随机生成器作为类成员

c++ - 是作者炫耀还是真实的做法

C++:OpenCV 扫描图像的性能问题

c++ - 查找点是否在几何内部