实时颜色检测及其功能

Real Time Color Detection and Functioning with it

我使用 OpenCV 工作了一段时间,以在屏幕上弹出颜色。最后,我成功遮盖了不同 windows 的颜色,如下图所示:

https://prnt.sc/qo3cjy

想知道什么是使 python 检测颜色并使其成为 运行 我编写的 functions() 的最有效和有效的方法。例如,如果检测到绿色,运行 函数 hellogreen(),它会在检测到绿色时打印 hello green 等等。

源代码,以防万一:

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    _, frame = cap.read()
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

      # Red color
    low_red = np.array([161, 155, 84])
    high_red = np.array([179, 255, 255])
    red_mask = cv2.inRange(hsv_frame, low_red, high_red)
    red = cv2.bitwise_and(frame, frame, mask=red_mask)

      # Blue color
    low_blue = np.array([94, 80, 2])
    high_blue = np.array([126, 255, 255])
    blue_mask = cv2.inRange(hsv_frame, low_blue, high_blue)
    blue = cv2.bitwise_and(frame, frame, mask=blue_mask)

    # Green color
    low_green = np.array([25, 52, 72])
    high_green = np.array([83, 255, 255])
    green_mask = cv2.inRange(hsv_frame, low_green, high_green)
    green = cv2.bitwise_and(frame, frame, mask=green_mask)

    # Every color except white
    low = np.array([0, 42, 0])
    high = np.array([179, 255, 255])
    mask = cv2.inRange(hsv_frame, low, high)
    result = cv2.bitwise_and(frame, frame, mask=mask)

    cv2.imshow("Frame", frame)
    cv2.imshow("Red", red)
    cv2.imshow("Blue", blue)
    cv2.imshow("Green", green)

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

将这些行放在代码的末尾(helloblue、hellogreen 和 hellored 是您假设的函数):

b = cv2.countNonZero(blue_mask) 
r = cv2.countNonZero(red_mask) 
g = cv2.countNonZero(green_mask) 

if b >= r and b >= g:
    helloblue()
elif r >= b and r >= g:
    hellred()
elif g >= b and g >= r:
    hellgreen()

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