Jacob Shulkin
Jacob Shulkin

Reputation: 1

Why will the trackbars in this Python Open CV program not display?

I'm currently learning how to use Open CV for python and I am trying to write a program to see an image in real time from a webcam based off of an hsv value range. When I run the program I am able to get the webcam to work (it shows a black screen as expected) but the trackbars to adjust the hsv range are not showing for some reason. Anyone have any solutions? Thanks.

import cv2
import numpy as np
cap = cv2.VideoCapture(0)

def nothing(x):
    pass


#creates three trackbars for color change
cv2.createTrackbar('H','frame',0,255,nothing)
cv2.createTrackbar('S','frame',0,255,nothing)
cv2.createTrackbar('V','frame',0,255,nothing)

while(1):
    # Capture frame-by-frame
    _, frame = cap.read()

    #creates trackbars
    h = cv2.getTrackbarPos('H','frame')
    s = cv2.getTrackbarPos('S','frame')
    v = cv2.getTrackbarPos('V','frame')


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

    # define color strenght parameters in HSV
    weaker = np.array([h+10, s+10, v+10])
    stronger = np.array([h-10,s-10,v-10])


    # Threshold the HSV image to obtain input color
    mask = cv2.inRange(hsv, weaker, stronger)

    #displays mask
    cv2.imshow('Result',mask)

    #terminates program
    if cv2.waitKey(1) == ord('q'):
        break

cv2.waitKey(0)
cv2.destroyAllWindows()

Upvotes: 0

Views: 2977

Answers (1)

101
101

Reputation: 8999

The second argument of cv2.createTrackbar('H','frame',0,255,nothing) should be the name of the window that will show the trackbars. You've used frame, but there doesn't seem to be a window named frame opened in your code. You could do so by adding

cv2.namedWindow('frame')

or by changing your display line to

cv2.imshow('frame', mask)

Upvotes: 1

Related Questions