You can perform Bitwise operations at pixel level. Following bitwise operators are used in common.
a. not
b. and
c. or
d. xor
not
It inverts a bit value. If the bit has value 0, then it inverts to 1. If the bit has value 1, then it inverts to 0.
Let me use a binary image to demonstrate the examples.
image = np.zeros((500, 500), dtype='uint8')
cv.rectangle(image, (50, 50), (400, 400), color=255, thickness=-1)
Above snippet create a blank image and draw a rectangle on it. It looks like below.
When we apply not operation on the above image, all the black pixels become white and white pixels become balck.
image_with_not_operator = cv.bitwise_not(image)
Above snippet create below image.
Find the below working application.
not_operator.py
import cv2 as cv
import numpy as np
image = np.zeros((500, 500), dtype='uint8')
cv.rectangle(image, (50, 50), (400, 400), color=255, thickness=-1)
image_with_not_operator = cv.bitwise_not(image)
cv.imshow('image', image)
cv.imshow('image_with_not_operator', image_with_not_operator)
cv.waitKey(0)
# Close the OpenCV windows
cv.destroyAllWindows()
Bitwise or operator
It is used to combine or overlay two images. Following table summarizes how bitwise operator works.
x |
y |
x or y |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
1 |
Suppose we have two images like below.
When you apply bitwise or operator on both the images, you will get below image.
or_operator.py
import cv2 as cv
import numpy as np
image = np.zeros((500, 500), dtype='uint8')
blank_image_1 = image.copy()
blank_image_2 = image.copy()
cv.rectangle(blank_image_1, (40, 40), (450, 450), color=255, thickness=-1)
cv.circle(blank_image_2, (250, 250), 240, color=255, thickness=-1)
image_with_or_operator = cv.bitwise_or(blank_image_1, blank_image_2)
cv.imshow('Rectangle', blank_image_1)
cv.imshow('Circle', blank_image_2)
cv.imshow('image_with_or_operator', image_with_or_operator)
cv.waitKey(0)
# Close the OpenCV windows
cv.destroyAllWindows()
Bitwise and operator
It is used for masking. You can create a binary mask with certain pixel values set to 1 (white) and others to 0 (black). By applying a bitwise AND operation with the masked image, you can extract specific regions of an image.
x |
y |
x and y |
0 |
0 |
0 |
0 |
1 |
0 |
1 |
0 |
0 |
1 |
1 |
1 |
Bitwise and operator generate below image.
and_operator.py
import cv2 as cv
import numpy as np
image = np.zeros((500, 500), dtype='uint8')
blank_image_1 = image.copy()
blank_image_2 = image.copy()
cv.rectangle(blank_image_1, (40, 40), (450, 450), color=255, thickness=-1)
cv.circle(blank_image_2, (250, 250), 240, color=255, thickness=-1)
image_with_and_operator = cv.bitwise_and(blank_image_1, blank_image_2)
cv.imshow('Rectangle', blank_image_1)
cv.imshow('Circle', blank_image_2)
cv.imshow('image_with_and_operator', image_with_and_operator)
cv.waitKey(0)
# Close the OpenCV windows
cv.destroyAllWindows()
Bitwise xor operator
When you XOR an image with a specific pattern, it can create visual effects or hide information within the image
x |
y |
x xor y |
0 |
0 |
0 |
0 |
1 |
1 |
1 |
0 |
1 |
1 |
1 |
0 |
‘xor’ operation generate below image.
xor_operator.py
import cv2 as cv
import numpy as np
image = np.zeros((500, 500), dtype='uint8')
blank_image_1 = image.copy()
blank_image_2 = image.copy()
cv.rectangle(blank_image_1, (40, 40), (450, 450), color=255, thickness=-1)
cv.circle(blank_image_2, (250, 250), 240, color=255, thickness=-1)
image_with_xor_operator = cv.bitwise_xor(blank_image_1, blank_image_2)
cv.imshow('Rectangle', blank_image_1)
cv.imshow('Circle', blank_image_2)
cv.imshow('image_with_xor_operator', image_with_xor_operator)
cv.waitKey(0)
# Close the OpenCV windows
cv.destroyAllWindows()
Previous Next Home
No comments:
Post a Comment