2016-02-16 196 views
2

我目前正在使用python和OpenCV開展一個項目。對於項目的一部分,我想查看一個特定像素(特別是座標爲100,100的像素)是否不等於黑色。我的代碼如下。使用Python檢查OpenCV中的像素顏色

import cv2 

img = cv2.imread('/Documents/2016.jpg') 

if img[100, 100] != [0, 0, 0]: 
    print("the pixel is not black") 

當我走在終端的樂趣,我得到這個錯誤。

File "/Documents/imCam.py", line 5, in <module> 
if img[100, 100] != [0, 0, 0]: 
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() 

我在做什麼錯?

回答

1

正如它所表明的那樣,您將列表與乘法條目進行比較,這太過於不道德。

你將不得不使用numpy.any

import cv2 
import numpy as np 

img = cv2.imread('/Documents/2016.jpg') 

if np.any(img[100, 100] != 0): 
    print("the pixel is not black") 
+0

謝謝!這工作! – user3127854