简体   繁体   English

如何在 RGB 颜色检测中使用“if 条件”?

[英]How to use an “if condition” on RGB color detection?

I am new to OpenCV and python.我是 OpenCV 和 python 的新手。 I am trying to detect colors in real-time using a camera.我正在尝试使用相机实时检测 colors。 I want to put an "if condition" when the detected color is Red, Green or Blue.当检测到的颜色是红色、绿色或蓝色时,我想放置一个“if 条件”。 If it detects red then it should print "The color is RED" The same is the case I want to apply with the Green and Blue color.如果它检测到红色,那么它应该打印“颜色是红色”我想应用绿色和蓝色的情况也是如此。 Here is the code that is showing RGB colors separately.这是分别显示 RGB colors 的代码。 Is there anyone who can help me out with this?有没有人可以帮助我解决这个问题? Thanks in advance:)提前致谢:)

import cv2
import numpy as np
import matplotlib.pyplot as plt

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([102, 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)

#   plt.imshow(mask,cmap='gray')

######## Chech if the shown object has red color or not ##########

    img_height, img_width, _=hsv_frame.shape
    for i in range(img_height):
        for j in range(img_width):
            if hsv_frame[i][j][1]>= low_red and hsv_frame[i][j][1]<=upper_red:
                print("Red found")

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

    if cv2.waitKey(1)==13:
        break

cap.release()
cv2.waitKey(1)
cv2.destroyAllWindows()

The error im getting is我得到的错误是

---> 40 if hsv_frame[i][j][1]>= low_red and hsv_frame[i][j][1]<=upper_red: ValueError: The truth value of an array with more than one element is ambiguous. ---> 40 if hsv_frame[i][j][1]>= low_red and hsv_frame[i][j][1]<=upper_red: ValueError: 具有多个元素的数组的真值不明确。 Use a.any() or a.all()使用 a.any() 或 a.all()

The line线

if hsv_frame[i][j][1]>= low_red and hsv_frame[i][j][1]<=upper_red:

compares two arrays, ie pixels (r,g,b) <= (r,g,b) which will return 3 values.比较两个 arrays,即像素 (r,g,b) <= (r,g,b) 将返回 3 个值。 ie (True/False, True/False, True/False).即(真/假,真/假,真/假)。

So you need to use either,所以你需要使用任何一个,

any() (atleast 1 is true) any() (至少 1 个为真)

or或者

all() (all must be true) all() (所有必须为真)

considering its pixel comparision i would recommend you to use all,考虑到它的像素比较,我建议你全部使用,

replace it with the below code.用下面的代码替换它。

if (hsv_frame[i][j][1]>= low_red).all() and (hsv_frame[i][j][1]<=upper_red).all():

Looks like you want to count if the image has red color or not, where red is defined by your low_red and high_red bounds.看起来您想计算图像是否具有红色,其中red由您的low_redhigh_red边界定义。

You already have the mask with the red region, why not use that?你已经有了带有红色区域的面具,为什么不使用它呢?

countRed = cv2.countNonZero(red_mask)
if countRed > 0: #choose whatever condition you want
    print("Red found")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM