如果cv2.inrange()无法找到匹配的颜色,则循环返回以开始

时间:2019-02-05 14:08:30

标签: colors mask cv2

我正在使用此代码(https://www.pyimagesearch.com/2014/08/04/opencv-python-color-detection/)的略微修改版本以及tesseract OCR,以从无人机上的摄像机识别地面标记上的字符。该标记将是明显的红色,我正在寻找一种方法来检查图像中是否存在红色,如果没有,请再次检查下一张图像(视频帧)。

目前,我只是在寻找一种方法来使用cv2.inrange()中出现的错误(如果不存在红色的话)将其发送回代码开头,以便可以手动选择新图像以进行检查。我意识到这可能很基础,但是我是python的新手!

import numpy as np
import argparse
import cv2
from PIL import Image

#       <<DETECT RED AREA OF IMAGE AND MASK OTHER COLOURS>>

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image")
args = vars(ap.parse_args())

# load the image
image = cv2.imread(args["image"])

# define the list of boundaries
boundaries = [([0, 0, 100], [0, 0, 255]),
]

# loop over the boundaries
for (lower, upper) in boundaries:
    # create NumPy arrays from the boundaries
    lower = np.array(lower, dtype = "uint8")
    upper = np.array(upper, dtype = "uint8")
    print(upper)
    # find the colors within the specified boundaries and apply
    # the mask
    mask = cv2.inRange(image, lower, upper)
    output = cv2.bitwise_and(image, image, mask = mask)

0 个答案:

没有答案