Trackbar with video keeps giong to zero

asked 2014-04-17 19:25:20 -0500

ss32 gravatar image

I'm trying to use the slider to control my lower and upper bounds for HSV masking. I'm able to get the slider but can't get it to hold the position I set; it keeps going back to zero each time a new frame is pulled in.

import numpy as np
import cv2

def nothing(x):

cap = cv2.VideoCapture(0)


    # Make a window for the video feed  

    # Capture frame-by-frame
    ret, frame =

    # Make the trackbar used for HSV masking    

    # 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

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

# When everything is done, release the capture
edit retag flag offensive close merge delete



just move the trackbar / window creation out of the loop

berak gravatar imageberak ( 2014-04-18 01:50:11 -0500 )edit

edit: Got it working. You were right, I just had some stuff out of place, thanks.

ss32 gravatar imagess32 ( 2014-04-18 09:49:10 -0500 )edit