SoFunction
Updated on 2024-11-14

opencv3/python mouse response operation details

Mouse callback function:

def setMouseCallback(
windowName,     #Window name
onMouse,      # Mouse response handler
param=None)     #The processing function of theID

eventThe mouse event:

event:
EVENT_LBUTTONDBLCLK = 7     Double left click
EVENT_LBUTTONDOWN = 1      left click
EVENT_LBUTTONUP = 4       Left click to release
EVENT_MBUTTONDBLCLK = 9     Intermediate release
EVENT_MBUTTONDOWN = 3      Middle click
EVENT_MBUTTONUP = 6       Intermediate release
EVENT_MOUSEHWHEEL = 11     Rolling Wheel Events
EVENT_MOUSEMOVE = 0       sliding movement
EVENT_MOUSEWHEEL = 10      Rolling Wheel Events
EVENT_RBUTTONDBLCLK = 8     right-click and double-click
EVENT_RBUTTONDOWN = 2      right click
EVENT_RBUTTONUP = 5       Right click release


flags:
EVENT_FLAG_ALTKEY = 32     check or refer toAltincidents of non-release
EVENT_FLAG_CTRLKEY = 8     check or refer toCtrlincidents of non-release
EVENT_FLAG_LBUTTON = 1     Left-click Drag and Drop
EVENT_FLAG_MBUTTON = 4     middle-button drag and drop (computing)
EVENT_FLAG_RBUTTON = 2     Right-click drag and drop
EVENT_FLAG_SHIFTKEY = 16    check or refer toShiftincidents of non-release

typical example

Draw a rectangle on the image by dragging with the left mouse click and output the rectangle information:

import cv2
def draw_rectangle(event,x,y,flags,param):
  global ix, iy
  if event==cv2.EVENT_LBUTTONDOWN:
    ix, iy = x, y
    print("point1:=", x, y)
  elif event==cv2.EVENT_LBUTTONUP:
    print("point2:=", x, y)
    print("width=",x-ix)
    print("height=", y - iy)
    (img, (ix, iy), (x, y), (0, 255, 0), 2)

img = ("") #Load the picture
('image')
('image', draw_rectangle)
while(1):
  ('image', img)
  if (20) & 0xFF == 27:
    break
()

Output:

point1:= 254 64
point2:= 708 569
width= 454
height= 505

Above this opencv3/python mouse response operation in detail is all I have shared with you, I hope to give you a reference, and I hope you support me more.