-2
 if image[i][j][1]>=lower_red and image[i][j][1]<=upper_red:

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

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
  • can you print what `image[i][j][1]` returns and post it? also, what are the values of `lower_red` and `upper_red`? –  Aug 29 '22 at 12:20
  • 3
    Use a.any() or a.all() perhaps? – Sayse Aug 29 '22 at 12:22
  • 1
    Since you are using OpenCV, you should use `cv2.inRange` instead. [See example here](https://stackoverflow.com/questions/48109650/how-to-detect-two-different-colors-using-cv2-inrange-in-python-opencv) – Jeru Luke Aug 29 '22 at 12:41
  • 1
    If you still need help then please print out `image.shape`, `lower_red.shape` and `upper_red.shape` and share the results with us! – Markus Aug 29 '22 at 12:44
  • the error is in the condition it self / i have tried .all() and .any() but it doesn't work – Mohamed Karam BOUDALI Aug 29 '22 at 13:43

2 Answers2

0

This is the right answer:

if ((image[i][j][1] >= lower_red).all()) and ((image[i][j][1] <= upper_red).any()):
    ...
Josef
  • 2,869
  • 2
  • 22
  • 23
-1

This does work when called in the correct way...

  • I make a suitable image array (list of lists) that has 3 dimensions.
  • I set values for upper an lower for testing.
  • I then add the logical check with an if-else.

This works:


# example image 3d array
image = [
    [[5,6,5],[1,2,3]],
    [[1,2,3],[5,6,5]]
    ]

# some values to check
lower_red = 10
upper_red = 10

# example of checking
i, j = 0,1
if image[i][j][1]>=lower_red and image[i][j][1]<=upper_red:
    print('done')
else:
    print(image[i][j][1])


result:

2
D.L
  • 4,339
  • 5
  • 22
  • 45