Monday, 5 February 2024

OpenCV: Bitwise operations on images

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

Merge individual color channel images to single channel image

Using ‘cv2.mege’ method, we can merge individual color channel images to single channel image.

 

Signature

dst = cv2.merge(images)

 

images: It is a list of individual numpy arrays (channels) that you want to merge into single image.

 

dst: Result of merged array is stored here.

 

Example

splits = cv.split(image)

blue_channel_image = splits[0]
green_channel_image = splits[1]
red_channel_image = splits[2]

merged_image = cv.merge([blue_channel_image, green_channel_image, red_channel_image])

 

Find the below working application.

 

merge_to_single_image.py

import cv2 as cv
import numpy as np

# Read the image as matrix of pixels
image = cv.imread('flowers.png')
#image = cv.resize(image, (800, 800))

# Default color space is BGR in OpenCV
splits = cv.split(image)

blue_channel_image = splits[0]
green_channel_image = splits[1]
red_channel_image = splits[2]

merged_image = cv.merge([blue_channel_image, green_channel_image, red_channel_image])

cv.imshow('image', image)
cv.imshow('merged_image', merged_image)

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

Output

 

Original image



 

Merged image

  


Previous                                                    Next                                                    Home

Split multi-channel image into individual color channel images in OpenCV

Using ‘cv2.split’ method, we can split the multi-channel image into individual color channel images.

 

Signature

dst = cv2.split(image[, mv])

 

image: Specifies the image that we want to split into individual color channels

 

mv: It is optional parameter. It's a list of indices representing the channels to be extracted.

 

dst: It is a list of NumPy arrays.

 

Example

splits = cv.split(image)

 

Find the below working application.

 

split_image_into_color_channel_images.py

import cv2 as cv
import numpy as np

# Read the image as matrix of pixels
image = cv.imread('flowers.png')
image = cv.resize(image, (800, 800))

# Default color space is BGR in OpenCV
splits = cv.split(image)

blue_channel_image = splits[0]
green_channel_image = splits[1]
red_channel_image = splits[1]

# Let's create a blank image use it in the merging process
blank_image = np.zeros(image.shape[:2], dtype='uint8')

blue = cv.merge([blue_channel_image, blank_image, blank_image])
green = cv.merge([blank_image, green_channel_image, blank_image])
red = cv.merge([blank_image, blank_image, red_channel_image])

cv.imshow('image', image)
cv.imshow('blue', blue)
cv.imshow('red', red)
cv.imshow('green', green)

# Wait for Infinite amount of time for a keyboard key to be pressed
cv.waitKey(0)

# Close the OpenCV windows
cv.destroyAllWindows()

Output

 

Original image


 

Blue color channel image (you can observe intensity of blue color)

 

 


Green color channel image (you can observe intensity of green color)

 

 


Red color channel image (you can observe intensity of red color)




  

Previous                                                    Next                                                    Home