Change language

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

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically