在本文中,我們將介紹如何在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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。