python测量图片两点距离代码
时间: 2023-07-22 20:57:08 浏览: 126
在Python中,你可以使用Pillow库(或者OpenCV库)来测量图片中两点之间的距离。下面是一个使用Pillow库实现的示例代码:
```python
from PIL import Image
import math
# 打开图片
image = Image.open('path_to_your_image.jpg')
# 选择两个点的坐标
point1 = (x1, y1)
point2 = (x2, y2)
# 计算两点之间的距离
distance = math.sqrt((point2[0] - point1[0])**2 + (point2[1] - point1[1])**2)
print("距离:", distance)
```
请确保将`path_to_your_image.jpg`替换为你的图片路径,并且将`x1`、`y1`、`x2`、`y2`替换为你想要测量距离的两个点的坐标。运行代码后,你将得到这两个点之间的距离。
相关问题
mapbox中测量两点距离的示例代码
以下是示例代码:
```python
from math import sin, cos, sqrt, atan2, radians
def distance(lat1, lon1, lat2, lon2):
# approximate radius of earth in km
R = 6373.0
lat1_rad = radians(lat1)
lon1_rad = radians(lon1)
lat2_rad = radians(lat2)
lon2_rad = radians(lon2)
dlon = lon2_rad - lon1_rad
dlat = lat2_rad - lat1_rad
a = sin(dlat / 2)**2 + cos(lat1_rad) * cos(lat2_rad) * sin(dlon / 2)**2
c = 2 * atan2(sqrt(a), sqrt(1 - a))
distance_km = R * c
return distance_km
```
此函数可以输入两个地点的经纬度,并返回它们之间的距离(单位为公里)。
基于python利用OpenCV写一段测量物体之间的距离代码
如下是一个简单的测量物体之间距离的示例代码:
```python
import cv2
# 加载图片
img = cv2.imread("img.png")
# 定义两个点的坐标
point1 = (10, 20)
point2 = (150, 200)
# 计算距离并输出
distance = ((point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2) ** 0.5
print("The distance between two points is: ", distance)
```
该代码中使用OpenCV加载图片,并定义了两个点的坐标。使用勾股定理计算出两点之间的距离,并输出结果。当然,实际应用中,我们可能需要进行更复杂的处理。
以上是一个简单的示例代码,如果您有其它更复杂的需求,可以针对您的具体需求进行相应的定制。