使用Opencv的circle函数绘制圆转成使用polylines函数绘制圆,并将外轮廓坐标点输出
时间: 2024-01-01 17:04:31 浏览: 103
好的,我知道了。这是一个关于OpenCV的问题,你可以使用`polylines`函数来绘制圆。具体步骤如下:
1. 定义一个圆的半径和圆心坐标。
```c++
int radius = 50;
Point center(100, 100);
```
2. 定义一个空向量来存储圆的所有点。
```c++
std::vector<Point> points;
```
3. 遍历圆的每个角度,使用三角函数计算出每个点的坐标。
```c++
for (int angle = 0; angle < 360; angle += 10) {
int x = cvRound(center.x + radius * cos(angle * CV_PI / 180));
int y = cvRound(center.y + radius * sin(angle * CV_PI / 180));
points.push_back(Point(x, y));
}
```
4. 最后,使用`polylines`函数绘制圆,并将所有坐标点输出。
```c++
std::vector<std::vector<Point>> contours;
contours.push_back(points);
polylines(image, contours, true, Scalar(255, 0, 0), 2);
std::cout << "Contours: " << contours[0] << std::endl;
```
这里的`image`是你要绘制圆的图像,`true`表示绘制封闭的轮廓,`Scalar(255, 0, 0)`表示轮廓的颜色,`2`表示轮廓的线宽。
输出的`contours[0]`是一个向量,包含了所有圆的坐标点。
完整代码如下:
```c++
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
int main() {
Mat image = Mat::zeros(200, 200, CV_8UC3);
// Define circle center and radius
int radius = 50;
Point center(100, 100);
// Define a vector to store all the points on the circle
std::vector<Point> points;
// Calculate the coordinates of each point on the circle
for (int angle = 0; angle < 360; angle += 10) {
int x = cvRound(center.x + radius * cos(angle * CV_PI / 180));
int y = cvRound(center.y + radius * sin(angle * CV_PI / 180));
points.push_back(Point(x, y));
}
// Draw the circle using polylines function
std::vector<std::vector<Point>> contours;
contours.push_back(points);
polylines(image, contours, true, Scalar(255, 0, 0), 2);
// Print out all the contour points
std::cout << "Contours: " << contours[0] << std::endl;
imshow("Circle", image);
waitKey(0);
return 0;
}
```
阅读全文
相关推荐


















