opencv学习第6课官方练习实现 Create a Paint application with adjustable colors and brush radius using trackbars
练习题目来源(网址最下方):https://docs.opencv.org/4.1.0/d9/dc8/tutorial_py_trackbar.html
import numpy as np
import cv2 as cv
brush_color = [80,80,80]
brush_size = 2
drawing = False
# mouse callback function
def draw_circle(event,x,y,flags,param):
global brush_color,brush_size,drawing
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
elif event == cv.EVENT_MOUSEMOVE:
if drawing == True:
cv.circle(img,(x,y),brush_size,brush_color,-1)
elif event == cv.EVENT_LBUTTONUP:
drawing = False
cv.circle(img,(x,y),brush_size,brush_color,-1)
def doChangeColor(x):
global brush_color
tem = cv.getTrackbarPos('brush-color','image')
brush_color = [tem,tem,tem]
def doChangeSize(x):
global brush_size
tem = cv.getTrackbarPos('brush-size','image')
brush_size = tem
img = np.zeros((512,512,3),np.uint8)
cv.namedWindow('image')
cv.createTrackbar('brush-color','image',80,255,doChangeColor)
cv.createTrackbar('brush-size','image',2,5,doChangeSize)
cv.setMouseCallback('image',draw_circle)
while(1):
cv.imshow('image',img)
k = cv.waitKey(1) & 0xFF
if k == 27:
break
# Destroy all windows that have been created
cv.destroyAllWindows()
参考内容:
https://blog.csdn.net/Ibelievesunshine/article/details/89351928
https://blog.csdn.net/Ibelievesunshine/article/details/89353097