编程语言
首页 > 编程语言> > Python opencv学习-5创建带调色板的画板

Python opencv学习-5创建带调色板的画板

作者:互联网

test5:带调色板的画板,可改变线的粗细,颜色,缺点,其实是不断画圆,鼠标动作快了能看出执行间隔
import cv2
import numpy as np

drawing = False


# mouse callback function
def nothing(x):
    pass


def draw_circle(event, x, y, flags, param):
    global drawing
    s = cv2.getTrackbarPos('size', 'image')
    b = cv2.getTrackbarPos('B', 'image')
    r = cv2.getTrackbarPos('R', 'image')
    g = cv2.getTrackbarPos('G', 'image')

    if event == cv2.EVENT_LBUTTONDOWN:
        drawing = True

    elif event == cv2.EVENT_MOUSEMOVE:
        # cv2.circle(img,(x,y),s,(b,g,r),-1)
        if drawing:
            cv2.circle(img, (x, y), s, (b, g, r), -1)

    elif event == cv2.EVENT_LBUTTONUP:
        drawing = False


# Create a black image, a window and bind the function to window
img = np.zeros((1024, 1024, 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)
cv2.createTrackbar('size', 'image', 1, 255, nothing)
cv2.setMouseCallback('image', draw_circle)

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

标签:Python,image,cv2,调色板,event,opencv,nothing,getTrackbarPos,drawing
来源: https://blog.csdn.net/qq_36071362/article/details/113497270