gpt4 book ai didi

python - 带有 bool 输出的opencv python颜色检测

转载 作者:行者123 更新时间:2023-12-02 17:44:33 26 4
gpt4 key购买 nike

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

# 导入必要的包

    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)

我需要一个用于颜色检测的 bool 变量。
我怎样才能做到这一点?

问候
托马斯

最佳答案

这是我自己的解决方案:

# 导入必要的包

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
        mask = cv2.inRange(image, lower, upper)
if np.sum(mask) < 100:
shutil.move(args["image"], "/temp/")

问候
托马斯

关于python - 带有 bool 输出的opencv python颜色检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38190498/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com