【问题标题】:OpenCV - Trackbar slider keeps going to zero with videoOpenCV - Trackbar滑块随着视频不断变为零
【发布时间】:2014-04-18 00:27:30
【问题描述】:

我正在尝试使用滑块来控制 HSV 屏蔽的下限和上限。我能够得到滑块,但不能让它保持我设定的位置;每次拉入新帧时,它都会一直回到零。

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()

【问题讨论】:

    标签: python opencv video trackbar


    【解决方案1】:

    您正在 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()
        ........................
        ........................
    

    【讨论】:

    • 我已将它们移出并尝试将用于分配 j 的行放入和退出循环,但我没有看到掩码发生变化。它使用 j = 0 而不是更新它。
    • Nvm,知道了。我为轨迹栏位置调用了错误的窗口
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    相关资源
    最近更新 更多