Is it possible to set cursor position from within OpenCV mouse events?

Is it possible to set/update the cursor coordinates from within OpenCV mouse events? I have a virtual keyboard and I am trying to make it such that whenever the cursor hovers over a button, it is locked at the mid-point of the button.

My current mouse callback function:

def on_mouse(self, event, x, y, flags, param):
        global x1, y1
        if event == cv2.EVENT_MOUSEMOVE:
            x1, y1 = x, y
            for button in self.button_list:
                if button.is_hovered_over(x1, y1):
                    # set the cursor coordinates here

The main function:

import cv2
cap = cv2.VideoCapture(0)

from pynput.mouse import Button, Controller
mouse = Controller()

from keyboards import QWERTYKeyboard
keyboard = QWERTYKeyboard.QWERTYKeyboard()

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

while cap.isOpened():
    success, image = cap.read()
    .
    .
    .
    pred_x, pred_y = model.predict(ave_x, ave_y)

    cv2.circle(image, (ave_x, ave_y), 1, (255, 0, 0), 5)

    mouse.position = (pred_x, pred_y)

    cv2.imshow("Image", image)

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

  cv2.destroyAllWindows()

cap.release()

no, it is neither possible, nor “meant to happen”

crosspost:

also, this is a different signature from what opencv expects
you’ll probably dont get a proper ‘self’ or ‘event’ at all …

if that’s a method, and keyboard is an instance of a class with that method, it should be okay.

there is no instance, where it is called, and it expects / calls a different signature

also no instance captured here

iirc, if it needs a self, it has to be passed via param, or use a lambda instead of a function

nope, I just tested that. it’s okay to pass an instance’s method to setMouseCallback. the method is “bound” to the instance.

class Foo:
    def on_mouse(self, event, x, y, flags, param=None):
            print("Foo.onmouse", event, (x,y), flags, param)

foo = Foo()

cv.namedWindow("win")
cv.setMouseCallback("win", foo.on_mouse)
cv.imshow("win", np.eye(512, dtype=np.uint8)*255)
cv.waitKey()
>>> foo.on_mouse
<bound method Foo.on_mouse of <__main__.Foo object at 0x00000251DE3B6DF0>>

keyboard = QWERTYKeyboard.QWERTYKeyboard() implies that keyboard.on_mouse is a method of a class. we don’t know if OP’s def on_mouse belongs to that class.

1 Like

Hi, just to clarify, yes keyboard is an instance of the class QWERTYKeyboard and the on_mouse method is a part of the class