Create a Paint application with adjustable colors and brush radius using trackbars.

前言

题目地址:https://docs.opencv.org/4.x/d9/dc8/tutorial_py_trackbar.html
题目内容:
Create a Paint application with adjustable colors and brush radius using trackbars. For drawing, refer previous tutorial on mouse handling.

一、Application

Source Code

# 开发时间:2022/2/20  20:10
# Create a Paint application with adjustable colors and brush radius using trackbars. For drawing, refer previous tutorial on mouse handling.
import cv2 as cv
import numpy as np

def nothing():
    pass

drawing = False # true if mouse is pressed
ix,iy = -1,-1


# mouse callback function
def draw_circle(event,x,y,flags,param):
    global ix,iy,drawing,Radius
    if event == cv.EVENT_LBUTTONDOWN:
        drawing = True
        ix,iy = x,y

    # elif event == cv.EVENT_MOUSEMOVE:
    #     cv.circle(img,(x,y),5,(0,0,255),-1)

    elif event == cv.EVENT_LBUTTONUP:
        cv.circle(img,(x,y),Radius,(r,g,b),5)

img = np.zeros((512,512,3),np.uint8)
cv.namedWindow("image")
cv.setMouseCallback("image",draw_circle) # 注意draw_circle写的时候不要加括号
# create trackbars for color change
cv.createTrackbar("R","image",0,255,nothing)
cv.createTrackbar("G","image",0,255,nothing)
cv.createTrackbar("B","image",0,255,nothing)
cv.createTrackbar("Radius","image",0,100,nothing)

while True:
    cv.imshow("image",img)
    k = cv.waitKey(1) & 0xFF
    if k == 27:
        break

    # get current positions of four trackbars
    r = cv.getTrackbarPos("R",'image')
    g = cv.getTrackbarPos("G",'image')
    b = cv.getTrackbarPos("B",'image')
    Radius = cv.getTrackbarPos("Radius",'image')


cv.destroyAllWindows()

Result

result

总结

参考代码地址:
https://docs.opencv.org/4.x/db/d5b/tutorial_py_mouse_handling.html
https://docs.opencv.org/4.x/d9/dc8/tutorial_py_trackbar.html

猜你喜欢

转载自blog.csdn.net/booze_/article/details/123036041