简体   繁体   English

opencv python颜色检测与布尔输出

[英]opencv python color detection with boolean output

i use this Script for color detection: 我使用此脚本进行颜色检测:

# import the necessary packages #导入必要的软件包

    import numpy as np
    import argparse
    import cv2

# 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 = [
        ([100,50,220],[135,80,245]),
]

# 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")

        # 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)
        print (output)

        # show the images
        cv2.imshow("images", np.hstack([image, output]))
        cv2.waitKey(0)

I need a boolean Variable for color detect or not. 我需要一个布尔变量来进行颜色检测。 How can I do that? 我怎样才能做到这一点?

regards Thomas 问候托马斯

here my own solution: 这是我自己的解决方案:

# import the necessary packages #导入必要的软件包

import shutil
import numpy as np
import argparse
import cv2

# 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 = [
        ([100,50,220],[135,80,245]),
]

# loop over the boundaries #越过边界

for (lower, upper) in boundaries:

# create NumPy arrays from the boundaries #从边界创建NumPy数组

    lower = np.array(lower, dtype = "uint8")
    upper = np.array(upper, dtype = "uint8")

# find the colors within the specified boundaries and apply the mask #找到指定边界内的颜色并应用遮罩

        mask = cv2.inRange(image, lower, upper)
        if np.sum(mask) < 100:
         shutil.move(args["image"], "/temp/")

regards Thomas 问候托马斯

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

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