【问题标题】:Real Time Color Detection and Functioning with it实时颜色检测及其功能
【发布时间】:2020-01-15 15:03:26
【问题描述】:

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

https://prnt.sc/qo3cjy

想知道什么是让 python 检测颜色并使其运行我编写的函数()的最有效和最有效的方法。例如,如果检测到绿色,则运行函数 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

【问题讨论】:

  • 你已经找到颜色了,有什么问题?
  • 我想匹配它们,所以它们运行不同的功能
  • 比如检测到红色,运行函数red()【我会写这些函数】
  • @MH304 例如,如果在窗口中检测到 30% 为红色,则运行检测到高百分比的红牌的函数。所以我可能会使用窗口选项卡的区域。我需要查找选项卡总面积的源代码的帮助,如果 0.3 * thatArea == 'red',则运行 red()

标签: python opencv colors real-time detection


【解决方案1】:

将这些行放在代码的末尾(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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-19
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-08-31
    • 2011-11-22
    相关资源
    最近更新 更多