Image Arithmetic Using OpenCV | Set-2 (bitwise operations on binary images)

Bitwise operations are used in image manipulation and are used to extract important parts of an image. This article uses bitwise operations:

  1. AND
  2. OR
  3. XOR
  4. NO

Bitwise operations also help in masking images. Imaging can be enabled using these operations. These operations can be useful for improving the properties of the input images.

Note: Bitwise operations must be applied on input images of the same size

Input image 1 :

Input Image 2:

Bitwise operation AND on the image:

Bitwise connection of elements of the input array.

Syntax: cv2.bitwise_and (source1, source2, destination, mask)

Parameters:
source1: First Input Image array (Single- channel, 8-bit or floating-point)
source2: Second Input Image array (Single-channe l, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

# Python illustration program
# arithmetic operation
# bitwise AND of two images

  
# organization of import

import cv2 

import numpy as np 

 
# specifies the path to the input images and
# images are loaded using the imread command

image1 = cv2.imread ( `input1.jpg`

image2 = cv2.imread ( `input2.jpg` )

 
# cv2.bitwise_and is applied over
# input images with applied parameters

dest_and = cv2.bitwise_and (img2, img1, mask = None )

 
# window with output image
# with bitwise AND operation
# on input images

cv2.imshow ( `Bitwise And` , dest_and)

 
# Unselect any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27

cv2.destroyAllWindows () 

Output:

Bitwise operation OR in the image:

Bitwise disjunction of input array elements.

Syntax: cv2.bitwise_or (source1, source2, destination, mask)

Parameters:
source1: First Input Image array (Single-channel, 8-bit or floating-point)
source2: Second Input Image array (Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Inpu t image array)
mask: Operation mask, Input / output 8-bit single-channel mask

# Python program for illustration
# arithmetic operation
# bitwise OR of two images

 
# organization of import

import cv2 

import numpy as np 

 
# specifies the path to input images and
# images are loaded using imread command

image1 = cv2.imread ( `input1.jpg`

image2 = cv2.imread ( `input2.jpg` )

 
# cv2.bitwise_or applied over
# image input with parameters applied

dest_or = cv2.bitwise_or (img2, img1, mask = None )

  
# window with output image
# with bitwise OR operation
# on input images

cv2.imshow ( `Bitwise OR`  , dest_or)

 
# Deallocate any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27

cv2.destroyAllWindows () 

Output:

Bitwise operation XOR on the image:

Bitwise exclusive OR on the elements of the input array.

Syntax: cv2.bitwise_xor (source1, source2, destination, mask )

Parameters:
source1: First Input Image array (Single-channel, 8-bit or floating-point)
source2: Second Input Image array (Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions an d type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

# Python illustration program
# arithmetic operation
# bitwise XOR of two images

 
# organizing imports

import cv2 

import numpy as np 

 
# specifies the path to the input images and
# images are loaded using imread

image1 = cv2.i mread ( `input1.jpg`

image2 = cv2.imread ( `input2.jpg` )

 
# cv2. bitwise_xor is applied over
# image input with parameters applied

dest_xor = cv2.bitwise_xor (img1, img2, mask = None )

 
# window with output image
# with bitwise XOR operation
# on input images

cv2.imshow ( `Bit wise XOR` , dest_xor)

 
# Unselect any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27

cv2.destroyAllWindows () 

Output:

Bitwise operation NOT in the image:

Invert the elements of the input array.

Syntax: cv2.bitwise_not (source, destination, mask)

Parameters:
source: Input Image array (Single-channel, 8-bit or floating-point)
dest: Output array (Similar to the dimensions and type of Input image array)
mask: Operation mask, Input / output 8-bit single-channel mask

< code>

# Python program for illustration
# arithmetic operation
# bitwise NOT on input image

 
# organization of import

import cv2 

import numpy as np 

 
# specifies the path to the input images and
# images are loaded using the imread command

image1 = cv2.imread ( `input1.jpg`

image2 = cv2.imread ( ` input2.jpg` )

 
# cv2.bitwise_not is applied over
# input image with applied parameters

dest_not1 = cv2.bitwise_not (img1, mask = None )

dest_not2 = cv2.bitwise_not (img2, mask = None )

 
# windows showing the output image
# with a bitwise NOT operator
# on the first and second input image

cv2.imshow ( `Bitwise NOT on image 1` , dest_not1)

cv2.imshow ( `Bitwise NOT on image 2` , dest_not2)

 
# Deallocate any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27

cv2.destroyAllWindows () 

Output:
Bitwise NOT in Image 1

Bitwise NOT in Image 2