2014-04-18 27 views
1

我正在嘗試使用滑塊來控制HSV掩蔽的下限和上限。我能夠得到滑塊,但無法讓它保持我設置的位置;每一個新的幀的時間拉它一直要回零。OpenCV - 軌道滑塊一直跟着視頻爲零

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

回答

2

你正在創建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() 
    ........................ 
    ........................ 
+0

我搬到那些並試圖把線在國內外享有很高的循環分配Ĵ但我沒有看到屏蔽的變化。它使用j = 0而不是更新它。 – ss32

+0

Nvm,明白了。我打電話給trackbar位置的窗口是錯誤的 – ss32