Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

why cv2.bitwise_and function of opencv-python returns four element array on single scalar value

Tags:

python

opencv

I'm trying to understand cv2.bitwise_and function of opencv-python. So I tried it as:

import cv2
cv2.bitwise_and(1,1)

above code returns

array([[1.],
       [0.],
       [0.],
       [0.]])

I don't understand why it returns this.

Documentation says :

dst(I) = src1(I) ^ src2(I) if mask(I) != 0

according to this output should be single value 1. where am I going wrong?

like image 993
Krishna Avatar asked Sep 19 '25 17:09

Krishna


1 Answers

The documentation says clearly that the function performs the operations dst(I) = src1(I) ^ src2(I) if mask(I) != 0 if the inputs are two arrays of the same size.

So try:

import numpy as np  # Opecv works with numpy arrays
import cv2

a = np.uint8([1])
b = np.uint8([1])
cv2.bitwise_and(a, b)

That code returns:

array([[1]], dtype=uint8)

That is a one dimensional array containing the number 1.

The documentation also mentions that the operation can be done with an array and a scalar, but not with two scalars, so the input cv2.bitwise_and(1,1) is not correct.

like image 110
sinecode Avatar answered Sep 22 '25 07:09

sinecode