import cv2 import numpy as np def resize_image(image, max_size=1200): # 获取原始尺寸 height, width = image.shape[:2] # 计算缩放比例 scale = min(max_size/width, max_size/height) if scale < 1: # 只有当图像过大时才进行缩放 new_width = int(width * scale) new_height = int(height * scale) resized = cv2.resize(image, (new_width, new_height)) return resized, scale return image, 1.0 def mouse_callback(event, x, y, flags, param): if event == cv2.EVENT_LBUTTONDOWN: # 计算原始图像上的坐标 original_x = int(x / scale) original_y = int(y / scale) print(f'原始图像坐标 (x, y): ({original_x}, {original_y})') # 在缩放后的图像上标记点击位置 cv2.circle(displayed_img, (x, y), 3, (0, 255, 0), -1) cv2.imshow('image', displayed_img) # 读取图像 img = cv2.imread(r"E:\datasets\UAV\134\project\images\20240312_093841_W_W.jpg") if img is None: print('错误:无法读取图像') exit() # 缩放图像 displayed_img, scale = resize_image(img) # 创建窗口并设置鼠标回调函数 cv2.imshow('image', displayed_img) cv2.setMouseCallback('image', mouse_callback) # 等待按键,按 'q' 退出 while True: if cv2.waitKey(1) & 0xFF == ord('q'): break cv2.destroyAllWindows()