Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 实时颜色检测及其功能_Python_Opencv_Colors_Real Time_Detection - Fatal编程技术网

Python 实时颜色检测及其功能

Python 实时颜色检测及其功能,python,opencv,colors,real-time,detection,Python,Opencv,Colors,Real Time,Detection,我已经使用OpenCV在屏幕上弹出了一些颜色。最后,我成功地屏蔽了不同窗口中的颜色,如下图所示: 我想知道让python检测颜色并让它运行我编写的函数()的最有效和最有效的方法是什么。例如,如果检测到绿色,则运行函数hellogreen(),该函数将在检测到绿色时打印hello green,依此类推 源代码(如果需要)以防万一: import cv2 import numpy as np cap = cv2.VideoCapture(0) while True: _, frame

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

我想知道让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

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


您已经找到了颜色,有什么问题吗?我想匹配它们,因此它们运行不同的函数。例如,如果检测到红色,请运行函数red()[我将编写这些函数]@MH304例如,如果在窗口中检测到30%的颜色为红色,请运行函数,确定检测到的红卡百分比很高。因此,我可能会使用窗口选项卡的区域。我需要有关查找选项卡总面积的源代码的帮助,如果0.3*thatArea=='red',请运行red()
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