gpt4 book ai didi

python - 如何在 OpenCV 中设置轨迹栏的默认位置?

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:06 25 4
gpt4 key购买 nike

在 OpenCV 中,如何使用 createTrackbar 函数将默认 slider 位置设置为最大值?

我有几个 slider ,一些代表最小值,一些代表最大值。如果最大值的 slider 从最大值 (255) 而不是最小值 (0) 开始,那就太好了。

我环顾了 OpenCV documentation pages , 但我还没有找到解决方案。

import cv2
import numpy as np

def nothing(x):
pass

# Create a black image, a window
#img = np.zeros((300,512,3), np.uint8)
cv2.namedWindow('image')
cv2.namedWindow('hsv')
cv2.namedWindow('masq')
cap = cv2.VideoCapture(0)

# create trackbars for color change
cv2.createTrackbar('R-low','image',0,255,nothing)
cv2.createTrackbar('R-high','image',0,255,nothing)

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

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


while(1):
ret, img = cap.read()
# Convert BGR to HSV
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

cv2.imshow('image',img)
k = cv2.waitKey(1) & 0xFF
if k == 27:
break

# get current positions of four trackbars
rl = cv2.getTrackbarPos('R-low','image')
rh = cv2.getTrackbarPos('R-high','image')

gl = cv2.getTrackbarPos('G-low','image')
gh = cv2.getTrackbarPos('G-high','image')

bl = cv2.getTrackbarPos('B-low','image')
bh = cv2.getTrackbarPos('B-high','image')

lower = np.array([rl,gl,bl])
upper = np.array([rh,gh,bh])

print(rl)

img[:] = [bl,gl,rl]

# Threshold the HSV image to get only certain colors
mask = cv2.inRange(hsv, lower, upper)


res = cv2.bitwise_and(img,img, mask= mask)

cv2.imshow('image',img)
cv2.imshow('masq',mask)
cv2.imshow('hsv',hsv)


cv2.destroyAllWindows()

加载时,它最终看起来像这样:

enter image description here

最佳答案

只需使用值字段:

Python: cv.CreateTrackbar(trackbarName, windowName, value, count,onChange) → None

Parameters:
trackbarname – Name of the created trackbar.

winname – Name of the window that will be used as a parentof the created trackbar.

value – Optional pointer to an integervariable whose value reflects the position of the slider. Uponcreation, the slider position is defined by this variable.

count –Maximal position of the slider. The minimal position is always 0.

onChange – Pointer to the function to be called every time the sliderchanges position.

This function should be prototyped as voidFoo(int,void*); , where the first parameter is the trackbar positionand the second parameter is the user data (see the next parameter). Ifthe callback is the NULL pointer, no callbacks are called, but onlyvalue is updated. userdata – User data that is passed as is to thecallback. It can be used to handle trackbar events without usingglobal variables.

Source

关于python - 如何在 OpenCV 中设置轨迹栏的默认位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39362497/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com