Bitwise Operations
Learn about bitwise operators and their applications.
We'll cover the following
Instead of just stacking images, we might also want to combine images. We’ll use bitwise operators to combine images in OpenCV. In this lesson, we’ll work with black in white images to better understand these operators.
What are bitwise operators?
There are four basic bitwise operators—AND
, OR
, XOR
, and NOT
. These operators are frequently used to edit images, especially while working with masking images.
First, we’ll create a blank image.
To create a blank image, we use the np.zeros()
function of the OpenCV library.
blank = np.zeros((600,600), dtype='uint8')
Note: We must import the NumPy library to use this function.
This function requires two parameters. The first parameter is the size of the blank image and the second parameter, dtype='uint8'
, is an unsigned integer of 8 bits.
In the code below, we draw a rectangle and a circle on the blank image, and use these two shapes to understand all the bitwise operators:
rectangle = cv2.rectangle(blank.copy(), (30,30), (570,570), 255, -1)
(30,30)
is the top left corner point of the image where we want to draw the rectangle.(570,570)
is the bottom right corner point of the image where we want to draw the rectangle.255
adds the white color to the rectangle.-1
is the thickness of the rectangle. It will fill the rectangle with the respective color.
circle = cv2.circle(blank.copy(), (200,200), 300, 255, -1)
(200,200)
is the center point of the circle around which the circle will be drawn.300
is the radius of the circle.255
adds white color to the circle.-1
is the thickness of the circle. It will fill the rectangle with the respective color.
Get hands-on with 1200+ tech skills courses.