Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the callback function of OpenCV4.X mouse

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this issue, the editor will bring you about how to achieve the OpenCV4.X mouse callback function. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.

1. Callback function

SetMouseCallback (windowName, onMouse, param=None)

WindowName: window name

OnMouse: mouse response handling function

Mouse callback function, which is called when a mouse event occurs. The mouse event can be any action related to the mouse, such as left button down, left button up, left button double click, etc. It provides us with the coordinates of each mouse event (xmemy). Through this event and location, we can do whatever we want. All mouse events can be listed with the following code.

"Code"

Import cv2

Events = [i for i in dir (cv2) if 'EVENT' in I]

Print (events)

"output"

['EVENT_FLAG_ALTKEY',' EVENT_FLAG_CTRLKEY', 'EVENT_FLAG_LBUTTON'

'EVENT_FLAG_MBUTTON', 'EVENT_FLAG_RBUTTON',' EVENT_FLAG_SHIFTKEY'

'EVENT_LBUTTONDBLCLK', 'EVENT_LBUTTONDOWN',' EVENT_LBUTTONUP', 'EVENT_MBUTTONDBLCLK'

'EVENT_MBUTTONDOWN', 'EVENT_MBUTTONUP',' EVENT_MOUSEHWHEEL', 'EVENT_MOUSEMOVE'

'EVENT_MOUSEWHEEL', 'EVENT_RBUTTONDBLCLK',' EVENT_RBUTTONDOWN', 'EVENT_RBUTTONUP']

Parameter meaning

EVENT_MOUSEMOVE 0 slide

EVENT_LBUTTONDOWN 1 left click

EVENT_RBUTTONDOWN 2 right click

EVENT_MBUTTONDOWN 3 Middle Click

EVENT_LBUTTONUP 4 left button release

EVENT_RBUTTONUP 5 right-click release

EVENT_MBUTTONUP 6 Intermediate release

EVENT_LBUTTONDBLCLK 7 left double click

EVENT_RBUTTONDBLCLK 8 right double click

EVENT_MBUTTONDBLCLK 9 double click in the middle

Second, application 1. Application one

Create a simple application that can draw a circle on the image as long as we double-click it.

"Code"

Import numpy as np

Import cv2

# mouse callback function

Def draw_circle (event, x, y, flags, param):

If event = = cv2.EVENT_LBUTTONDBLCLK:

Cv2.circle (img, (x, y), 100,255,0,0),-1)

# Create a black image, a window and bind the function to window

Img = np.zeros ((512,512,3), np.uint8)

Cv2.namedWindow ('image')

Cv2.setMouseCallback ('image', draw_circle)

While True:

Cv2.imshow ('image', img)

If cv2.waitKey (20) & 0xFF = = 27:

Break

Cv2.destroyAllWindows ()

two。 Application two

Draw a rectangle or circle by dragging the mouse (depending on the mode we choose), just like in a drawing application. So our mouse callback function has two parts, one for drawing rectangles and the other for drawing circles. In the main loop, set the key'm'to set the keyboard binding to switch between rectangles and circles.

"Code"

Import numpy as np

Import cv2

Drawing = False # true if mouse is pressed

Mode = True # if True, draw rectangle. Press 'm'to toggle to curve

Ix,iy =-1mai Mui 1

# mouse callback function

Import numpy as np

Import cv2

Drawing = False # true if mouse is pressed

Mode = True # if True, draw rectangle. Press 'm'to toggle to curve

Ix,iy =-1mai Mui 1

# mouse callback function

Def draw_circle (event,x,y,flags,param):

Global ix,iy,drawing,mode

If event = = cv2.EVENT_LBUTTONDOWN:

Drawing = True

Ix,iy = XBI y

Elif event = = cv2.EVENT_MOUSEMOVE:

If drawing = = True:

If mode = = True:

Cv2.rectangle (img, (ix,iy), (xmemy), (0pr 255pr 0),-1)

Else:

Cv2.circle (img, (xQuery), 5, (0B0255),-1)

Elif event = = cv2.EVENT_LBUTTONUP:

Drawing = False

If mode = = True:

Cv2.rectangle (img, (ix,iy), (xmemy), (0pr 255pr 0),-1)

Else:

Cv2.circle (img, (xQuery), 5, (0B0255),-1)

Img = np.zeros ((512, 512), np.uint8)

Cv2.namedWindow ('image')

Cv2.setMouseCallback ('image',draw_circle)

While True:

Cv2.imshow ('image',img)

K = cv2.waitKey (1) & 0xFF

If k = = ord ('m'):

Mode = not mode

Elif k = = 27:

Break

Cv2.destroyAllWindows ()

The above is the editor for you to share how to achieve the OpenCV4.X mouse callback function, if you happen to have similar doubts, you might as well refer to the above analysis to understand. If you want to know more about it, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report