Skip to content
Search

Cv2.setMouseCallback

lastmod 2023-01-05created 2023-01-053 edits

Sets the mouse callback.

Example

cv2.imshow("draw", src)
cv2.setMouseCallback("draw", mouse_event, src)
cv2.waitKey()

Callback example

def mouse_event(event, x, y, flags, param):
    global radius

    if event == cv2.EVENT_FLAG_LBUTTON:    
        cv2.circle(param, (x, y), radius, (255, 0, 0), 2)
        cv2.imshow("draw", src)

    elif event == cv2.EVENT_MOUSEWHEEL:
        if flags > 0:
            radius += 1
        elif radius > 1:
            radius -= 1
  • event - The event that occurred in the window.
  • x, y - The mouse coordinates.
  • flags - Used together with event to check special states.
  • param - User-defined data passed along to the mouse callback setter function.

Events

  • EVENT_MOUSEMOVE - When the mouse pointer moves over the window
  • EVENT_LBUTTONDOWN - When the left mouse button is pressed
  • EVENT_MBUTTONDOWN - When the middle mouse button is pressed
  • EVENT_RBUTTONDOWN - When the right mouse button is pressed
  • EVENT_LBUTTONUP - When the left mouse button is released
  • EVENT_MBUTTONUP - When the middle mouse button is released
  • EVENT_RBUTTONUP - When the right mouse button is released
  • EVENT_LBUTTONDBLCLK - When the left mouse button is double-clicked
  • EVENT_MBUTTONDBLCLK - When the middle mouse button is double-clicked
  • EVENT_RBUTTONDBLCLK - When the right mouse button is double-clicked
  • EVENT_MOUSEWHEEL - When the mouse is scrolled vertically
  • EVENT_MOUSEHWHEEL - When the mouse is scrolled horizontally

Flags

  • EVENT_FLAG_LBUTTON - The left mouse button is held down
  • EVENT_FLAG_MBUTTON - The middle mouse button is held down
  • EVENT_FLAG_RBUTTON - The right mouse button is held down
  • EVENT_FLAG_CTRLKEY - The Control (Ctrl) key is held down
  • EVENT_FLAG_SHIFTKEY - The Shift key is held down
  • EVENT_FLAG_ALTKEY - The Alt key is held down
  • flags > 0 - Upward or rightward direction of a mouse scroll event
  • flags < 0 - Downward or leftward direction of a mouse scroll event

Favorite site

author Zer0 sitemap