Lab 03 Arithematic and Logical Operations
Lab 03 Arithematic and Logical Operations
LEARNING OUTCOME:
Understanding the importance and applications arithmetic and logical operations in digital image
processing.
INTRODUCTION:
Element-wise arithmetic operations between two images 𝑓(𝑥, 𝑦) and 𝑔(𝑥, 𝑦) are denoted as
While performing the arithmetic operations, care should be taken not to exceed the maximum
allowable gray level value (dictated by the type of the image data class). Also, the negative values
need to be clipped to zero or scaled properly in the allowable range. Image values can be rescaled
between desired gray scale values using the following relation.
𝐼 − min(𝐼)
𝐼𝑠𝑐𝑎𝑙𝑒𝑑 = 𝐾 × ( )
max(𝐼) − min(𝐼)
where value of 𝐾 = 255 for 256 gray scale values. Arithmetic operations are used in number of
image applications, i.e., image enhancement, brightness adjustment, negative image generation,
region of interest extraction, etc.
LEARNING TASKS:
Download chapter 2 images of the Gonzalez DIP 3rd edition book from the link given below.
https://www.imageprocessingplace.com/DIP-3E/dip3e_book_images_downloads.htm
Import the libraries using the following commands
import os # Importing operating system library
import cv2 # Importing OpenCV library
import matplotlib.pyplot as plt # Importing Matploblib library
import numpy as np # Importing NumPy library
Working directory can be checked and changed using the following commands
print(os.getcwd()) # Checking default current working directory
new_cwd = os.chdir('complete path') # Changing/updating current working directory
print(os.getcwd()) # Should output new changed directory
Image Subtraction:
1. Loss in an image resolution can also be identified through the image subtraction. In PYTHON,
read and display images Fig0220(a)(chronometer 3692x2812 2pt25 inch 1250 dpi).tif
a. Use the cv2.resize() command with the nearest neighbor interpolation function to
reduce the spatial resolution of the image by a factor of 3, 6 and 12. Then increase the
sizes of all three images using the nearest interpolation function to match the size of
the original image. Show the original image and resized images in a single 1 × 4 subplot
grid.
b. Calculate and view the difference between the original and the upscaled images from part-
a. Rescale the gray values in the difference images before visualizing the images. Show
the three difference images in a single 1 × 3 subplot grid. Where do you see the most
difference in an image?
c. Repeat part-a and part-b using the bicubic and bilinear interpolation schemes.
Image Division:
3. An important application of image division is shading correction. In PYTHON, read images
Fig0229(a)(tungsten_filament_shaded).tif and Fig0229(b)(tungsten_sensor_shading).tif.
a. Display both images side by side with titles ‘Shaded Image’ and ‘Shade Pattern’. An
imaging sensor has an associated shade pattern which is being multiplied with the image
during acquisition. As a result, the acquired image also has the similar shading pattern.
b. Perform shading correction by dividing the Shaded image with the shading pattern.
Rescale the gray values in the resulting image is needed.
c. Show the shaded, pattern and corrected images in a single 1 × 3 subplot grid. Can you
see the difference between the shaded image and the corrected image?
Image Multiplication:
4. Image multiplication is also used in masking to extract region of interest (ROI). Read
Fig0230(a)(dental_xray).tif') and Fig0230(b)(dental_xray_mask).tif in PYTHON.
a. Display both images side by side with titles ‘Original Image’ and ‘Mask’. Which region
from the original image will be extracted if it is multiplied with mask image?
b. Multiply the mask image with the original image and display the resulting ROI after
scaling gray values properly.
5. Image subtraction is used routinely for enhancing differences between images when they are
not visible with the naked eye. Read and view the Fig0227(a)(washington_infrared).tif image
in PYTHON.
a. We want to set the least significant bit of all image pixels to zero using bitwise AND
operator. What should be the values in the mask image with which you will perform the
bitwise AND operation to set LSB = 0 in your original image?
b. Perform bitwise AND operation by using cv2.bitwise_and() command. What will
be the result of this operation on your pixel intensities?
c. View the original and modified image by setting LSB to zero side by side. Can you see
any difference between the original and the modified image?
d. Display the original image, modified image and the difference image in a single 1 × 3
subplot grid.