以真或假表示颜色检测

时间:2019-09-21 05:43:53

标签: python image-processing computer-vision color-detection

我正在尝试将颜色检测设置为True或False值。我想制作一个程序,通过将“检测状态”恢复为“真”和“假”值来告诉我“交通信号灯红灯状态”为“开”或“关”。

我正在使用OpenCV,因为我想用python编写一个简单而准确的脚本。

def detect_status(image, Threshold=0.01):

    height = image.shape[0]
    width = image.shape[1]
    desired_dim = (width, height) # width, height
    image = cv2.resize(np.array(image), desired_dim, interpolation=cv2.INTER_LINEAR)
    image_hsv=cv2.cvtColor(image, cv2.COLOR_RGB2HSV)

    # Masks for red
    # mask 0
    lower_red = np.array([99,126,0])
    upper_red = np.array([180,255,255])
    mask0 = cv2.inRange(image_hsv, lower_red, upper_red)

    # mask 1
    lower_red = np.array([0,50,50])
    upper_red = np.array([10,255,255])
    mask2 = cv2.inRange(image_hsv, lower_red, upper_red)

    # mask 2
    lower_red = np.array([170,50,50])
    upper_red = np.array([180,255,255])
    mask1 = cv2.inRange(image_hsv, lower_red, upper_red)



    # red pixels' mask
    mask = mask0+mask1+mask2

    Compare the percentage of red values
    rate = np.count_nonzero(mask) / (desired_dim[0] * desired_dim[1])

    if rate > Threshold:
        return True
    else:
        return False

期待真假中的色彩检测。

0 个答案:

没有答案