【问题标题】:opencv python color detection with boolean output带有布尔输出的opencv python颜色检测
【发布时间】:2016-07-04 18:30:21
【问题描述】:

我使用此脚本进行颜色检测:

#导入必要的包

    import numpy as np
    import argparse
    import cv2

#构造参数解析并解析参数

ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help = "path to the image")
args = vars(ap.parse_args())

#加载图片

image = cv2.imread(args["image"])

#定义边界列表

boundaries = [
        ([100,50,220],[135,80,245]),
]

# 循环遍历边界

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)

我需要一个用于颜色检测的布尔变量。 我该怎么做?

问候 托马斯

【问题讨论】:

    标签: python opencv color-detection


    【解决方案1】:

    这是我自己的解决方案:

    #导入必要的包

    import shutil
    import numpy as np
    import argparse
    import cv2
    

    #构造参数解析并解析参数

    ap = argparse.ArgumentParser()
    ap.add_argument("-i", "--image", help = "path to the image")
    args = vars(ap.parse_args())
    

    #加载图片

    image = cv2.imread(args["image"])
    

    #定义边界列表

    boundaries = [
            ([100,50,220],[135,80,245]),
    ]
    

    # 循环遍历边界

    for (lower, upper) in boundaries:
    

    # 从边界创建 NumPy 数组

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

    #找到指定边界内的颜色并应用遮罩

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

    问候 托马斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-03-19
      • 2014-03-15
      • 2023-03-13
      • 1970-01-01
      • 2021-08-20
      • 2021-12-25
      • 1970-01-01
      相关资源
      最近更新 更多