在本文中,我们将介绍如何在OpenCV中使用setTrackbarMax() python。
让用户通过线性滑动滑块来选择某个值范围内的特定值的 GUI 元素称为轨迹栏。该元素与滚动类似,但它允许用户选择特定值及其最小和最大限制。在本文中,我们将讨论如何设置轨迹栏的最大位置。轨迹栏的最大值可以使用函数setTrackbarMax()来实现。该函数设置创建轨迹条后指定轨迹条在指定窗口中的最大位置。
用法: cv.setTrackbarMax(trackbarname, winname, maxval)
参数:
- trackbarname:轨迹栏名称
- winname:轨迹栏父窗口的名称
- maxval:新的最大位置
返回:None
示例1:
在此示例中,我们创建了一个带有黑色图像的窗口。此外,我们使用createTrackbar()函数创建一个轨迹栏,并使用setTrackbarMax()函数将轨迹栏的最大值设置为200。之后,我们将创建一个循环来显示图像和轨迹栏。因此,每当我们移动轨迹栏位置时,黑色的阴影都会发生变化。
Python3
# Python program for setTrackbarMax()
#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 maximum position of 'color_track' trackbar to 200
cv2.setTrackbarMax('color_track', 'image', 200)
# 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()函数创建红、绿、蓝三个颜色的轨迹条,并使用setTrackbarMax()函数将轨迹条的最大值设置为100、200和150。稍后,我们创建一个循环来显示图像和轨迹栏。因此,每当我们移动轨迹栏位置时,红色、绿色和蓝色的色调就会发生变化。
Python
# Python program for setTrackbarMax()
#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 maximum values of green color trackbar
cv2.setTrackbarMax('Green', 'image', 100)
# Setting maximum values of red color trackbar
cv2.setTrackbarMax('Red', 'image', 200)
# Setting maximum values of blue color trackbar
cv2.setTrackbarMax('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()
输出:
相关用法
- Python OpenCV cv2.circle()用法及代码示例
- Python OpenCV cv2.blur()用法及代码示例
- Python OpenCV cv2.ellipse()用法及代码示例
- Python OpenCV cv2.cvtColor()用法及代码示例
- Python OpenCV cv2.copyMakeBorder()用法及代码示例
- Python OpenCV cv2.imread()用法及代码示例
- Python OpenCV cv2.imshow()用法及代码示例
- Python OpenCV cv2.imwrite()用法及代码示例
- Python OpenCV cv2.putText()用法及代码示例
- Python OpenCV cv2.rectangle()用法及代码示例
- Python OpenCV cv2.arrowedLine()用法及代码示例
- Python OpenCV cv2.erode()用法及代码示例
- Python OpenCV cv2.line()用法及代码示例
- Python OpenCV cv2.flip()用法及代码示例
- Python OpenCV cv2.transpose()用法及代码示例
- Python OpenCV cv2.rotate()用法及代码示例
- Python OpenCV cv2.polylines()用法及代码示例
- Python OpenCV Canny()用法及代码示例
- Python OpenCV destroyAllWindows()用法及代码示例
- Python OpenCV Filter2D()用法及代码示例
- Python OpenCV getgaussiankernel()用法及代码示例
- Python OpenCV getRotationMatrix2D()用法及代码示例
- Python OpenCV getTrackbarPos()用法及代码示例
- Python OpenCV haveImageReader()用法及代码示例
- Python OpenCV imdecode()用法及代码示例
注:本文由纯净天空筛选整理自vin8rai大神的英文原创作品 Python OpenCV setTrackbarMax() Method。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。