Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Checking pixel color in OpenCV with Python

I am currently working on a project with python and 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. 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?

like image 765
user3127854 Avatar asked Jul 14 '26 05:07

user3127854


1 Answers

As it states, you're comparing lists with multiply entries, which is too unprecise.

You'll have to use 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")
like image 189
s1hofmann Avatar answered Jul 18 '26 20:07

s1hofmann



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!