2016-07-04 72 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/") 

問候 托馬斯