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

这篇博客介绍了一个使用OpenCV实现的涂鸦应用,该应用允许用户通过鼠标绘制,并可以调整颜色和画笔半径。应用中包含了鼠标事件处理和轨迹条(trackbar)功能,用户可以通过轨迹条改变圆圈的颜色和半径。代码示例详细展示了如何结合鼠标回调函数和轨迹条创建这个交互式绘图应用。
摘要由CSDN通过智能技术生成

前言

题目地址: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

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

布兹学长

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值