opencv求两点距离
时间: 2023-11-07 19:05:52 浏览: 80
可以使用以下代码来求两点距离:
```
import cv2
import math
# 两点坐标
point1 = (10, 20)
point2 = (30, 40)
# 求两点距离
distance = math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)
print(distance)
```
其中,math.sqrt() 函数用于求平方根,** 表示乘方。
相关问题
opencv求两点之间的距离
请参考以下Python代码示例:
```python
import cv2
import math
# 定义两个点的坐标
point1 = (50, 50)
point2 = (100, 100)
# 计算两点之间的距离
distance = math.sqrt((point2[0] - point1[0]) ** 2 + (point2[1] - point1[1]) ** 2)
# 打印输出距离值
print("The distance between the two points is:", distance)
```
该代码中,使用了math库中的sqrt函数来计算距离值。首先定义了两个点的坐标,然后根据勾股定理计算距离值,并打印输出。
opencv求点到直线距离
使用 OpenCV 中的 `pointPolygonTest` 函数可以求点到直线的距离。具体步骤如下:
1. 定义直线上的两个点 `pt1` 和 `pt2`。
2. 定义需要求距离的点 `pt3`。
3. 使用 `cv::Vec4f` 类型的变量 `line` 存储直线的参数,即 `cv::fitLine(pt1, pt2, line, cv::DIST_L2, 0, 0)`。
4. 使用 `cv::pointPolygonTest` 函数求解距离,即 `double dist = cv::pointPolygonTest(line, pt3, true);`。
其中 `true` 表示计算有符号距离,即点在直线的一侧为正,另一侧为负;`false` 表示计算无符号距离,即距离的绝对值。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)