Ask Your Question

Revision history [back]

What does cv2.bitwise_and do? What are its parameters?

I'm just starting out with OpenCV and following this tutorial: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html

Here's the code:

 # Load two images

img1 = cv2.imread('messi5.jpg') img2 = cv2.imread('opencv_logo.png')

rows,cols,channels = img2.shape roi = img1[0:rows, 0:cols ]

# Now create a mask of logo and create its inverse mask also

img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask)

# Now black-out the area of logo in ROI

img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

# Take only region of logo from logo image.

img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

# Put logo in ROI and modify the main image

dst = cv2.add(img1_bg,img2_fg) img1[0:rows, 0:cols ] = dst

cv2.imshow('res',img1) cv2.waitKey(0) cv2.destroyAllWindows()

I understand everything here except the 2 lines of code containing "bitwise_and" .

click to hide/show revision 2
None

updated 2018-02-06 05:55:07 -0600

berak gravatar image

What does cv2.bitwise_and do? What are its parameters?

I'm just starting out with OpenCV and following this tutorial: http://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_core/py_image_arithmetics/py_image_arithmetics.html

Here's the code:

 # Load two images

img1 = cv2.imread('messi5.jpg') img2 = cv2.imread('opencv_logo.png')

cv2.imread('opencv_logo.png')

rows,cols,channels = img2.shape roi = img1[0:rows, 0:cols ]

]
# Now create a mask of logo and create its inverse mask also

img2gray = cv2.cvtColor(img2,cv2.COLOR_BGR2GRAY) ret, mask = cv2.threshold(img2gray, 10, 255, cv2.THRESH_BINARY) mask_inv = cv2.bitwise_not(mask)

cv2.bitwise_not(mask)
# Now black-out the area of logo in ROI

img1_bg = cv2.bitwise_and(roi,roi,mask = mask_inv)

mask_inv)
# Take only region of logo from logo image.

img2_fg = cv2.bitwise_and(img2,img2,mask = mask)

mask)
# Put logo in ROI and modify the main image

dst = cv2.add(img1_bg,img2_fg) img1[0:rows, 0:cols ] = dst

dst

cv2.imshow('res',img1) cv2.waitKey(0) cv2.destroyAllWindows()

cv2.destroyAllWindows()

I understand everything here except the 2 lines of code containing "bitwise_and" .