Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to operate OpenCV4.X Slider Bar

2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

How to carry out OpenCV4.X slider operation, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, people who have this need can learn, I hope you can gain something.

I. Slider bar

createTrackbar(trackbarName, windowName, value, count, onChange): Create slider

getTrackbarPos(trackbarname, winname): Get the slider value

trackbarName: slider name windowName: window name value: default value of parameter count: maximum value of parameter onChange: callback function executed apply one

Create an application that displays the specified colors: there is a window that displays colors and three sliding track bars for specifying B, G, R color values. You can slide the track bar and change the color. The initial color is set to black.

code

import numpy as np

import cv2

def nothing(x):

Pass #do nothing.

#Create initial black image and window

img = np.zeros((300,512,3), np.uint8)

cv2.namedWindow('image')

#Create R, G, B sliders

cv2.createTrackbar('R', 'image', 0, 255, nothing)

cv2.createTrackbar('G', 'image', 0, 255, nothing)

cv2.createTrackbar('B', 'image', 0, 255, nothing)

#Create switches via slider bar, simulate buttons

#Another important application of the track bar is to use it as a button or switch. By default,

# OpenCV has no button functionality. Therefore, tracking bars can be used to obtain such functionality.

#In our app, a switch was created where the app only works when the switch is open

#Valid, otherwise the screen is always black

switch = '0 : OFF \n1 : ON'

cv2.createTrackbar(switch, 'image', 0, 1, nothing)

while True:

cv2.imshow('image',img)

k = cv2.waitKey(1) & 0xFF

if k == 27:

break

#Get slider position set RGB values

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()

2. application two

Slider bar controls video playback progress

code

import cv2

def nothing(emp):

pass

video = 'input.mp4'

cv2.namedWindow('video')

cap = cv2.VideoCapture(video)

frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))

loop_flag = 0

pos = 1

cv2.createTrackbar('time', 'video', 0, frames, nothing)

while 1:

if loop_flag == pos:

loop_flag = loop_flag + 1

cv2.setTrackbarPos('time', 'video', loop_flag)

else:

pos = cv2.getTrackbarPos('time', 'video')

loop_flag = pos

cap.set(cv2.CAP_PROP_POS_FRAMES, pos)

ret, img = cap.read()

cv2.imshow('video', img)

if cv2.waitKey(1) & loop_flag == frames:

Break, did reading the above help you? If you still want to have further understanding of related knowledge or read more related articles, please pay attention to the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report