使用OpenCV获取鼠标移动坐标,可以通过以下步骤实现:
import cv2
import numpy as np
def get_mouse_position(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE:
print("Mouse position:", x, y)
cv2.namedWindow("image")
cv2.setMouseCallback("image", get_mouse_position)
image = cv2.imread("image.jpg")
cv2.imshow("image", image)
cv2.waitKey(0)
完整的示例代码如下:
import cv2
import numpy as np
def get_mouse_position(event, x, y, flags, param):
if event == cv2.EVENT_MOUSEMOVE:
print("Mouse position:", x, y)
cv2.namedWindow("image")
cv2.setMouseCallback("image", get_mouse_position)
image = cv2.imread("image.jpg")
cv2.imshow("image", image)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行代码后,每当鼠标在图像窗口中移动时,都会在控制台打印出当前的鼠标坐标。