opencv练习题 使用轨迹栏创建颜色和画笔半径可调的Paint应用程序
import cv2 as cv
import numpy as np
img = np.zeros((512,512,3),np.uint8)
cv.namedWindow('image')
def nothing(x):
pass
def draw_circle(event,x,y,thick,param):
global ix,iy,drawing,color,thickness
if event == cv.EVENT_LBUTTONDOWN:
drawing = True
ix,iy = x,y
elif event == cv.EVENT_MOUSEMOVE:
if drawing == True:
cv.circle(img,(x,y),thickness,color,-1,cv.LINE_AA)
elif event == cv.EVENT_LBUTTONUP:
drawing = False
cv.circle(img,(x,y),thickness,color,-1,cv.LINE_AA)
cv.setMouseCallback('image',draw_circle,(0,0,255))
cv.createTrackbar('Red','image',0,255,nothing)
cv.createTrackbar('Green','image',0,255,nothing)
cv.createTrackbar('Blue','image',0,255,nothing)
cv.createTrackbar('Thickness','image',1,10,nothing)
color = [0,0,0]
drawing = False
thickness = 5
while(1):
cv.imshow('image',img)
if cv.waitKey(1) & 0xFF == ord('q'):
break
r = cv.getTrackbarPos('Red','image')
g = cv.getTrackbarPos('Green','image')
b = cv.getTrackbarPos('Blue','image')
thickness = cv.getTrackbarPos('Thickness','image')
color = [b,g,r]
cv.destroyAllWindows()
刚开始学习python还有opencv,代码可以按照要求运行,不过还有修改的地方