简体   繁体   中英

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

    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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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