1. Experimental objectives
(1), mouse as pen
(2), function: setMouseCallback()
(3), sliding palette
(4), function cv.createTrackbar(), cv.getTrackbarPos()
2. Experimental steps
(1), install the necessary library
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
(2), View the operation that supports the mouse
events = [i for i in dir(cv) if 'EVENT' in i]
print(events)
(3), double-click the mouse to draw a circle
cv2.setMouseCallback (winname, onMouse, userdata=0)
winname: custom window name
onMouse: callback function of mouse event (executed when mouse event occurs)
userdata: passed to the callback function optional parameters for
#双击鼠标画一个圆
import numpy as np
import cv2 as cv
def draw_circle(event,x,y,flags,param):
if event==cv.EVENT_LBUTTONDBLCLK:
cv.circle(img,(x,y),100,(255,0,0),-1)
img = np.zeros((512,512,3),np.uint8)#创建黑色图像
cv.namedWindow('image')#创建一个窗口
cv.setMouseCallback('image',draw_circle)#函数与窗口进行绑定
while(1):
cv.imshow('image',img)
if cv.waitKey(20) & 0xFF==27:
break
cv.destroyAllWindows()
(4) Drag the mouse to draw a rectangle or circle
The callback function contains two parts, one part draws a rectangle and the other part draws a curve
drawing = False #默认False,按下鼠标为True
mode = True #if True,画出矩形,按'm'切换曲线
ix,iy=-1,-1
#响应鼠标函数
def draw_circle(event,x,y,flags,param):
global ix,iy,drawing,mode
if event == cv.EVENT_LBUTTONDOWN:#左键击下
drawing = True
ix,iy=x,y
elif event==cv.EVENT_MOUSEMOVE:#鼠标移动
if drawing == True:
if mode==True:
cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv.circle(img,(x,y),5,(0,0,255),-1)
elif event==cv.EVENT_LBUTTONUP:#左键弹起
drawing = False
if mode == True:
cv.rectangle(img,(ix,iy),(x,y),(0,255,0),-1)
else:
cv.circle(img,(x,y),5,(0,0,255),-1)
#将函数与窗口进行绑定
img = np.zeros((512,512,3),np.uint8)
cv.namedWindow('image')
cv.setMouseCallback('image',draw_circle)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
#按下'm'键切换模式
if k==ord('m'):#获得按键编码
mode = not mode
elif k==27:#按下ESC键退出
break
cv.destroyAllWindows()
(5) Slider palette
cv.getTrackbarPos (trackbarname, winname)
The first parameter is the name of the slider,
the second is the window where it is located, and
the return value is the value of the slider.
cv.createTrackbar ("scale", "display", 0, 100, self.opencv_calibration_node.on_scale)
The first parameter is the name of the slider,
the second parameter is the name of the window where the slider is placed, and
the third parameter It is the default value of the slider,
the fourth parameter is the maximum value of the slider, and
the fifth parameter is the callback function, which will be called every time you slide.
def nothing(x):
pass
img = np.zeros((300,512,3),np.uint8)#创建黑色图片
cv.namedWindow('image')#创建窗口
#创建滑动条
cv.createTrackbar('R','image',0,255,draw_circle)
cv.createTrackbar('G','image',0,255,draw_circle)
cv.createTrackbar('B','image',0,255,draw_circle)
#创建开关
switch = '0 : OFF \n1 : ON'
cv.createTrackbar(switch,'image',0,1,nothing)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k==27:
break
r = cv.getTrackbarPos('R','image')
g = cv.getTrackbarPos('G','image')
b = cv.getTrackbarPos('B','image')
s = cv.getTrackbarPos(switch,'image')
# print(r,g,b,s)
if s==0:
img[:] = 0
else:
img[:] = [b,g,r]
cv.destroyAllWindows()
(6), draw with the mouse, use the palette to adjust the color
img = np.zeros((300,512,3),np.uint8)#创建黑色图片
cv.namedWindow('image')#创建窗口
#创建滑动条
r = cv.createTrackbar('R','image',0,255,nothing)
g = cv.createTrackbar('G','image',0,255,nothing)
b = cv.createTrackbar('B','image',0,255,nothing)
drawing = False #默认False,按下鼠标为True
ix,iy=-1,-1
#响应鼠标函数
def nothing(x):
pass
def draw(event,x,y,flags,param):
global ix,iy,drawing
r = cv.getTrackbarPos('R','image')
g = cv.getTrackbarPos('G','image')
b = cv.getTrackbarPos('B','image')
if event == cv.EVENT_LBUTTONDOWN:#左键击下
drawing = True
ix,iy=x,y
elif event==cv.EVENT_MOUSEMOVE:#鼠标移动
if drawing == True:
cv.circle(img,(x,y),5,(b,g,r),-1)
elif event==cv.EVENT_LBUTTONUP:#左键弹起
drawing = False
cv.circle(img,(x,y),5,(b,g,r),-1)
cv.setMouseCallback('image',draw)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k==27:
break
cv.destroyAllWindows()