Bitwise Operations on Binary Image

Bitwise operations are used in image manipulation and used for extracting essential parts in the image. Bitwise operations used are :

  • AND
  • OR
  • XOR
  • NOT

Bitwise AND operation

Bit-wise conjunction of input array elements. 

Input Image 1:

Input Image 2: 

Python3

# Python program to illustrate
# arithmetic operation of
# bitwise AND of two images
      
# organizing imports
import cv2
import numpy as np
      
# path to input images are specified and
# images are loaded with imread command
img1 = cv2.imread('input1.png')
img2 = cv2.imread('input2.png')
  
# cv2.bitwise_and is applied over the
# image inputs with applied parameters
dest_and = cv2.bitwise_and(img2, img1, mask = None)
  
# the window showing output image
# with the Bitwise AND operation
# on the input images
cv2.imshow('Bitwise And', dest_and)
  
# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

                    

Output:

Bitwise OR operation

Bit-wise disjunction of input array elements. 

Python3

# Python program to illustrate
# arithmetic operation of
# bitwise OR of two images
      
# organizing imports
import cv2
import numpy as np
      
# path to input images are specified and
# images are loaded with imread command
img1 = cv2.imread('input1.png')
img2 = cv2.imread('input2.png')
  
# cv2.bitwise_or is applied over the
# image inputs with applied parameters
dest_or = cv2.bitwise_or(img2, img1, mask = None)
  
# the window showing output image
# with the Bitwise OR operation
# on the input images
cv2.imshow('Bitwise OR', dest_or)
  
# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

                    

Output:

Bitwise XOR operation

Bit-wise exclusive-OR operation on input array elements. 

Python3

# Python program to illustrate
# arithmetic operation of
# bitwise XOR of two images
      
# organizing imports
import cv2
import numpy as np
      
# path to input images are specified and
# images are loaded with imread command
img1 = cv2.imread('input1.png')
img2 = cv2.imread('input2.png')
  
# cv2.bitwise_xor is applied over the
# image inputs with applied parameters
dest_xor = cv2.bitwise_xor(img1, img2, mask = None)
  
# the window showing output image
# with the Bitwise XOR operation
# on the input images
cv2.imshow('Bitwise XOR', dest_xor)
  
# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

                    

Output:

Bitwise NOT operation

Inversion of input array elements. 

Python3

# Python program to illustrate
# arithmetic operation of
# bitwise NOT on input image
      
# organizing imports
import cv2
import numpy as np
      
# path to input images are specified and
# images are loaded with imread command
img1 = cv2.imread('input1.png')
img2 = cv2.imread('input2.png')
  
# cv2.bitwise_not is applied over the
# image input with applied parameters
dest_not1 = cv2.bitwise_not(img1, mask = None)
dest_not2 = cv2.bitwise_not(img2, mask = None)
  
# the windows showing output image
# with the Bitwise NOT operation
# on the 1st and 2nd input image
cv2.imshow('Bitwise NOT on image 1', dest_not1)
cv2.imshow('Bitwise NOT on image 2', dest_not2)
  
# De-allocate any associated memory usage
if cv2.waitKey(0) & 0xff == 27:
    cv2.destroyAllWindows()

                    

Output:

Bitwise NOT on Image 1 

Bitwise NOT on Image 2 

Getting Started with Python OpenCV

Computer Vision is one of the techniques from which we can understand images and videos and can extract information from them. It is a subset of artificial intelligence that collects information from digital images or videos. 

Python OpenCV is the most popular computer vision library. By using it, one can process images and videos to identify objects, faces, or even handwriting of a human. When it is integrated with various libraries, such as NumPy, python is capable of processing the OpenCV array structure for analysis.

In this article, we will discuss Python OpenCV in detail along with some common operations like resizing, cropping, reading, saving images, etc with the help of good examples.

Similar Reads

Installation

To install OpenCV, one must have Python and PIP, preinstalled on their system. If Python is not present, go through How to install Python on Linux? and follow the instructions provided. If PIP is not present, go through How to install PIP on Linux? and follow the instructions provided....

Reading Images

To read the images cv2.imread() method is used. This method loads an image from the specified file. If the image cannot be read (because of the missing file, improper permissions, unsupported or invalid format) then this method returns an empty matrix....

Example: Python OpenCV Read Image

Python3 # Python code to read image import cv2   # To read image from disk, we use # cv2.imread function, in below method, img = cv2.imread("geeks.png", cv2.IMREAD_COLOR)   print(img)...

Displaying Images

...

Saving Images

cv2.imshow() method is used to display an image in a window. The window automatically fits the image size....

Rotating Images

...

Resizing Image

cv2.imwrite() method is used to save an image to any storage device. This will save the image according to the specified format in the current working directory....

Color Spaces

...

Arithmetic Operations

cv2.rotate() method is used to rotate a 2D array in multiples of 90 degrees. The function cv::rotate rotates the array in three different ways....

Bitwise Operations on Binary Image

...

Image Translation

...

Edge Detection

Image resizing refers to the scaling of images. It helps in reducing the number of pixels from an image and that has several advantages e.g. It can reduce the time of training of a neural network as more is the number of pixels in an image more is the number of input nodes that in turn increases the complexity of the model. It also helps in zooming in images. Many times we need to resize the image i.e. either shrink it or scale up to meet the size requirements....

Simple Thresholding

...

Adaptive Thresholding

Color spaces are a way to represent the color channels present in the image that gives the image that particular hue. There are several different color spaces and each has its own significance. Some of the popular color spaces are RGB (Red, Green, Blue), CMYK (Cyan, Magenta, Yellow, Black), HSV (Hue, Saturation, Value), etc....

Otsu Thresholding

...

Image blurring

Arithmetic Operations like Addition, Subtraction, and Bitwise Operations(AND, OR, NOT, XOR) can be applied to the input images. These operations can be helpful in enhancing the properties of the input images. Image arithmetics are important for analyzing the input image properties. The operated images can be further used as an enhanced input image, and many more operations can be applied for clarifying, thresholding, dilating, etc of the image....

Bilateral Filtering

...

Image Contours

...

Erosion and Dilation

Bitwise operations are used in image manipulation and used for extracting essential parts in the image. Bitwise operations used are :...

Feature Matching

...

Drawing on Images

...

Example: Python OpenCV Draw on Image

...

Face Recognition

...