CV4 simple human-computer interaction application based on mouse callback function and track color matching

Q: use the track bar to create a Paint application with adjustable color and brush radius?

A mouse callback function

Creating a mouse callback function has a specific format, which is the same everywhere. It is only different in function.

Therefore, in the following code, our mouse callback function can do one thing - draw a circle where we double-click

import cv2
import numpy as np

#Define a mouse callback function. If you want to know more mouse events, you can implement it through the following shielded code
# import cv2 as cv
# events = [i for i in dir(cv) if 'EVENT' in i]
# print( events )

#Define the processing event of a mouse callback function
def draw_circle(event,x,y,flags,param):
    if event == cv2.EVENT_LBUTTONDBLCLK:
        cv2.circle(img,(x,y),100,(0,0,255),-1)


img = np.zeros((512,512,3),np.uint8)
cv2.namedWindow('img')

# Mouse event check, input parameters: 1 Picture name, 2 Defined mouse events
cv2.setMouseCallback('img',draw_circle)

while(1):
    cv2.imshow('img',img)
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()
  • cv2.setMouseCallback function

Function: mouse event check

The input parameters are: 1 Picture name, 2 Defined mouse events

Here are some common mouse events

Event nameMouse activity
EVENT_MOUSEMOVEMove the mouse
EVENT_LBUTTONDOWNLeft click
EVENT_RBUTTONDOWNRight click
EVENT_MBUTTONDOWNMiddle click
EVENT_LBUTTONUPLeft button release
EVENT_RBUTTONUPRight click to release
EVENT_MBUTTONUPMiddle key release
EVENT_LBUTTONDBLCLKDouble left click
EVENT_RBUTTONDBLCLKRight click double click
EVENT_MBUTTONDBLCLKMiddle double click

Next, we need to do a more advanced operation - draw a rectangle or circle by dragging the mouse (depending on the mode we choose)

import cv2
import numpy as np

drawing = False
mode = True
ix,iy=-1,-1

#Multi condition mouse judgment function
def draw_circle(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_MOUSEMOVE:
        if drawing == True:
            if mode == True:
                cv2.rectangle(img,(ix,iy),(x,y),(0,0,0),5)
            else:
                cv2.circle(img,(x,y),5,(0,0,255),-1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        if mode == True:
            cv2.rectangle(img,(ix,iy),(x,y),(0,255,0),5)
        else:
            cv2.circle(img,(x,y),5,(0,0,255),-1)

img = np.zeros((720,1280,3),np.uint8)
cv2.namedWindow('img')

# Mouse event check, input parameters: 1 Picture name, 2 Defined mouse events
cv2.setMouseCallback('img',draw_circle)

while(1):
    cv2.imshow('img',img)
    if cv2.waitKey(50) & 0xFF == ord('m'):
        mode=~mode
    if cv2.waitKey(20) & 0xFF == 27:
        break
cv2.destroyAllWindows()
  • Global means to define a global variable in python
  • ix,iy=-1,-1 means filling the figure
  • Take the inverse operation in python as: mode=~mode

In essence, we just add a few more conditional judgments to our def function

If your opencv Python version is 4.5 x. Then you will report the following errors:

error: (-27:Null pointer) NULL window: 'TrackBars' in function 'cvGetTrackbarPos'

To eliminate this error, we need to reduce the version of OpenCV Python to 4.1 x

For details, please refer to this blog

How can I fix get trackbar position error in pycharm?

The second track bar acts as a palette

We will create a simple application to display the colors you specify. You have a window that displays colors,

And three tracking bars for specifying B, G and R colors. Slide the track bar and change the window color accordingly.

By default, the initial color is set to black

import numpy as np
import cv2

def nothing(x):
    pass

image = np.zeros((300,512,3),np.uint8)
cv2.namedWindow('image')

cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)

switch = '0:OFF \n 1:ON'
cv2.createTrackbar(switch,'image',0,1,nothing)
while(1):
    cv2.imshow('image',image)
    if cv2.waitKey(20)&0xFF == ord('q'):
        break

    r = cv2.getTrackbarPos('R','image')
    g = cv2.getTrackbarPos('G','image')
    b = cv2.getTrackbarPos('B','image')
    s = cv2.getTrackbarPos(switch,'image')

    if s == 0:
        image[:] = 0
    else:
        image[:] = [b,g,r]

cv2.destroyAllWindows()

cv2. The image window name named by namedwindow ('image ') is the * * window name to be displayed * * of the following functions, which cannot be changed

  • cv2.createTrackbar function

Function: create a track visible on the picture for users to control

Input parameters: 1 Name 2 The name of the window to be displayed (you must define it first and the name must be the same)

​ 3. Min. 4 Max. 5 Event function (here is nothing, i.e. do nothing)

  • cv2.getTrackbarPos function

Function: obtain the data changed by the user in real time

Input parameters: 1 Track name 2 Window name

Third, use the track bar to create a Paint application with adjustable color and brush radius

import numpy as np
import cv2

ix,iy=-1,-1
drawing=False

#Multi condition mouse judgment function
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing,mode,b,g,r

# The function of drawing is to record when the track starts and ends
    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True
        ix,iy = x,y
    elif event == cv2.EVENT_MOUSEMOVE:
        if drawing == True:
           cv2.circle(image,(x,y),size,(b,g,r),-1)
    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False
        cv2.circle(image,(x,y),size,(b,g,r),-1)

# In order to cooperate with CV2 The createtrackbar function uses
def nothing(x):
    pass

# Create a white canvas
image = np.zeros((300,512,3),np.uint8)+255
cv2.namedWindow('image')

# Create tracks, which are R,G,B, and brush size
cv2.createTrackbar('R','image',0,255,nothing)
cv2.createTrackbar('G','image',0,255,nothing)
cv2.createTrackbar('B','image',0,255,nothing)
cv2.createTrackbar('SIZE','image',0,10,nothing)


# Mouse event check, input parameters: 1 Picture name, 2 Defined mouse events
cv2.setMouseCallback('image',draw_circle)

while(1):
    cv2.imshow('image',image)
    if cv2.waitKey(20)&0xFF == ord('q'):
        break

# Get the value of track in real time
    r = cv2.getTrackbarPos('R', 'image')
    g = cv2.getTrackbarPos('G', 'image')
    b = cv2.getTrackbarPos('B', 'image')
    size = cv2.getTrackbarPos('SIZE','image')


cv2.destroyAllWindows()

Size is the size of the brush, and ix/iy is the position of the point pressed for the first time

Four renderings


R/G/B/Size can be adjusted to meet the requirements of the topic~

Keywords: Python OpenCV AI Computer Vision image identification

Added by manimoor on Fri, 04 Feb 2022 15:00:44 +0200