2016-07-04 109 views
0

我使用这个脚本用于颜色检测:OpenCV的蟒蛇颜色检测与布尔输出

#导入必要的包

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) 

我需要颜色的布尔变量检测与否。 我该怎么做?

问候 托马斯

回答

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: 

#从边界

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

#发现内的色彩营造出与NumPy阵列指定的边界并应用掩码

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

问候 托马斯