2016-09-27 235 views
0

我有一個代碼來檢測兩種顏色綠色和藍色。我要檢查,如果檢測 綠色打印的按摩,如果檢測藍色彩色打印,另一條消息也檢查顏色Opencv Python

這裏是代碼:

import cv2 

import numpy as np 

cap = cv2.VideoCapture(0) 

while(1): 

    # Take each frame 
    _, frame = cap.read() 

    # Convert BGR to HSV 
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) 

    # define range of blue color in HSV 
    lower_blue = np.array([110,50,50]) 
    upper_blue = np.array([130,255,255]) 

    lower_green = np.array([50, 50, 120]) 
    upper_green = np.array([70, 255, 255]) 
    green_mask = cv2.inRange(hsv, lower_green, upper_green) # I have the Green threshold image. 

    # Threshold the HSV image to get only blue colors 
    blue_mask = cv2.inRange(hsv, lower_blue, upper_blue) 
    mask = blue_mask + green_mask 
############this is the Error #################### 
    if mask==green_mask: 
     print "DOne" 
################################################ 

    # Bitwise-AND mask and original image 
    res = cv2.bitwise_and(frame,frame, mask= mask) 

    cv2.imshow('frame',frame) 
    cv2.imshow('mask',mask) 
    cv2.imshow('res',res) 
    k = cv2.waitKey(5) & 0xFF 
    if k == 27: 
     break 

cv2.destroyAllWindows() 

運行上面的代碼給我下面的錯誤:

if mask==green_mask:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

任何想法如何解決這個問題?

回答

0

比較if mask==green_mask返回它們維度的布爾值數組,它不能滿足需要單個布爾值的if條件。

您需要使用它會返回true如果矩陣maskgreen_mask都是相同的,並返回false否則的功能。該函數應在此if條件下調用。

編輯:

if np.array_equal(mask, green_mask):

這將解決您的問題,請更換代碼。

+0

你的意思是我需要寫這個如果在一個函數中的條件,並調用它,即時通訊抱歉,我需要eny提示這個函數@MD。 Nazmul Kibria –

+0

看到我的編輯....希望能解決您的問題 –

+0

非常感謝它很有效 如果您介意給我發電子郵件更多Qustions plez @MD。 Nazmul Kibria –