1、实验目标
(1)、鼠标当笔
(2)、函数:setMouseCallback()
(3)、滑动调色板
(4)、函数cv.createTrackbar(),cv.getTrackbarPos()
2、实验步骤
(1)、安装必要的库
pip install opencv-python -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install numpy -i https://pypi.tuna.tsinghua.edu.cn/simple
(2)、查看支持鼠标的操作
events = [i for i in dir(cv) if 'EVENT' in i]
print(events)
(3)、双击鼠标画出一个圆
cv2.setMouseCallback(winname, onMouse, userdata=0)
winname: 自定义的窗口名
onMouse:鼠标事件的回调函数(在发生鼠标事件时执行)
userdata:传递给回调函数的可选参数
#双击鼠标画一个圆
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)、拖动鼠标绘制矩形或圆圈
回调函数包含两部分,一部分画矩形,一部分画曲线
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,modeif event == cv.EVENT_LBUTTONDOWN:#左键击下drawing = Trueix,iy=x,yelif 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 = Falseif 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 modeelif k==27:#按下ESC键退出break
cv.destroyAllWindows()
(5)滑动条调色板
cv.getTrackbarPos(trackbarname, winname)
第一个参数是滑动条名字,
第二个时所在窗口,
返回值是滑动条的数值。
cv.createTrackbar(“scale”, “display”, 0, 100, self.opencv_calibration_node.on_scale)
第一个参数时滑动条的名字,
第二个参数是滑动条被放置的窗口的名字,
第三个参数是滑动条默认值,
第四个参数时滑动条的最大值,
第五个参数时回调函数,每次滑动都会调用回调函数。
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) & 0xFFif k==27:breakr = 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[:] = 0else:img[:] = [b,g,r]cv.destroyAllWindows()
(6)、鼠标绘图,使用调色板调节颜色
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,drawingr = cv.getTrackbarPos('R','image')g = cv.getTrackbarPos('G','image')b = cv.getTrackbarPos('B','image')if event == cv.EVENT_LBUTTONDOWN:#左键击下drawing = Trueix,iy=x,yelif event==cv.EVENT_MOUSEMOVE:#鼠标移动if drawing == True:cv.circle(img,(x,y),5,(b,g,r),-1)elif event==cv.EVENT_LBUTTONUP:#左键弹起drawing = Falsecv.circle(img,(x,y),5,(b,g,r),-1)cv.setMouseCallback('image',draw)
while(1):cv.imshow('image',img)k = cv.waitKey(1) & 0xFFif k==27:break
cv.destroyAllWindows()