python - 实时色彩检测及其功能

标签 python opencv colors real-time detection

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

https://prnt.sc/qo3cjy

想知道什么是使python检测颜色并使其运行由我编写的functions()的最有效和可行的方法。例如,如果检测到绿色,请运行函数hellogreen(),当检测到绿色时将显示绿色。

源代码(如果需要)以防万一:

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

关于python - 实时色彩检测及其功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59754116/

相关文章:

python - OpenCV HOG 描述符参数

python - 从 IDE 下载时的不同 PyCharm 版本

python - 当我点击一个按钮时,不同的按钮在 kivy recycleview 中闪烁

python - Django - 使用 XHTML2PDF 创建和存储 PDF 文件

c# - 在 OpenCVSharp 中水平翻转图像

java - java中发送者和接收者的不同聊天文本背景颜色

java - jtable 的行不接受选择

c++ - 从线程获取相机图像

python-3.x - 如何将 PyCairo 表面转换为 OpenCV numpy 并在 Python 3 中返回

java - 使用 OpenGL 进行纹理颜色操作(在使用 Java 的 LWJGL 中)