import cv2
import sys
# 定义鼠标回调函数,用于存储鼠标点击的点
def mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
points.append((x, y))
cv2.circle(image, (x, y), 5, (0, 255, 0), -1)
# 读取图片
image_o = cv2.imread(r'F:\3d\Python-3D-Rasterizer-main\pyrender-main\res\50\00004_img.jpg')
image = cv2.cvtColor(image_o, cv2.COLOR_BGR2RGB)
# 读取图片
if image is None:
print("Error: Could not read the image.")
sys.exit(1)
# 创建一个窗口并设置鼠标回调
cv2.namedWindow("Image")
cv2.setMouseCallback("Image", mouse_callback)
points = []
while True:
# 显示图片
cv2.imshow("Image", image)
# 等待按键,如果是回车键(ASCII码为13),则退出循环
key = cv2.waitKey(1) & 0xFF
if key == 13: # Press Enter key to exit
break
cv2.destroyAllWindows()
# 打印选中的点的坐标
print("Selected points:", points)
opencv 选点
于 2023-04-27 15:30:13 首次发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)