Updated April 19, 2023
Introduction to OpenCV bitwise_and
Whenever we are dealing with images while solving computer vision problems, there arises a necessity to wither manipulate the given image or extract parts of the given image based on the requirement, in such cases we make use of bitwise operators in OpenCV and when the elements of the arrays corresponding to the given two images must be combined bit wise, then we make use of an operator in OpenCV called but wise and operator using which the arrays corresponding to the two images can be combined resulting in merging of the two images and bit wise operation on the two images returns an image with the merging done as per the specification.
The syntax to define bitwise_and() operator in OpenCV is as follows:
bitwise_and(source1_array, source2_array, destination_array, mask)
- where source1_array is the array corresponding to the first input image on which bitwise and operation is to be performed,
- source2_array is the array corresponding to the second input image on which bitwise and operation is to be performed,
- destination_array is the resulting array by performing bitwise operation on the array corresponding to the first input image and the array corresponding to the second input image and
- mask is the mask operation to be performed on the resulting image and it is optional.
Working of bitwise_and() Operator in OpenCV
Working of bitwise_and() operator in OpenCV is as follows:
- In order to be able to perform bit wise conjunction of the two arrays corresponding to the two images in OpenCV, we make use of bitwise_and operator.
- To be able to make use of bitwise_and operator in our program, we must import the module cv2.
- The images whose arrays are to be combined using bitwise_and operator are read using imread() function.
- Then the corresponding arrays of those images are passed to the bitwise_and operator.
- The bitwise_and operator returns an array that corresponds to the resulting image from the merger of the given two images.
- The operation of bitwise_and can be done on images having same dimensions only.
Examples of OpenCV bitwise_and
Following are the examples are given below:
Example #1
OpenCV program in python to demonstrate bitwise_and operator to read two images using imread() function and then merge the given two images using bitwise_and operator and then display the resulting image as the output on the screen:
Code:
#importing the modules cv2 and numpy
import cv2
import numpy as np
#reading the two images that are to be merged using imread() function
imageread1 = cv2.imread('C:/Users/admin/Desktop/plane.jpg')
imageread2 = cv2.imread('C:/Users/admin/Desktop/car.jpg')
#using bitwise_and operation on the given two images
resultimage = cv2.bitwise_and(imageread1, imageread2, mask = None)
#displaying the merged image as the output on the screen
cv2.imshow('Merged_image', resultimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the two images that are to be merged using imread() function. Then we making use of bitwise_and operator by specifying the two input images as the parameters which returns the merged image as the resulting image displayed as the output on the screen. The output is shown in the snapshot above.
Example #2
OpenCV program in python to demonstrate bitwise_and operator to read two images using imread() function and then merge the given two images using bitwise_and operator and then display the resulting image as the output on the screen:
Code:
#importing the modules cv2 and numpy
import cv2
import numpy as np
#reading the two images that are to be merged using imread() function
imageread1 = cv2.imread('C:/Users/admin/Desktop/logo.png')
imageread2 = cv2.imread('C:/Users/admin/Desktop/educbalogo.jpg')
#using bitwise_and operation on the given two images
resultimage = cv2.bitwise_and(imageread1, imageread2, mask = None)
#displaying the merged image as the output on the screen
cv2.imshow('Merged_image', resultimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the two images that are to be merged using imread() function. Then we making use of bitwise_and operator by specifying the two input images as the parameters which returns the merged image as the resulting image displayed as the output on the screen. The output is shown in the snapshot above.
Example #3
OpenCV program in python to demonstrate bitwise_and operator to read two images using imread() function and then merge the given two images using bitwise_and operator and then display the resulting image as the output on the screen:
Code:
#importing the modules cv2 and numpy
import cv2
import numpy as np
#reading the two images that are to be merged using imread() function
imageread1 = cv2.imread('C:/Users/admin/Desktop/tree.jpg')
imageread2 = cv2.imread('C:/Users/admin/Desktop/educbatree.jpg')
#using bitwise_and operation on the given two images
resultimage = cv2.bitwise_and(imageread1, imageread2, mask = None)
#displaying the merged image as the output on the screen
cv2.imshow('Merged_image', resultimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the two images that are to be merged using imread() function. Then we making use of bitwise_and operator by specifying the two input images as the parameters which returns the merged image as the resulting image displayed as the output on the screen. The output is shown in the snapshot above.
Example #4
OpenCV program in python to demonstrate bitwise_and operator to read two images using imread() function and then merge the given two images using bitwise_and operator and then display the resulting image as the output on the screen:
Code:
#importing the modules cv2 and numpy
import cv2
import numpy as np
#reading the two images that are to be merged using imread() function
imageread1 = cv2.imread('C:/Users/admin/Desktop/plane1.jpg')
imageread2 = cv2.imread('C:/Users/admin/Desktop/educbatree.jpg')
#using bitwise_and operation on the given two images
resultimage = cv2.bitwise_and(imageread1, imageread2, mask = None)
#displaying the merged image as the output on the screen
cv2.imshow('Merged_image', resultimage)
cv2.waitKey(0)
cv2.destroyAllWindows()
Output:
In the above program, we are importing the module cv2 and numpy. Then we are reading the two images that are to be merged using imread() function. Then we making use of the bitwise_and operator by specifying the two input images as the parameters which returns the merged image as the resulting image displayed as the output on the screen. The output is shown in the snapshot above.
Recommended Articles
We hope that this EDUCBA information on “OpenCV bitwise_and” was beneficial to you. You can view EDUCBA’s recommended articles for more information.