使用Python检查OpenCV中的像素颜色
我目前正在使用python和OpenCV进行一个项目。对于项目的一部分,我想检查一个特定像素(特别是坐标为100100的像素)是否不等于黑色。我的代码如下使用Python检查OpenCV中的像素颜色,python,image,opencv,pixels,Python,Image,Opencv,Pixels,我目前正在使用python和OpenCV进行一个项目。对于项目的一部分,我想检查一个特定像素(特别是坐标为100100的像素)是否不等于黑色。我的代码如下 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
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()
文件“/Documents/imCam.py”,第5行,在
如果img[100100]!=[0, 0, 0]:
ValueError:包含多个元素的数组的真值不明确。使用a.any()或a.all()
我做错了什么?正如它所说的,您将列表与多个条目进行比较,这太不精确了 你必须像这样使用
试试这个:)这个解决方案不是只适用于所有B、G和R值相同的颜色吗?你怎么能和别的颜色相比呢?如果img[100100]!=[22212127]:
img[100100]!=[222,12,127]
返回布尔值列表,np.all()
返回True
如果给定列表的每个元素都是True
。所以np.all(img[100100]=[222,12127])
将检查两种颜色是否匹配。
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")
import cv2
image = cv2.imread('abc.jpg')
if image[50, 50, 0] != 0:
print("the pixel is not black")