×

Bitwise Operators and Masking in OpenCV

Bitwise Operators

In computer science terms, everything we represent is converted into binary language and also stored in binary format. When it comes to displaying an image if there is no color in the pixel, the value is assigned ‘0’, and when there is some color stored in the pixel then the value is assigned ‘1’.

When we perform bitwise operations on images in OpenCV, they are actually performed on 0’s and 1’s of the image. Operations such as

  • bitwise_and
  • bitwise_or
  • bitwise_nor
  • bitwise_not

These operations can be performed on the images to obtain great results. For this purpose, we define a blank image to draw different shapes and perform bitwise operations on them.

import numpy as np
black = np.zeros((480, 480), dtype = "uint8")
cv2.imshow("black", black)
Image 45

Bitwise AND

Bitwise AND performs operations between two images by checking the pixel intensities present in the image, if both of them are of the same intensity it applies the same intensity to the pixel on the resulting image. thus we can say it looks for an intersection point between the images.

To display the operations performed we are considering only two colors black and white and shapes such as rectangle and circle.

When we apply cv2.bitwise_and() it gives the intersection point between the rectangle and circle images.

import cv2
import numpy as np
black = np.zeros((480, 480), dtype = "uint8")
rect = cv2.rectangle(black.copy(), (50, 60), (400, 200), 255, -1)
circle = cv2.circle(black.copy(), (240, 240), 150, 255, -1)
bit_and = cv2.bitwise_and(rect, circle)
cv2.imshow("rect", rect)
cv2.imshow("circle", circle)
cv2.imshow("Bit_AND", bit_and)
Image 46
Image 48

Bitwise OR

Bitwise OR performs operations between images by checking the pixel intensities, if they are of the same intensity or both of them are different intensities it performs “OR” operation to the pixels on the resulting image. thus we can say it looks for a union point between the images( intersecting and non-intersecting regions ).

When we apply cv2.bitwise_or() it displays as if one image overlaps the other image by performing a union operation between the rectangle and circle images.

import cv2
import numpy as np
black = np.zeros((480, 480), dtype = "uint8")
rect = cv2.rectangle(black.copy(), (50, 60), (400, 200), 255, -1)
circle = cv2.circle(black.copy(), (240, 240), 150, 255, -1)

bit_or = cv2.bitwise_or(rect, circle)
cv2.imshow("Bit_OR", bit_or)
Image 50

Bitwise XOR

Bitwise XOR looks at an area where there is either one of the pixel’s values is greater than zero and performs cv2.bitwise_xor(). It displays a non-intersecting area between two images leaving the intersecting area.

bit_Xor = cv2.bitwise_xor(rect, circle)
cv2.imshow("Bit_Xor", bit_Xor)

Output

Image 51

Bitwise NOT

Bitwise Not does not perform any union or intersection operations it rather inverts the binary number of the pixels. If the value of a pixel is “0” it inverts it into “1” and if the value of the pixel is “1” it inverts it into “0”.

cv2.bitwise_not() take one parameter known as the source image

bit_not1 = cv2.bitwise_not(rect)
bit_not2 = cv2.bitwise_not(circle)
cv2.imshow("Bit_Not_Rect", bit_not1)
cv2.imshow("Bit_Not_Circle", bit_not2)

Output: As we can notice the color inversion is done by cv2.bitwise_not, it inverted black pixels into white and white pixels into black color.

Image 52

Masking on Images using OpenCV

Masking is a method of extracting required parts of an image by performing bitwise operations on an image. Using making we can extract parts of an image in different shapes.

To performing masking we create a blank image with similar dimensions to the source image and perform Bitwise operations on it.

We can create a blank image using numpy.zeros() method and pass dimensions similar to the source image ( using src.shape[::2] ) data type as image ( “uint8” ).

Then Create a mask by drawing a shape such as a rectangle on the blank image using cv2.rectangle(), such that pixels inside the rectangle will have binary “1” value.

import cv2
import numpy as np

#create a blank image using np.zeros()
blank = np.zeros(image.shape[:2], dtype = "uint8")

#draw rectangle on the blank image
mask = cv2.rectangle(blank, (375, 230), (420, 340), (255, 255, 255), -1)
cv2.imshow("mask", mask)

Output:

Image 53

Now performing bitwise_and operation on the source image and passing the mask we created to cv2.bitwise_and displays the intersection point of pixel values ( i.e., ‘1’).

import cv2
import numpy as np

image = cv2.imread('C:\images\publicplace.jpg')
blank = np.zeros(image.shape[:2], dtype = "uint8")
mask = cv2.rectangle(blank, (375, 230), (420, 340), (255, 255, 255), -1)

#Passing the mask to the bitwise_and gives intersection point of the mask and the image
maskimage= cv2.bitwise_and(image, image, mask=mask)
cv2.imshow("img", image)
cv2.imshow("Masked", maskimage)

Output:- We have displayed a person separately using masking, thus removing unnecessary objects from the image.

Image 54