如何使用c++和opencv输出多边形中顶点在图像中的位置
时间: 2023-05-20 12:01:25 浏览: 108
您可以使用OpenCV中的函数cv::approxPolyDP来检测多边形的顶点,并使用cv::drawContours函数将多边形轮廓绘制在图像上。以下是示例代码:
```
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("image.jpg");
Mat gray, edges;
cvtColor(image, gray, COLOR_BGR2GRAY);
Canny(gray, edges, 50, 200, 3);
vector<vector<Point>> contours;
vector<Vec4i> hierarchy;
findContours(edges, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);
for (size_t i = 0; i < contours.size(); i++)
{
vector<Point> approx;
approxPolyDP(contours[i], approx, arcLength(contours[i], true) * 0.02, true);
if (approx.size() == 4)
{
for (size_t j = 0; j < approx.size(); j++)
{
circle(image, approx[j], 3, Scalar(0, 255, 0), -1);
}
}
}
imshow("Image", image);
waitKey(0);
return 0;
}
```
这个示例代码将检测图像中的四边形,并在图像中绘制其顶点。
阅读全文
相关推荐


















