import cv2 import numpy as np def find_nearest_points(image): # 获取图像的高度和宽度 height, width = image.shape[:2] # 计算四个端点的坐标 top_left = (0, 0) top_right = (width-1, 0) bottom_left = (0, height-1) bottom_right = (width-1, height-1) # 初始化最近的四个点的列表 nearest_points = [] # 计算每个端点最近的点 for point in [top_left, top_right, bottom_left, bottom_right]: min_distance = float('inf') # 初始化最小距离为无穷大 nearest_point = None # 遍历图像中的每个像素,找到最近的点 for y in range(height): for x in range(width): distance = cv2.pointPolygonTest([point], (x, y), True) # 更新最小距离和对应的最近点 if distance < min_distance: min_distance = distance nearest_point = (x, y) nearest_points.append(nearest_point) return nearest_points如何将它的点打印出来
时间: 2024-02-14 14:30:17 浏览: 67
要将找到的最近点打印出来,你可以在函数的末尾添加以下代码:
```python
for point in nearest_points:
print(f"Nearest point: {point}")
```
这将遍历最近点列表并打印每个点的坐标。完整的函数代码如下:
```python
import cv2
import numpy as np
def find_nearest_points(image):
# 获取图像的高度和宽度
height, width = image.shape[:2]
# 计算四个端点的坐标
top_left = (0, 0)
top_right = (width-1, 0)
bottom_left = (0, height-1)
bottom_right = (width-1, height-1)
# 初始化最近的四个点的列表
nearest_points = []
# 计算每个端点最近的点
for point in [top_left, top_right, bottom_left, bottom_right]:
min_distance = float('inf') # 初始化最小距离为无穷大
nearest_point = None
# 遍历图像中的每个像素,找到最近的点
for y in range(height):
for x in range(width):
distance = cv2.pointPolygonTest([point], (x, y), True)
# 更新最小距离和对应的最近点
if distance < min_distance:
min_distance = distance
nearest_point = (x, y)
nearest_points.append(nearest_point)
for point in nearest_points:
print(f"Nearest point: {point}")
return nearest_points
```
当调用`find_nearest_points()`函数时,它将打印出最近点的坐标。你可以根据需要自定义打印的格式。
阅读全文