opencv图像处理怎么找轮廓两点的距离
时间: 2023-11-07 18:04:46 浏览: 42
要找到轮廓上两点的距离,可以使用OpenCV中的`arcLength`函数来计算轮廓的周长,然后使用`approxPolyDP`函数来近似轮廓,将其转换为一组顶点。接下来,可以使用`cv::norm`函数来计算两个顶点之间的距离。
以下是一个示例代码片段,演示如何计算轮廓上两点的距离:
```
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
using namespace cv;
using namespace std;
int main()
{
Mat image = imread("contour.jpg", IMREAD_GRAYSCALE);
// Find contours
vector<vector<Point>> contours;
findContours(image, contours, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);
// Approximate contour
vector<Point> approx;
double epsilon = 0.01 * arcLength(contours[0], true);
approxPolyDP(contours[0], approx, epsilon, true);
// Calculate distance between two points
Point p1 = approx[0];
Point p2 = approx[approx.size() - 1];
double distance = norm(p1 - p2);
cout << "Distance between two points: " << distance << endl;
return 0;
}
```
在这个示例中,我们首先读取了一个灰度图像,并找到了其边界轮廓。然后,我们使用`approxPolyDP`函数近似轮廓,找到了最靠近轮廓起点和终点的两个顶点,并计算它们之间的距离。