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

标签 python opencv video trackbar

我正在尝试使用 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/

相关文章:

python - 基于颜色python的对象边界框

video - 有没有办法使用 ColdFusion 获取视频文件的持续时间?

python - 如何将 Django 模型对象转换为字典并且仍然拥有它们的外键?

cvSet() 如何使用该函数仅将颜色设置为像素

python - Python "float"和 PostgreSQL "double precision"的 float

c++ - OpenCV:如何有效地将 Mat2d 矩阵的每个元素乘以 Mat1d 矩阵

php - 如何解码youtube api v3 json响应

javascript - 在灯箱或页面加载时打开 YouTube 视频

python - 在Python中生成pdf文件校验和时排除元数据

python - 根据字典中是否存在过滤 numpy 数组