OpenCV mouse coordinates differs from pynput mouse.position

Question:

I was testing out OpenCV‘s mouse callback function with pynput.mouse and I realised the coordinates of the cursor are different. Here is the output from the code below. Does anyone know what the offset is as it does not seem to be consistent in the output

import cv2
cap = cv2.VideoCapture(0)

from pynput.mouse import Controller
mouse = Controller()

def on_mouse(event, x, y, flags, param):
    '''
    Mouse callback function
    '''
    global x1, y1
    if event == cv2.EVENT_MOUSEMOVE:
        x1, y1 = x, y
        print("opencv: ", str((x1, y1)))
        print("pynput: ", str(mouse.position))

cv2.namedWindow("Image", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("Image", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setMouseCallback("Image", on_mouse)

while cap.isOpened():
    success, image = cap.read()

    cv2.imshow("Image", image)

    if cv2.waitKey(1) & 0xFF == 27:
      break


cv2.destroyAllWindows()
cap.release()
Asked By: tayron.vikranth

||

Answers:

I just tested my theory, and it seems to be the case.

Small piece of code used:

import cv2
import numpy as np

from pynput.mouse import Controller

mouse = Controller()

f1 = np.zeros((1080, 1920))
f2 = np.zeros((720, 1280))


def on_mouse(event, x, y, flags, param):
    if event == cv2.EVENT_MOUSEMOVE:
        print(f"{param} - opencv: {x, y}")
        print(f"{param} - pynput: {mouse.position}")


cv2.namedWindow("1080x1920", cv2.WND_PROP_FULLSCREEN)
cv2.namedWindow("720x1280", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("1080x1920", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)
cv2.setWindowProperty("720x1280", cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

cv2.setMouseCallback("1080x1920", on_mouse, "1080x1920")
cv2.setMouseCallback("720x1280", on_mouse, "720x1280")

while True:
    cv2.imshow("1080x1920", f1)
    cv2.imshow("720x1280", f2)

    cv2.waitKey()

The results obtained for the bottom right corner:

1080x1920 - opencv: (1919, 1079)
1080x1920 - pynput: (1919, 1079)

720x1280 - opencv: (1279, 719)
720x1280 - pynput: (1919, 1079)
Answered By: Jay
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.