【OpenCV + Python】实现滚动条功能

import cv2
import numpy as np
def nothing(x):
    pass
img=np.zeros((300,512,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)
switch='0:OFF\n1:ON'
cv2.createTrackbar(switch,'image',0,1,nothing)
while(1):
    cv2.imshow('image',img)
    k=cv2.waitKey(1)&0xFF
    if k==27:
        break
    r=cv2.getTrackbarPos('R','image')
    g=cv2.getTrackbarPos('G','image')
    b=cv2.getTrackbarPos('B','image')
    s=cv2.getTrackbarPos(switch,'image')
    if s==0:
        img[:]=0
    else:
        img[:]=[b,g,r]
cv2.destroyAllWindows()

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,我可以为您提供一个基于OpenCV滚动条交互程序的设计思路: 1. 首先,需要使用OpenCV库,可以使用Python或C++来实现。在这个例子中,我们以Python为例来进行说明。 2. 首先导入OpenCV库和numpy库,并创建一个空图像。 ```python import cv2 import numpy as np img = np.zeros((500, 500, 3), np.uint8) ``` 3. 接下来,创建两个滚动条元素,用于控制圆形的实心与空心以及半径的大小。 ```python cv2.namedWindow("image") def nothing(x): pass cv2.createTrackbar('Circle Type', 'image', 0, 1, nothing) cv2.createTrackbar('Circle Radius', 'image', 10, 100, nothing) ``` 4. 在一个while循环中,监听滚动条元素的变化事件,并相应地更新绘图属性。比如,当滚动条的值发生变化时,可以更新线条宽度、颜色等等。 ```python while True: cv2.imshow("image", img) k = cv2.waitKey(1) & 0xFF if k == 27: break circle_type = cv2.getTrackbarPos('Circle Type', 'image') circle_radius = cv2.getTrackbarPos('Circle Radius', 'image') if circle_type == 0: color = (255, 255, 255) thickness = -1 else: color = (0, 0, 0) thickness = 2 cv2.circle(img, (250, 250), circle_radius, color, thickness) ``` 5. 最后,需要显示图像并等待用户按下ESC键退出程序。 ```python cv2.destroyAllWindows() ``` 完整代码如下: ```python import cv2 import numpy as np img = np.zeros((500, 500, 3), np.uint8) cv2.namedWindow("image") def nothing(x): pass cv2.createTrackbar('Circle Type', 'image', 0, 1, nothing) cv2.createTrackbar('Circle Radius', 'image', 10, 100, nothing) while True: cv2.imshow("image", img) k = cv2.waitKey(1) & 0xFF if k == 27: break circle_type = cv2.getTrackbarPos('Circle Type', 'image') circle_radius = cv2.getTrackbarPos('Circle Radius', 'image') if circle_type == 0: color = (255, 255, 255) thickness = -1 else: color = (0, 0, 0) thickness = 2 cv2.circle(img, (250, 250), circle_radius, color, thickness) cv2.destroyAllWindows() ``` 在这个示例中,我们使用了cv2.createTrackbar函数来创建滚动条元素,并使用cv2.getTrackbarPos函数来获取滚动条的值。通过更新绘图属性和重新绘制圆形,实现滚动条交互程序的功能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

胖子工作室

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

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

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

打赏作者

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

抵扣说明:

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

余额充值