# # 鼠标点击图片输出该点像素坐标
import cv2
# 鼠标点击事件的回调函数
def mouse_callback(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
# 在图片上画一个圆标记鼠标点击的点
cv2.circle(img, (x, y), 1, (0, 255, 0), -1)
# 输出鼠标点击处的像素坐标
print(f"鼠标点击处的像素坐标:({x}, {y})")
# 获取点击处的像素颜色值
pixel_color = img[y, x]
print(f"像素颜色值:{pixel_color}")
# 读取图片,输入需要点击的图片的路径
img = cv2.imread('D:\PythonProjects\Codes\camera_to_robot\data\9.jpg')
# 创建图片显示窗口
cv2.namedWindow('image')
# 设置鼠标回调函数
cv2.setMouseCallback('image', mouse_callback)
while True:
# 在窗口中显示图片
cv2.imshow('image', img)
# 按下ESC键退出循环
if cv2.waitKey(1) == 27:
break
# 关闭窗口
cv2.destroyAllWindows()
使用Python实现鼠标点击输出图片点击处的像素坐标值
于 2023-12-25 15:40:08 首次发布