我的代码中的跟踪条没有显示任何值吗?


我正在遵循使用OpenCV HSV检测特定颜色的教程。为了识别正确的HSV值(如本教程所示),我添加了跟踪条来检测HSV的上下值。一切正常,但我没有在我的跟踪条上显示任何值。

我在使用PyCharm 2018.2.4的Macbook Pro OS 10.13.6上这样做。在我的虚拟环境中签入终端时,OpenCV显示为4.0.0-dev,python版本为3.7.1

下面是我的代码:

# script for detecting colours with OpenCV using HSV (Hue, Saturation, Value)
# credits https://www.youtube.com/watch?v=AMFhjir4WgQ

import cv2
import numpy as np

def nothing(x):
    #any function here
    pass

cap = cv2.VideoCapture(0) #captures video from the main camera,

# create a trackbar window to help tune the detection and find the HSV
cv2.namedWindow("Trackbars")

# create the trackbars for each HSV value
cv2.createTrackbar("L-H", "Trackbars", 0, 180, nothing)
cv2.createTrackbar("L-S", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("L-V", "Trackbars", 0, 255, nothing)
cv2.createTrackbar("U-H", "Trackbars", 180, 180, nothing)
cv2.createTrackbar("U-S", "Trackbars", 255, 255, nothing)
cv2.createTrackbar("U-V", "Trackbars", 255, 255, nothing)

while True:
    _, frame = cap.read() # read each video frame
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) # converts BGR colour to HSV

    l_h = cv2.getTrackbarPos("L-H", "Trackbars")
    l_s = cv2.getTrackbarPos("L-S", "Trackbars")
    l_v = cv2.getTrackbarPos("L-V", "Trackbars")
    u_h = cv2.getTrackbarPos("U-H", "Trackbars")
    u_s = cv2.getTrackbarPos("U-S", "Trackbars")
    u_v = cv2.getTrackbarPos("U-V", "Trackbars")

    # set upper and lower red values
    lower_red = np.array([l_h, l_s, l_v])
    upper_red = np.array([u_h, u_s, u_v])

    mask = cv2.inRange(hsv, lower_red, upper_red) # identify red in frame

    #cv2.imshow("Frame", frame) # opens a window to show original video being captured
    resize = cv2.resize(mask, (960, 540))
    cv2.imshow("Mask", resize) # opens a window to show the mask

    key = cv2.waitKey(1)
    if key == 27:
        break

cap.release()
cv2.destroyAllWindows()

转载请注明出处:http://www.cxyjjj.com/article/20230526/1613367.html