使用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()
運行代碼后,每當鼠標在圖像窗口中移動時,都會在控制臺打印出當前的鼠標坐標。