如何给用户输入以检测 opencv 视频中的特定颜色?

How to give user inputs to detect a specific color from video in opencv?

我在 python 中编写了一段代码来检测 OpenCV 中的红色。我得到了正确的输出,但我想在代码中给用户输入以检测特定颜色。例如:如果我将蓝色作为用户输入,它应该只在输出中显示蓝色。我还想添加一些属性作为输出,例如检测到该对象的时间和实时视频的位置。我是 python 和 opencv 的新手,如果我能得到一些指导就太好了。

我现有的代码如下:

import cv2
import numpy as np

# Capture the input frame from webcam
def get_frame(cap, scaling_factor):
    # Capture the frame from video capture object
    ret, frame = cap.read()

    # Resize the input frame
    frame = cv2.resize(frame, None, fx=scaling_factor,
            fy=scaling_factor, interpolation=cv2.INTER_AREA)

    return frame

if __name__=='__main__':
    cap = cv2.VideoCapture(0)
    scaling_factor = 0.5

    # Iterate until the user presses ESC key
    while True:
        frame = get_frame(cap, scaling_factor)

        # Convert the HSV colorspace
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        # Define 'blue' range in HSV colorspace
        lower = np.array([60,100,100])
        upper = np.array([180,255,255])

        # Threshold the HSV image to get only blue color
        mask = cv2.inRange(hsv, lower, upper)

        # Bitwise-AND mask and original image
        res = cv2.bitwise_and(frame, frame, mask=mask)
        res = cv2.medianBlur(res, 5)

        cv2.imshow('Original image', frame)
        cv2.imshow('Color Detector', res)

        # Check if the user pressed ESC key
        c = cv2.waitKey(5)
        if c == 27:
            break

    cv2.destroyAllWindows()   

您的代码似乎可以正常工作。查看关于转换 from RGB to HSV 的方程式,记住 OpenCV 中的边界在 H 通道的 0-180 和 SV 通道的 0-255 之间,我们可以概括代码以适用于所有颜色。

import cv2
import numpy as np

# Capture the input frame from webcam
def get_frame(cap, scaling_factor):
    # Capture the frame from video capture object
    ret, frame = cap.read()

    # Resize the input frame
    frame = cv2.resize(frame, None, fx=scaling_factor,
            fy=scaling_factor, interpolation=cv2.INTER_AREA)

    return frame

# Define the color range in HSV colorspace
lower1 = np.array([0,100,100])
upper1 = np.array([60,255,255])
lower2 = np.array([120,100,100])
upper2 = np.array([180,255,255])
def color_range(degree):
    # Degree is between 0-360
    # OpenCV uses 0-180 range for H channel
    degree = int(degree/2);
    global lower1, upper1, lower2, upper2
    if degree < 60:
        lower1 = np.array([int(0),100,100])
        upper1 = np.array([degree+60,255,255])
        lower2 = np.array([degree+120,100,100])
        upper2 = np.array([int(180),255,255])
    elif degree > 120:
        lower1 = np.array([degree-60,100,100])
        upper1 = np.array([int(180),255,255])
        lower2 = np.array([int(0),100,100])
        upper2 = np.array([degree-120,255,255])
    else:
        lower1 = np.array([degree-60,100,100])
        upper1 = np.array([degree+60,255,255])
        # ineffective variables
        lower2 = np.array([181,100,100])
        upper2 = np.array([181,255,255])

if __name__=='__main__':
    cap = cv2.VideoCapture(0)
    scaling_factor = 0.5

    # create trackbar for color change
    winname = 'Color Detector'
    cv2.namedWindow(winname)
    cv2.createTrackbar('Color', winname, 0, 360, color_range)

    # Iterate until the user presses ESC key
    while True:
        frame = get_frame(cap, scaling_factor)

        # Convert the HSV colorspace
        hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

        # Threshold the HSV image to get only desired color
        mask1 = cv2.inRange(hsv, lower1, upper1)
        mask2 = cv2.inRange(hsv, lower2, upper2)
        mask = cv2.bitwise_or(mask1, mask2)

        # Bitwise-AND mask and original image
        res = cv2.bitwise_and(frame, frame, mask=mask)

        cv2.imshow('Original image', frame)
        cv2.imshow(winname, res)

        # Check if the user pressed ESC key
        c = cv2.waitKey(1) & 0xFF
        if c == 27:
            break

    cv2.destroyAllWindows()
    cap.release()