From b387b61b126f23a2540e45558e4d4a7e0a1fc029 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BE=99=E6=BE=B3?= Date: Wed, 15 Jan 2025 15:44:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E4=B8=80=E4=B8=AA=E5=B0=8F?= =?UTF-8?q?=E5=B7=A5=E5=85=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tools/get_pixel_xy.py | 45 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 tools/get_pixel_xy.py diff --git a/tools/get_pixel_xy.py b/tools/get_pixel_xy.py new file mode 100644 index 0000000..e57d813 --- /dev/null +++ b/tools/get_pixel_xy.py @@ -0,0 +1,45 @@ +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() \ No newline at end of file