Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to operate OpenCV4.X Slider Bar

Shulou Source: shulou.com Published: 2022-06-01 12:54:45 10月01日 Update

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.

Tags: Application color application button program black code function parameter name track help maximum effective clear important three position content function Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi NVidia Huawei MySQL Docker