This is the 25th day of my participation in Gwen Challenge

OpenCV is a C++ library, currently popular computer vision programming library, for real-time processing of computer vision problems, it covers a lot of modules in the field of computer vision. In Python, the OpenCV library is often used for image processing.

This article shows you how to use OpenCV for mouse operations in Python3.

Mouse events

As an interactive operation between human and computer, mouse events are very convenient to use and conform to the operation habits of many people.

The function form of its Opencv source code is as follows:

void cv::setMouseCallback( const String & winname,MouseCallback onMouse,void * 	userdata = 0 )	
Copy the code
  • Code function: set the mouse handler for the specified window.

  • Winname: Specifies the name of the window for which the mouse handler is set.

  • OnMouse: Mouse callback function that is called when a mouse event occurs. When the left mouse button is pressed to do something, the callback function itself is written as needed.

  • Userdata: Optional parameter passed to the callback. The default is 0.

The create mouse callback function has a specific format that is the same everywhere; it differs only in functionality.

Def callback(event,x,y,flags,param)

  • event: Mouse event.
  • x,y: Coordinates of the current mouse.
  • flagsIs:CV_EVENT_FLAGThe combination of.
  • param: User-defined parameter to be passed.

OpenCV provides the following event types:

code function
cv.EVENT_MOUSEMOVE Indicates that the mouse pointer has moved on the window
cv.EVENT_LBUTTONDOWN The left mouse button is pressed down
cv.EVENT_RBUTTONDOWN Press the right mouse button
cv.EVENT_MBUTTONDOWN Press the middle mouse key
cv.EVENT_LBUTTONUP The left mouse button is released
cv.EVENT_RBUTTONUP Indicates that the right mouse button has been released
cv.EVENT_MBUTTONUP Indicates that the mouse button is released
cv.EVENT_LBUTTONDBLCLK Double-click the left mouse button
cv.EVENT_RBUTTONDBLCLK Double-click the right mouse button
cv.EVENT_MBUTTONDBLCLK Double-click the middle mouse button
cv.EVENT_MOUSEWHEEL Positive and negative values indicate forward and backward scrolling, respectively
cv.EVENT_MOUSEHWHEEL Positive and negative values indicate scrolling right and left, respectively

Code case

Draw round

Below through a simple case to achieve the mouse draw circle operation.

The code is as follows:

import cv2
import numpy asImg =np.zeros(()500.500Def draw_circle(event,x,y,flags,param) def draw_circle(event,x,y, param):ifEvent ==cv2.EVENT_LBUTTONDBLCLK: # Draw a circle function with parameters representing the original image, coordinates, radius, color and line width (if -)1Cv2. circle(img,(x,y),20.255, -1)
cv2.namedWindow('img'Cv2.setmousecallback ('img',draw_circle)
while(1):
    cv2.imshow('img',img)
    if cv2.waitKey(1)==ord('q') :break
cv2.destroyAllWindows()
Copy the code

The output is:

Draw a rectangular

The following is a simple case to achieve the mouse draw rectangle operation.

The code is as follows:

import cv2
import numpy as np
drawing = False

ix,iy = -1, -1Def mouse_draw(event,x,y,flags,param):global ix,iy,drawing,mode
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix,iy = x,y
    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        cv2.rectangle(img,(ix,iy),(x,y),(255.0.0),1)
img = np.zeros((512.512.3), np.uint8)
cv2.namedWindow('img')
cv2.setMouseCallback('img',mouse_draw)
while(1):
    cv2.imshow('img',img)
    if cv2.waitKey(1)==ord('q') :break
cv2.destroyAllWindows()
Copy the code

The output is:

A series of articles will follow this month,

Wonderful article, please pay attention.