gpt4 book ai didi

python - OpenCV - Trackbar slider 随着视频不断归零

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

我正在尝试使用 slider 来控制 HSV 掩蔽的下限和上限。我能够获得 slider ,但无法使其保持在我设置的位置;每次拉入新帧时,它都会回到零。

import numpy as np
import cv2

def nothing(x):
pass

cap = cv2.VideoCapture(0)

while(True):

# Make a window for the video feed
cv2.namedWindow('frame',cv2.CV_WINDOW_AUTOSIZE)

# Capture frame-by-frame
ret, frame = cap.read()

# Make the trackbar used for HSV masking
cv2.createTrackbar('HSV','frame',0,255,nothing)

# Name the variable used for mask bounds
j = cv2.getTrackbarPos('HSV','image')

# Convert BGR to HSV
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

# define range of color in HSV
lower = np.array([j-10,100,100])
upper = np.array([j+10,255,255])

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

# Bitwise-AND mask the original image
res = cv2.bitwise_and(frame,frame, mask= mask)

# Display the resulting frame
cv2.imshow('frame',res)

# Press q to quit
if cv2.waitKey(3) & 0xFF == ord('q'):
break


# When everything is done, release the capture
cap.release()
cv2.destroyAllWindows()

最佳答案

您正在 while 循环中创建轨迹栏,这就是为什么您在每一帧上都获得新的轨迹栏。

所以改变你的代码,比如,

# Make a window for the video feed  
cv2.namedWindow('frame',cv2.CV_WINDOW_AUTOSIZE)
# Make the trackbar used for HSV masking
cv2.createTrackbar('HSV','frame',0,255,nothing)

while(True):

# Capture frame-by-frame
ret, frame = cap.read()
........................
........................

关于python - OpenCV - Trackbar slider 随着视频不断归零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23145613/

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