Python OpenCV – setTrackbarMin()
在这篇文章中,我们将讨论如何设置轨迹条的最小位置。轨迹条的最小值可以通过python中OpenCV包的函数setTrackbarMin()来实现。这个函数在创建轨迹条后,在指定窗口中设置指定轨迹条的最小位置。
setTrackbarMin()函数
setTrackbarMin()函数设置轨迹条在窗口中的最小位置。它不返回任何东西。这个函数需要三个参数。第一个参数是轨迹条的名称,第二个参数是作为轨迹条父级的窗口名称,第三个参数是要设置给轨迹条的位置的新值。它返回无。
语法: cv.setTrackbarMax(trackbarname, winname, maxval)
参数。
- trackbarname:轨道栏的名称
- winname:轨道栏的父窗口的名称
- maxval:新的最大位置
返回值: None
示例1:
在这个例子中,我们创建了一个黑色图像的窗口。此外,我们使用createTrackbar()函数创建了一个轨迹条,并使用setTrackbarMin()函数将轨迹条的最小值设置为100。之后,我们将创建一个循环来显示图像和轨迹条。因此,每当我们移动轨迹条的位置时,黑色的色调就会改变。
# Python program for setTrackbarMin()
# Python OpenCV
# Importing the libraries OpenCV and numpy
import cv2
import numpy
# Create a function 'nothing' for
# creating trackbar
def nothing(x):
pass
# Creating a window with black image
img = numpy.zeros((300, 512, 3), numpy.uint8)
cv2.namedWindow('image')
# Creating trackbars for color change
cv2.createTrackbar('color_track', 'image', 0, 255, nothing)
# Setting minimum position of 'color_track'
# trackbar to 100
cv2.setTrackbarMin('color_track', 'image', 100)
# Create a loop for displaying image and
# trackbar
while(True):
# Display the image
cv2.imshow('image', img)
# Create a button for pressing and changing
# the window
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# Get current positions of trackbar
color = cv2.getTrackbarPos('color_track', 'image')
# Display color mixture
img[:] = [color]
# Close the window
cv2.destroyAllWindows()
输出:
示例2:
在这个例子中,我们已经创建了一个带有黑色图像的窗口。此外,我们使用createTrackbar()函数创建了三个红色、绿色和蓝色的轨迹条,并使用setTrackbarMin()函数将轨迹条的最小值设置为50、100和150。之后,我们创建一个循环,用于显示图像和轨迹条。因此,每当我们移动轨迹条的位置时,红、绿、蓝的色调就会改变。
# Python program for setTrackbarMin()
#Python OpenCV
# Importing the libraries OpenCV and numpy
import cv2
import numpy
# Create a function 'nothing' for
# creating trackbar
def nothing(x):
pass
# Creating a window with black image
img = numpy.zeros((300, 512, 3), numpy.uint8)
cv2.namedWindow('image')
# Creating trackbars for red color change
cv2.createTrackbar('Red', 'image', 0, 255, nothing)
# Creating trackbars for Green color change
cv2.createTrackbar('Green', 'image', 0, 255, nothing)
# Creating trackbars for Blue color change
cv2.createTrackbar('Blue', 'image', 0, 255, nothing)
# Setting minimum values of green color trackbar
cv2.setTrackbarMin('Green', 'image', 50)
# Setting minimum values of red color trackbar
cv2.setTrackbarMin('Red', 'image', 100)
# Setting minimum values of blue color trackbar
cv2.setTrackbarMin('Blue', 'image', 150)
# Create a loop for displaying image and trackbar
while(True):
# Display the image
cv2.imshow('image', img)
# Create a button for pressing and
# changing the window
k = cv2.waitKey(1) & 0xFF
if k == 27:
break
# Get current positions of red color trackbar
red_color = cv2.getTrackbarPos('Red', 'image')
# Get current positions of green color trackbar
green_color = cv2.getTrackbarPos('Green', 'image')
# Get current positions of blue color trackbar
blue_color = cv2.getTrackbarPos('Blue', 'image')
# Display color mixture
img[:] = [blue_color, green_color, red_color]
# Close the window
cv2.destroyAllWindows()
输出: