简体   繁体   English

使用 Python 检查 OpenCV 中的像素颜色

[英]Checking pixel color in OpenCV with Python

I am currently working on a project with python and OpenCV.我目前正在使用 python 和 OpenCV 开发一个项目。 For one part of the project, I would like to check and see if one specific pixel (specifically the pixel with coordinate 100, 100) is not equal to the color black.对于项目的一部分,我想检查一个特定的像素(特别是坐标为 100、100 的像素)是否不等于黑色。 My code is as follows.我的代码如下。

import cv2

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

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

When I go and fun in the terminal I get this error.当我去终端玩乐时,我收到此错误。

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()

What am I doing wrong?我究竟做错了什么?

As it states, you're comparing lists with multiply entries, which is too unprecise.正如它所说,您正在将列表与多个条目进行比较,这太不精确了。

You'll have to use numpy.any like你必须使用numpy.any like

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")

Try this :)尝试这个 :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM