Embedded_game/地图映射.py

50 lines
1.5 KiB
Python
Raw Normal View History

2025-01-02 12:48:11 +08:00
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 加载地图图片
image_path = 'map.png' # 替换为你的地图图片路径
image = cv2.imread(image_path)
if image is None:
raise FileNotFoundError("地图图片未找到,请检查路径!")
# 转换为RGB格式方便使用matplotlib显示
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# 创建一个函数,在指定像素坐标画圆
def draw_circle(img, coordinates, color=(255, 0, 0), radius=10, thickness=2):
"""在给定图片上画圆"""
x, y = coordinates
cv2.circle(img, (x, y), radius, color, thickness)
return img
# 复制地图以进行标注
annotated_image = image_rgb.copy()
while True:
try:
# 用户输入像素坐标
user_input = input("请输入像素坐标格式x,y或输入'q'退出:")
if user_input.lower() == 'q':
print("退出程序。")
break
# 解析输入坐标
x, y = map(int, user_input.split(","))
# 检查坐标是否超出范围
if not (0 <= x < image_rgb.shape[1] and 0 <= y < image_rgb.shape[0]):
print("输入的坐标超出图片范围,请重新输入!")
continue
# 在指定位置画圆
annotated_image = draw_circle(annotated_image, (x, y))
# 使用matplotlib显示更新后的图片
plt.imshow(annotated_image)
plt.axis('off')
plt.show()
except ValueError:
print("输入格式错误请输入像素坐标格式为x,y例如100,200")