Digital Image Processing: Lab 4: Image Enhancement in The Spatial Domain

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Digital Image Processing

Lab 4: Image Enhancement in the Spatial Domain

Waqar Tanveer
Department of Computer Engineering
Bahria University, Islamabad

October 12, 2020

Digital Image Processing 1 / 24


Objective

For image enhancement, we will cover the following topics:

Mathematical and Logical Operations on Images

Masking Operations

Spatial Filtering

Digital Image Processing 2 / 24


Mathematical Operations: Addition

Addition of images is similar to addition of matrices.

The images should be of identical size so that addition can be


done.

The size of the output image would be same as that of input


images. Only the pixel values of output image are the sum of
pixels values of input images.

Mathematical form of image addition is given as:


f (x, y ) = g (x, y ) + h(x, y )
where,
f (x, y ) : Output Image
g (x, y ) : Input Image 1
h(x, y ) : Input Image 2
Digital Image Processing 3 / 24
Mathematical Operations: Addition

Figure: Addition of Images

Digital Image Processing 4 / 24


Mathematical Operation: Subtraction
Subtraction of images is similar to subtraction of matrices.

The images should be of identical size so that subtraction can


be done.

The size of the output image would be same as that of input


images. Only the pixel values of output image are the
difference of pixels values of input images.

Mathematical form of image subtraction is given as:

f (x, y ) = g (x, y ) − h(x, y )

where,
f (x, y ) : Output Image
g (x, y ) : Input Image 1
h(x, y ) : Input Image 2
Digital Image Processing 5 / 24
Mathematical Operations: Subtraction
Mostly image subtraction is used for removal of background
from images, image matching, missing object tracing etc.

Figure: Subtraction of Images

Digital Image Processing 6 / 24


Mathematical Operations: Multiplication
Superimpose texture on images.

Convolution and correlation of images.

The mathematical form of image multiplication is given as:


where, f (x, y ) = g (x, y ). ∗ h(x, y )
f (x, y ) : Output Image
g (x, y ) : Input Image 1
h(x, y ) : Input Image 2

Figure: Multiplication of Images


Digital Image Processing 7 / 24
Logical Operations

Figure: Logical operations on image

Digital Image Processing 8 / 24


Average of Images

A noisy image can be represented by:

f (x, y ) = g (x, y ) + η(x, y )


where,
f (x, y ) : Averaged Image
g (x, y ) : Input Image
η(x, y ) : Noise

The noise is random and the content is fixed

The noise can be removed by taking more noisy images of the


same object and averaging them out

Digital Image Processing 9 / 24


Average of Images

Figure: Averaging the Image


Digital Image Processing 10 / 24
Spatial Filtering

The process of filtering is also known as convolving a mask


with an image.

A mask is a filter. Concept of masking is also known as


spatial filtering.

The general process of filtering and applying masks is consists


of moving the filter mask from point to point in an image.

At each point (x,y) of the original image, the response of a


filter is calculated by a pre defined relationship.

Digital Image Processing 11 / 24


Spatial Filtering

Figure: Masking
Digital Image Processing 12 / 24
Spatial Filtering

Figure: Masking over image

Digital Image Processing 13 / 24


Problems during masking of image

Problem arises when part of the mask is located outside the


image plane, we can use following techniques to overcome this
problem:

Discard the problem pixels (e.g. 512x512 input 510x510


output if mask size is 3x3).

Zero padding: expand the input image by padding zeros


(512x512 input 514x514 output) Zero padding is not good, it
creates artificial lines or edges on the border.

We normally use the gray levels of border pixels to fill up the


expanded region (for 3x3 mask). For larger masks a border
region equal to half of the mask size is mirrored on the
expanded region.

Digital Image Processing 14 / 24


MATLAB Code for Zero Padding

Figure: Zero Padding in MATLAB

Digital Image Processing 15 / 24


Effect of Zero Padding

Figure: Effect of Zero Padding

Digital Image Processing 16 / 24


Mask operation near borders

Figure: Masking around borders

Digital Image Processing 17 / 24


MATLAB code for weighted mask operation

Figure: MATLAB code for mask operation

Digital Image Processing 18 / 24


Types of Spatial Filters

Generally we have two types of spatial filters:


1. Linear filters or smoothing filters.
2. Frequency Domain Filters.

The linear filters are further divided into two categories:


1. Box Filter.
2. Weighted average filter.

Smoothing filters are used for blurring/noise reduction.


Blurring is usually used in pre-processing steps e.g., to remove
small details from an image prior to object extraction, or to
bridge small gaps in lines or curves.

Digital Image Processing 19 / 24


Smoothing Filters

Figure: Smoothing Filters

Digital Image Processing 20 / 24


Tasks
Task 1
Take any two images of your choice and perform image
addition, image subtraction and image multiplication. Do the
analysis on the results.

Task 2
Take any image of your choice, corrupt it by adding Gaussian
Noise and Salt and Pepper Noise. Compare and analyze
the result.
Hint: Use ”imnoise” MATLAB command.

Task 3
Perform noise removal by averaging multiple samples of an
image corrupted by gaussian noise. Show the effect of
averaging 5, 25 and 50 samples of noisy image. And comment
on the results.

Digital Image Processing 21 / 24


Tasks

Task 4
Take any image, corrupt it by adding gaussian noise and the
filter the image using 3x3 weighted smoothing mask. Do
analysis on result.

Task 5
Apply box filter mask of size 3x3,5x5,9x9,15x15 and 35x35 on
any gray scale image of your choice , show and comment on
the results.
Hint: Use ”fspecial” and ”imfilter” MATLAB commands.

Digital Image Processing 22 / 24

You might also like