LINEAR AND NONLINEAR FILTER FOR
IMAGE PROCESSING USING
MATLAB’S IMAGE PROCESSING
TOOLBOX
This report is submitted to the School of Engineering and Information
Technology, Murdoch University as a partial fulfilment of the requirement for
the Bachelor of Engineering (Honours)
FATIHAH MOHD PADZIL
PROJECT SUPERVISOR:
DR. GREGORY CREBBIN
JANUARY, 2016
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
i
ACKNOWLEDGEMENT
Firstly, I would like to take this opportunity to express my sincere gratitude to my supervisor,
Dr. Gregory Crebbin, who was always there to listen and give advice with patience and a
smile on his face, despite his busy schedule. His valuable comments, guidance and constant
encouragement gave me motivation to finish this project. Also, I cannot forget to thank Dr.
Linh Vu, as my second supervisor, and the School of Engineering and Information
Technology for their support throughout this project.
Finally, to all my family and friends, I would like to send my deep appreciation and respect
for their continuous support and encouragement throughout my thesis and entire degree.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
ii
ABSTRACT
The proposal of the thesis is basically to study techniques in digital image processing. This
thesis will cover two image processing areas, which are image restoration and image
enhancement. More specifically, image restoration will involve the removal of noise and
image enhancement will look into technique for edge enhancement.
In this project, two classes of filter will be introduced, which are linear and nonlinear filters.
Two type of noise source will be used which are Gaussian noise and salt and pepper noise.
For noise removal, the mean filter is used as example of a linear filter and the median filter is
used as an example of a nonlinear filter. For edge enhancement, only a linear filter is used,
which is the unsharp mask filter.
The simulation programs are written using the Image Processing Toolbox in MATLAB
(MATrix LABoratory). Test images that corrupted by noise will be used in investigations to
assess the strength and weakness for each type of filter.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
iii
TABLE OF CONTENTS
ACKNOWLEDGEMENT ...............................................................................................................................
ABSTRACT ................................................................................................................................................ ii
TABLE OF CONTENTS .............................................................................................................................. iii
1. Introduction .................................................................................................................................... 1
1.1 Problem scope ........................................................................................................................ 1
1.2 Thesis Objective ...................................................................................................................... 3
1.3 Report Outline......................................................................................................................... 4
2 Technical Review of Image Processing ........................................................................................... 5
2.1 Overview of Digital Image Processing ..................................................................................... 5
2.2 Image Processing System ........................................................................................................ 6
2.3 Digital Image Representation ................................................................................................. 7
2.4 Image Processing Operations ................................................................................................. 8
2.4.1 Noise ............................................................................................................................... 8
2.4.2 Neighborhood Operations ............................................................................................ 11
2.5 Image Restoration ................................................................................................................. 13
2.5.1 Linear Filter ................................................................................................................... 13
2.5.2 Nonlinear Filter ............................................................................................................. 15
2.6 Image Enhancement ............................................................................................................. 17
2.6.1 2.6.1 Unsharp mask filter .............................................................................................. 19
2.7 MATLAB ................................................................................................................................. 21
2.7.1 Image Processing Toolbox ............................................................................................. 21
3 Methodology ................................................................................................................................. 23
3.1 Image Input ........................................................................................................................... 23
3.2 Noise Removal ...................................................................................................................... 24
3.2.1 3.2.1 Process plan ......................................................................................................... 24
3.2.2 Mean Filter .................................................................................................................... 26
3.2.3 Median Filter ................................................................................................................. 27
3.3 Edge Enhancement ............................................................................................................... 29
3.3.1 Process Plan .................................................................................................................. 29
4 Results ........................................................................................................................................... 32
4.1 Mathematical rules ............................................................................................................... 32
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
iv
4.2 Mean Filter Result ................................................................................................................. 33
4.2.1 Result of 3 by 3 filtered image ...................................................................................... 33
4.2.2 Result of 5 by 5 filtered image ...................................................................................... 35
4.3 Median Filter Result .............................................................................................................. 37
4.3.1 Result of 3 by 3 filtered image ...................................................................................... 37
4.3.2 Result of 5 by 5 filtered image ...................................................................................... 39
4.4 Result of 11-by-11 window filter .......................................................................................... 41
4.5 Unsharp Mask Filter Result ................................................................................................... 41
4.6 Result Summary .................................................................................................................... 44
5 Problem Encountered and Countermeasures .............................................................................. 45
6 Conclusion and Recommendations............................................................................................... 46
6.1 Conclusion ............................................................................................................................. 46
6.2 Recommendations ................................................................................................................ 47
7 References .................................................................................................................................... 48
8 Appendix ....................................................................................................................................... 49
8.1 Appendix A ............................................................................................................................ 49
Result for a 11 by 11 mean filter ................................................................................................... 49
Result for a 11 by 11 median filter. ............................................................................................... 51
8.2 Appendix B ............................................................................................................................ 53
8.3 Appendix C ............................................................................................................................ 55
8.4 Appendix D ............................................................................................................................ 57
8.5 Appendix E ............................................................................................................................ 58
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
v
LIST OF FIGURES
Figure 1: Typical overall image processing system ................................................................................. 6
Figure 2: An image and the conversion used to represent rows (m) and columns (n) of the image. .... 7
Figure 3: Different type of noise ........................................................................................................... 10
Figure 4: A 3x3 window that centered over the picture element ........................................................ 11
Figure 5: Illustration of Median filter operates. ................................................................................... 16
Figure 6: Different between ideal edges and ramp edge ..................................................................... 17
Figure 7: Illustartion of Unsharp mask filter operates .......................................................................... 20
Figure 8: 8-bit image ............................................................................................................................. 23
Figure 9: Program simulation process for noise removal ..................................................................... 24
Figure 10: Pixel value before filter take place ...................................................................................... 26
Figure 11: Pixel value after filter take place ......................................................................................... 26
Figure 12: original pixel value ............................................................................................................... 27
Figure 13: After median filtering ........................................................................................................... 28
Figure 14: Process flow of image sharpening by unsharp mask filter .................................................. 30
Figure 15: 3-by-3 Mean filter with salt and pepper noise .................................................................... 33
Figure 16: 3-by-3 Mean Filter with Gaussian Noise .............................................................................. 34
Figure 17: 5-by-5 mean filter with salt and pepper noise..................................................................... 35
Figure 18: 5-by-5 mean filter wit Gaussian noise ................................................................................. 35
Figure 19: 3-by-3 median filtered image with salt & pepper noise ...................................................... 37
Figure 20: 3-by-3 median filtered image with Gaussian noise ............................................................. 38
Figure 21: 5-by-5 median filtered image with salt & pepper noise ...................................................... 39
Figure 22: 5-by-5 median filtered image with Gaussian noise ............................................................. 39
Figure 23: Result of blurring with mean filter ....................................................................................... 42
Figure 24: The edge image .................................................................................................................... 42
Figure 25: Image edge enhancement with different scaling constant k .............................................. 43
LIST OF TABLES
Table 1: Image file formats ................................................................................................................... 22
Table 2: MSE value for mean filter ........................................................................................................ 34
Table 3: MSE value for 5-by-5 mean filter ............................................................................................ 36
Table 4: MSE value for median filter ..................................................................................................... 38
Table 5: MSE value for 5 by 5 median filter .......................................................................................... 40
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
1
1. INTRODUCTION
1.1 PROBLEM SCOPE
In the modern world, digital images are necessary for a broad field of human
activities. Image processing is rapidly evolving field with growing application in science and
engineering. An image is a visual representation of an object or a scene produced by an
optical device such as mirror or lens (Marques 2011). Digital images are pictures that have
been transformed into a computer readable binary format consisting of logical 0s and 1s.
They are widely used in medical applications such as digital radiography and MRI (Magnetic
Resonance Imaging), in industrial applications such as safety system and quality control in
manufacturing process, and in law enforcement and security system applications such as
biometric techniques used in face, hand and fingerprint recognition (Solomon 2010)
(Marques 2011).
Image processing begins with the capture of an analog image by suitable devices,
which is then converted to digital image before the image is processed by computer for better
visual appearance to a human viewer (Marques 2011). Image processing covers a wide range
of techniques and algorithms. Digital image processing can be separated into four categories,
which are restoration, enhancement, coding and understanding (Lim 1990).
In image restoration, the objective is to reduce or remove the noise in an image. Two
type of filter will be designed, which are the mean filter, which is an example of a linear
filter, and the median filter, which is an example of a nonlinear filter (Lim 1990). Two type
of noise source are added to the image before filtering by both types of filter.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
2
In image enhancement, the objective is to improve the pictorial appearance for human
viewers and to prepare an image for storage and representation for machine perception. An
unsharp mask filter is an example of an edge enhancement filter (Solomon 2010).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
3
1.2 THESIS OBJECTIVE
The aim of the project is designing the two classes of filter in image processing for noise
removal and edge enhancement. The investigations that will be carried out in this project are
motivated by the following scenario:
An image is read using a special Matlab toolbox. For noise removal, noise is added to the
image. After that, different filters will be applied to the noisy image. The strengths and
weaknesses of each class of filter will be investigated and compared. For edge enhancement,
different scale degree of enhancement are used to the one type of blurred picture.
The main technical objectives of the thesis are:
To identify the requirements of filters for specified image processing applications.
These applications will include the removal of unwanted artefacts, such as noise, and
the enhancement of critical features, such as the edges of objects.
To develop a simulation program in Matlab, using the image processing toolbox.
To design both linear and nonlinear filters for specific image processing tasks, and to
compare the results of these two classes of filter.
To design and implement image filters using MATLAB.
To brief an introduction to the vast field of image processing.
The main personal development objectives of the thesis are:
To apply the theoretical and practical knowledge gained from 3 years of study at
Murdoch University.
To develop skills in programming, troubleshooting and system design.
To develop project and time management skills.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
4
1.3 REPORT OUTLINE
The remaining chapters of this thesis will describe to the works undertaken in the project. The
thesis is outlined as follows:
Chapter 2: Will present a literature review and provide a background of image
processing that covers the overview, techniques, and operations including edge
enhancement.
Chapter 3: Will describe the methodology and implementation of the simulation
program. The linear and nonlinear filters for noise removal and image edge
enhancement will be described. A process flow of the simulation program for both
types of noise removal filter and edge enhancement filter will be described clearly.
Chapter 4: Will investigate the findings of each type of image filtering technique.
The strengths and weaknesses of each filter will be discuss in this chapter.
Chapter 5: Will describe the problems that were encountered and the
countermeasures that were taken to finish the thesis project.
Chapter 6: Will conclude of the thesis report. This will also include recommendation
for future research based on the findings of this thesis.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
5
2 TECHNICAL REVIEW OF IMAGE PROCESSING
The aim of this chapter is to conduct a literature review on digital image processing.
This will include the basics of two image processing methods, which are image filtering and
image edge enhancement. It will end by introducing MATLAB functions for reading, writing,
querying, converting and displaying images. To conduct the review, books, journals, websites
and the online resources were consulted so that more could be learnt about image processing.
2.1 OVERVIEW OF DIGITAL IMAGE PROCESSING
The field of digital imaging has grown rapidly in recent years. Humans are exposed
to the images on a daily basis in print, photographs and computer displays, where the images
are still pictures that do not change with time. An image is a representation of an object or a
view that is produced by an optical device such as camera or mirrors. A digital image
represents an image by a two dimensional array of numbers, where each element of the array
is a sampled value of the original image within a small square segment of space. These
picture elements are called “pixels”. Each pixels is addressed by its rows and columns
position in the array (Marques 2011).
Digital image processing refers to the processing of a digital image by digital
computers. The processing serves two main purposes, which are to improve the pictorial
appearance of the original image for human viewers (Lim 1990) and to prepare the image for
storage, transmission and representation for autonomous machine perception (Lim 1990)
(Russ 2007). Digital image processing is widely used in various fields of application such as
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
6
industrial inspection, meteorology, astronomy, ultrasonic imaging, radiology, satellite
imaging, remote sensing, and law enforcement (Bovik 2009).
Digital image processing can be divided into three levels of processing which are low,
mid and high. Low level processes involve basic operations such as noise reduction, contrast
enhancement and image sharpening. The input and output at this level are both images. Mid-
level processes result in attributes being extracted from an image, such as edges, contours,
and the identity of individual objects. Lastly, higher level processes involves the analysis of
images and interpretation of the contents of a scene (Marques 2011).
2.2 IMAGE PROCESSING SYSTEM
A typical image processing system is built around a computer that performs digital
signal processing, as shown in Figure 1 (Marques 2011). The input source is usually an object
or a natural scene which is digitized and stored as a matrix of binary digits inside the
computer. The digitized image is then processed using either a general purpose computer, a
microprocessor or special purpose hardware. The result of processing is then displayed for
human viewing or is used as an input to another system. The display could be a monitor, a
photograph or it could be just a printer (Lim 1990).
Figure 1: Typical overall image processing system
object (input)
imaging system
(observer)
sample and
quantitize (digitize)
digital storage (store)
digital computer (process)
display (output)
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
7
2.3 DIGITAL IMAGE REPRESENTATION
A digital image, I (m, n) is a representation of an image as a two-dimensional array of
picture elements or pixels where the indicator m is represent the row of image and the
indicator n represents the column of the image. The number of column times the number of
rows gives the total number of pixels in the image. Figure 2 shows an image and the
conversion used to represent rows, m and column, n of an image. Each pixel is represents an
as intensity value of the image, with intensity values typically ranging from 0 to 255. For
grayscale images, a pixel is usually encoded as a value between 0 and 255, where “black”
corresponds to pixel value 0 and “white” corresponds to pixel value 255. Intermediate values
represent the various “shades of grey” (Marques 2011). For color images, each pixel is
encoded with red, green, and blue (RGB) components, where each component value usually
encoded in 8-bits, so that the combination these three elements is 24 bits in total (Tan 2007).
Figure 2: An image and the conversion used to represent rows (m) and columns (n) of the image.
The size of an image is a measure of its resolution. The resolution is related to image quality.
Most image are not square but also in rectangular form with a width up to 4000pixels. In
image processing the standard image sizes are used 256 x 256, 512 x 512 and 1024 x 1024
(Lim 1990).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
8
2.4 IMAGE PROCESSING OPERATIONS
In this section, several fundamental techniques that are commonly used in image
processing will be discussed. Digital image processing can be divided to four common
application areas which are image enhancement, restoration, coding and understanding. This
thesis will focus on image restoration (noise removal) and enhancement (edge enhancement).
The usual objective of these techniques is to make the processed image look better than the
unprocessed image (Lim 1990).
2.4.1 Noise
An important problem for image processing is noise removal. Noise is unwanted
contamination of an image. In digital images, noise can come from a variety of sources
including noise of capture noise that results of variations of lighting i.e. too much or too little
light, and processing noise that results from limitations in numerical precision, and
mathematical approximations (e.g. 𝜋 = 3.142. . ). A digital image is a representation of an
original scene but it is not always perfect because of limitations of resolution by sampling
and noise contamination (Solomon 2010).
Noise in images can be characterized in many ways. In image processing, two main
type of noise are often used, which are salt and pepper noise and Gaussian noise (Ajay 2015).
Salt-and-pepper noise, also known as an impulsive noise, is caused by the random
introduction of pure white or pure black pixels into image. This noise is commonly caused by
camera sensor faults. Meanwhile, Gaussian noise, is also known as additive noise is noise
with random intensity levels that have a Gaussian or normal probability density function.
Usually it is caused by random processes in electronic equipment. Gaussian noise is the most
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
9
commonly used noise model in image processing (Solomon 2010). Figure 3 shows an
example of different types of noise at an image.
(a): Original Image
(b): Image with salt-and-
pepper noise
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
10
(b): Image with Gaussian noise
Figure 3: Different type of noise
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
11
2.4.2 Neighborhood Operations
Usually, in image processing it is impractical to work on the whole image at once.
When processing an image, usually the only a reference pixel and nearby pixels are used. To
apply this principle, a neighborhood technique (also known as windowing technique) is used.
A window in image processing is usually a small square array centered on the reference pixel.
The operation on a given pixel is called a neighborhood operation because it used only the
original pixel value at the given point and the pixel values in a neighborhood around that
point (Marques 2011). For example, a 3x3 window that is centered over the picture at
location (i, j) is shown in Figure 4.
Figure 4: A 3x3 window that centered over the picture element
A windowing function starts by defining a reference point in the input array, and then
it performs an operation that involves only pixels in the neighborhood surrounding that
reference point. It could be any shape or size of window but usually it is a square array
centered on the input pixel (e.g. 3 x 3 or 5 x 5 windows centered at the given input pixel)
(Marques 2011).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
12
The result of the operation becomes the pixel value at the same location in the output
image. For the edge pixels, it also use same window size, but the pixels value for outside an
image is set at 0. The process is repeated for every pixel in the input image. For example, the
function that is applied might be an averaging operation; it will sum the values of the
neighborhood pixel then divide the result by the number of pixels in the neighborhood,
thereby replacing the centre pixel by the “average” values of pixels in the neighborhood
(Jaehne 2005).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
13
2.5 IMAGE RESTORATION
Image restoration is used to carry out several useful tasks in image processing. One of
the primary tasks is noise removal. A filter is used to reduce the amount of unwanted noise in
the particular image. As mentioned early, a filter usually operates on a neighborhood of
pixels in an image. In the spatial domain, image filtering is done by convolving the input with
the filter function to obtain the filtered image, (Abdul Rasak Zubair 2014) where the
convolution takes place over the neighborhood of each input pixel.
A mean filter and a median filter are both types of filter that can be used for noise
removal. Whereas the mean filter is an example of a linear filter, the median filter is an
example of a nonlinear filter (Lim 1990) (Solomon 2010). The next section will discuss in
more detail both types of filter.
2.5.1 Linear Filter
Linear filtering is carried out by convolving the input with a filter function to obtain
the filtered image. A linear filter can be written as follows (Marques 2011):
𝑓(𝑥, 𝑦) = ∑ 𝐼 (𝑚, 𝑛) ∙ 𝑤(𝑥 − 𝑚, 𝑦 − 𝑛)
𝑚,𝑛
Here, I is represents the original image, f is represents the filtered pixels value, and w
is represents the filter coefficients.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
14
The mean filter, also known as an averaging filter, is an example of a simple linear
filter. It operates by assigning the average value of all pixels in the neighborhood of the input
pixels to the corresponding pixel in the output image. The principle of this filter matches
perfectly with it name, as each pixel in the targeted image is replaced with the mean value of
pixels surrounding it. The mathematical formulation for the mean filter (Marques 2011) is
given as follows:
𝑓(𝑥, 𝑦) =1
𝑚𝑛 ∑ 𝐼(𝑚, 𝑛)(𝑚,𝑛)∈𝑊
Where: I = the noisy image, f = the restored image and (m, n) = the row and column
coordinate respectively, within a window W of size m x n where the operation take places
centered on input pixels at (x, y) (Marques 2011).
For example, if a 3 x 3 window is used, the averaging operation uses the following
3x3 mask, which is placed over at the input pixel and its eight neighborhoods:
1
9 [
1 1 11 1 11 1 1
]
Averaging has the effect of smoothing the noise, as along as the noise is addictive and
not impulsive. This filter is effective in removing the noise, but at the same time it blurs the
image. Despite this drawback, it is still a useful filtering technique.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
15
2.5.2 Nonlinear Filter
One type of nonlinear filter operates by ranking the pixels in the specified
neighborhood. It replaces the target pixel by a value corresponding to the chosen rank. The
rank could be maximum value, minimum value or middle value in the neighborhoods pixels
(Marques 2011) (Solomon 2010). In this project, the median filter will be used as an example
of nonlinear filter. It used the middle value in the neighborhood of the target pixel.
The median filter is a popular example of a nonlinear filter that is used in image
processing. The median filter overcomes one of the limitations of the mean filter, which is its
inability to reduce impulsive noise. The median filters are also useful in preserving edges of
an image while reducing the image noise (Marques 2011).
The median filter operates by sorting the pixel values within a neighborhood, then
finding the median value from that ordered list and the replacing the original pixel by this
median value in output image (Marques 2011). The mathematical formulation for median
filter is given as follows:
𝑓(𝑥, 𝑦) = 𝑚𝑒𝑑𝑖𝑎𝑛 {𝐼 (𝑚, 𝑛)|(𝑚, 𝑛) ∈ 𝑊}
Where, I = the noisy image, f = the restored image and (m, n) = the row and column
coordinate respectively, within a window W of size m x n where the operation take places
(Marques 2011).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
16
For example, the pixel values within a window are 9, 12, 0, 5, 5, 9, 8, 10 and 7, and
they are processed and ranked in order to give 0, 5, 5, 7, 8, 9, 9, 10 and 12. The median
values is 8, which is then used as the output pixel value (Marques 2011). Figure 5 shows an
illustration of how the median filter works.
Figure 5: Illustration of Median filter operates.
Compared to the mean filtering technique, the median filter simply replaces each
pixel with the middle value in the neighborhood of the pixel, instead of using an averaging
operation. As the median is one of the pixel values in the input image, the median filter does
not create a new pixel value. This helps in preserving edges and shows why these filters can
be useful for image processing (Solomon 2010).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
17
2.6 IMAGE ENHANCEMENT
Instead of image smoothing for image restoration, there are techniques for image
enhancement that are called edge enhancement or image sharpening. If noise removal is an
application of low pass filtering, then the image sharpening is an example of high pass
filtering. Image sharpening refers to any enhancement technique that highlights edges and
fine details in an image. Because edges contain the high frequency components of an image,
high pass filters are used to sharpen the images and increase the contrast. Image sharpening is
widely used in printing and photography (Bovik 2009).
An edge represents a discontinuity or change of intensity (e.g. brightness, contrast) or
color (e.g. luminance, chrominance) in an image. Edges in grayscale images appear at
transitions between dark and bright areas in an image such as the boundaries that separate
foreground objects and background scene. Figure 6 illustrates the difference between an ideal
edge which is a sharp transition, and a ramp edge, which is a gradual transition between dark
and bright areas in an image (Marques 2011) (Abdul Rasak Zubair 2014).
a) Ideal edge
b) Ramp edge
Figure 6: Different between ideal edges and ramp edge
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
18
Edge detection is the extraction of edges of significant objects in an image. It is
basically a method of segmenting an image into regions based on discontinuities between
their regions. Detection of edges is very useful in many applications such as pattern
recognition and fingerprint and iris biometric identification, space science and robot vision
(Abdul Rasak Zubair 2014).
Image edge enhancement enhances the edges, such that an image with highlighted
edges is more attractive than the original image. The objective of image edge enhancement is
to improve visual appeal of the processed image compared to the unprocessed image. An
alternative to an image sharpening filter is the unsharp mask filter (Marques 2011).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
19
2.6.1 2.6.1 Unsharp mask filter
Unsharp mask filtering is method that boosts the high frequency edge information in
images (Lim 1990). An unsharp mask filter operates by subtracting a blurred version of the
image from the original image in order to highlight the edges in the image. An unsharp
masking filter is described by the equations (Solomon 2010):
𝐼𝑒𝑑𝑔𝑒(𝑚, 𝑛) = 𝐼𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙(𝑚, 𝑛) − 𝐼𝑠𝑚𝑜𝑜𝑡ℎ𝑒𝑑(𝑚, 𝑛)
𝐼(𝑠ℎ𝑎𝑟𝑝)(𝑚, 𝑛) = 𝐼𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙(𝑚, 𝑛) + 𝑘 [𝐼𝑒𝑑𝑔𝑒(𝑚, 𝑛)]
where k is a scaling constant.
To use this approach, first the edge image, 𝐼𝑒𝑑𝑔𝑒 must be produced by subtracting the
smoothed image, 𝐼𝑠𝑚𝑜𝑜𝑡ℎ𝑒𝑑 from the original image, 𝐼𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙 , where the smoothed image,
𝐼𝑠𝑚𝑜𝑜𝑡ℎ𝑒𝑑 is often obtained by filtering the original image with a low pass filter, of which
mean filter is the simplest example. (Solomon 2010).
Using a specified scaling constant 𝑘, the edge image is then added to the original
image to obtain the sharpened image. As the scaling constant k is increased from 0 to 𝑘𝑙𝑖𝑚𝑖𝑡 ,
the degree of enhancement is increased. Generally, values for k is between 0.2 and 0.7 are
acceptable, depending on level of enhancement required (Solomon 2010) (Bovik 2009).
Figure 7 illustrate the idea of unsharp mask filter works.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
20
Figure 7: Illustartion of Unsharp mask filter operates
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
21
2.7 MATLAB
MATLAB (MATrix LABoratory) is a high-level text language and interactive
environment used for numeric computation and data analysis (The MathWorks Inc 2016).
MATLAB is the software that is used to illustrate signal processing concepts in this project. It
consists of a main engine to perform computations and also several toolboxes for special
purpose applications. Some special code will clarify why these are used instead of the other
code. MATLAB is an essential of learning for Industrial Computer Systems Engineering
(ICSE) and Instrumentation & Control Engineering (ICE) students as it can be used to
simulate process systems and analyze process data. This section will describe the Image
Processing Toolbox (IPT) in MATLAB that is used in developing the simulation programs
for this thesis.
2.7.1 Image Processing Toolbox
The image processing toolbox is distributed by MathWorks is contains a collection of
functions for performing different operations specifically for signal and image processing.
The operations available in this toolbox are: image analysis, segmentation, morphology,
image enhancement, noise removal, blurring, geometric transformation, image registration
and others. Each of these operations is performed by using their own in-built functions and
can operate on diverse image types and on different features of the images (MathWorks
2016).
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
22
In MATLAB’s IPT, there are two basic built-in functions, imread and imshow, that
are used to read and display the image respectively. The imread function allows users to read
image files in any type of image format. While imshow is used to display an image and
includes a number of optimization parameters for setting properties associated with image
display. These functions support all prevalent image formats and create the image array for
use by other MATLAB functions. A various type of image format can be used inside the
MATLAB toolbox. Table 1 simplify the image formats that are supported by MATLAB
(Solomon 2010). For this project, the TIFF format was chosen because it comes with
compressed and uncompressed formats.
Table 1: Image file formats
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
23
3 METHODOLOGY
This section will discuss the approach that is used to develop the simulation program.
Both of the core image processing operations, which are noise removal and edge
enhancement, will be discussed here.
3.1 IMAGE INPUT
The original images that is used in this thesis come from the Signal and Image
Processing Institute (SIPI) at University of Southern California (USC). These images can be
accessed in their webpage (SIPI 2015). The images in the USC-SIPI image database are
provided for research purposes. SIPI provide image of various sizes in their database, the
images are stored in TIFF format.
Figure 8: 8-bit image
Figure 8 show the original image clock is used throughout project. This grayscale image is
256 width x 256 height. This is an uncompressed version of the original image.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
24
3.2 NOISE REMOVAL
The purpose of this section is to describe the methods that are used to complete the
simulation program to filter images for noise removal. As discussed in Section 2.5, two types
of filter are used for noise removal: mean filter and median filter.
3.2.1 3.2.1 Process plan
In this thesis project, the image database was simulated in MATLAB’s IPT by following the
flow diagram given in Figure 9.
Figure 9: Program simulation process for noise removal
Read
•Input image
Convert
•Indexed image
•Double precision classes
Add
•Salt and Pepper Noise
•Gaussian Noise
Choose
•Window size for mean Filter and median Filter
Display
•Original image
•Noisy Image
•Filtered image
Compare
•Mean- square value
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
25
First, when the simulation program is run the image input is read using imread
function. The input image is a grayscale image with 256 x 256 pixels with 1 byte per pixel
that stored an intensity value from 0 to 255. This image is indexed image in uint8 (unsigned
8-bit integer) data classes. To make the input image suitable for image processing that
performs a numbers of an arithmetic operations, the image input is changed from uint8 to
double (double precision floating point) class to allow for more precision. This class
increases storage from 1 byte per pixel to 8 bytes per pixel, with pixels value that range from
0.0 to 1.0, with no negative numbers. This format is more useful when arithmetic operations
are performed.
Next is where the noise is added to the input image. Two types of noise were
introduced in Chapter 2 Section 2.4.1 which were salt and pepper noise and Gaussian noise.
The imnoise function in IPT can set the type of noise along with intensity parameter value
ranging from 0 to 1. The noise is added to the image. For salt and pepper noise, the noise
density, which has a default value of 0.5 is the additional parameter. While, for Gaussian
noise, mean and variance of Gaussian noise is needed in, this has default values of zero mean
with 0.01 variance.
Then the window operation size is chosen. In the simulation program the window size
can be added manually. This makes it easier to change the size when the visual appearance of
image is compared for different window sizes. Lastly, after filtering the original image, the
noisy image and the filtered image are displayed. There will be two windows that will pop up
for image display, where the “Figure 1” window displays the original image and the “Figure
2” window display the noise added image and the filtered image.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
26
When the image displays, the image processing results can be see subjectively. But to
confirm that a filter is good at reducing noise, the mean-square error between original and
processed images is calculated. The smaller the error, the better the filtering performance.
3.2.2 Mean Filter
The mean filter operates by replaces each pixel by the average value of the
neighborhood. In this simulation a by 3 x 3 mean window filter is used. The method of mean
filter works is illustrated in Figures 10 and 11:
Figure 10: Pixel value before filter take place
Figure 11: Pixel value after filter take place
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
27
Consider the pixels at the coordinate (2, 3), which has an original value of is 0.7216
as shown in Figure 10. The nine neighboring pixels value are summed and then divided by 9
to give the average value;
1
9(0.7412 + 0.7176 + 1.0 + 0.7490 + 0.7216 + 0.7176 + 1.0 + 0.7296 + 0.7176)
= 𝟎. 𝟕𝟖𝟖𝟐
The output value is shown in Figure 11. The process is repeated at the next pixel until
all pixels are processed. The same method could be apply to 5 x 5 mean filter but, this time a
25 neighborhood of pixels would be used in the averaging.
3.2.3 Median Filter
The median filter operates by sorting the pixel values within the neighborhood, where
the median value within the window replaces the original value. The simulation using a 3 by
3 median filter is shown in Figures 12 and 13.
Figure 12: original pixel value
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
28
Figure 13: After median filtering
Again, the pixels at coordinate (2, 3) with original pixel values of is 0.7216, is shown in
Figure 12. The nine neighboring pixels is arranged in ascending order as follows:
0.7176, 0.7176, 0.7176, 0.7216, 0.7255, 0.7296, 0.7412, 0.7412, 0.7490
The median value of 0.7255 replaces the original value, as shown in Figure 13. The process is
repeated at the next pixel until all pixels are processed. The same method could be applied to
a 5 x5 median filter but this time 25 neighborhood pixels would be sorted.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
29
3.3 EDGE ENHANCEMENT
The purpose of this section is to describe a method of operation that is used to complete
the simulation program for image sharpening by using an unsharp mask filter. As discussed
in Section 2.6.1, the unsharp mask filter is a technique that is used to increases the sharpness
of the image by using a blurred image of the original input image.
3.3.1 Process Plan
In this section, the image sharpening was simulated by following the flow diagram shown in
Figure 14.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
30
Figure 14: Process flow of image sharpening by unsharp mask filter
Read
• Input image
Convert
• Double precision
Blur
• Using mean filter 3 by 3 window
Subtract
• Original image with filtered image
Choose
• Scaling constant, k
Add
• Original image with blurry image
Display
• Original image and sharpening image
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
31
The simulation for edge enhancement is based on the unsharp masking theory
(Solomon 2010) (Marques 2011). Firstly, the image was read by using the imread function.
The image input values that range from 0 to 255 in the uint8 data class are converted to the
double precision data class with values that range from 0.0 to 1.0. This allows for more
precise calculation when the arithmetic operations of addition, and subtraction are done.
Next, the input image is filtered with a 3 x 3 mean filter to obtain the blurred image
from an original image. The blurred image is now subtracted from the original image to get
an edge image. The edge detect image will be displayed together with the final result.
Then, the scaling constant k is chosen. It can be varied manually in this simulation
program. After the scaling constant k is chosen, each pixel in the edge images is scaled by k
and then is added back into the original image to obtain the edge sharpened image. The image
edge enhancement was tested using three different values of scaling constant.
Lastly, the blurred image, the edge image and the enhanced image are displayed
together in the “Figure 1” window.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
32
4 RESULTS
In this section the results from this project are presented. The results are in the form of
filtered images and mean square error measurements. For noise removal technique, two types
of noise are added to the input image. The filtered image is then compared to the original
noise-free image and noisy image. The results will show how effective the filters are at
removing the different kinds of noise in the image. For edge enhancement, three different
scaling factors are compared to find scaling factor that gives the best enhancement of the
edges in the image.
4.1 MATHEMATICAL RULES
The mean square error (MSE) is chosen as an objective metric for deciding which
type of filter is best at removing the different types of noise. MSE is widely used in image
processing (Kaiwen Zhang 2002). It measures overall gray-value difference between pixels of
the reference image and the filtered image for the entire image without considering
correlation between the neighborhood pixels. The MSE is defined as:
𝑀𝑆𝐸 =1
𝑚𝑛 ∑ ∑(𝑥𝑜𝑟𝑖𝑔𝑖𝑛𝑎𝑙(𝑚, 𝑛) −
𝑛
𝑥𝑓𝑖𝑙𝑡𝑒𝑟𝑒𝑑(𝑚, 𝑛))2
𝑚
Pixels are treated individually so all pixels in the image are equally important. As a
human observer, some pixels errors may cause more significant visual effects than other
pixels errors, but MSE would assign the same error value to both pixels. The greatest quality
is when the MSE equal to zero. The smaller MSE, the better the filter for filtering that type of
noise.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
33
4.2 MEAN FILTER RESULT
4.2.1 Result of 3 by 3 filtered image
The first result that is presented are for the 3-by-3 mean filter applied to images with
both type of noise. From Figure 15 and Figure 16, the filtered image give evidence of less
noise than the unfiltered noisy image. The noise tends to be removed but some details such as
edges, becomes blurred so, while the mean filter can remove the noise, it tends to blur the
image.
Figure 15: 3-by-3 Mean filter with salt and pepper noise
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
34
Figure 16: 3-by-3 Mean Filter with Gaussian Noise
The Gaussian noise is removed more effectively with the mean filter compared to salt
and pepper noise. The salt and pepper noise becomes blurred but is not removed. The mean-
square-errors support these observations. The MSE value is calculated for, the original to the
noise added image, and the original image to the filtered image. The values are recorded in
Table 2.
Table 2: MSE value for mean filter
Gaussian Salt and pepper
Original image and noise added image 0.01617 0.01753
Original image and filtered image 0.00425 0.0464
From the table, the filtered Gaussian noise image has the smallest MSE value, which
shows that the mean filter removes Gaussian noise better than it removes salt and pepper
noise.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
35
4.2.2 Result of 5 by 5 filtered image
This is result that is presented are for the 5-by-5 mean filter applied to images with
both type of noise. Figure 17 shows the 5 by 5 mean filter with salt and pepper noise and
Figure 18 shows the 5-by-5 mean filter with Gaussian noise. From the image review, the both
image became more blurred because the mean filter tends to blur the noise instead of reduce
it. It shows that window 5-by-5 mean filter not really suitable to uses when to reduce the
noise.
Figure 17: 5-by-5 mean filter with salt and pepper noise
Figure 18: 5-by-5 mean filter wit Gaussian noise
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
36
Table 3: MSE value for 5-by-5 mean filter
Gaussian Salt and pepper
Original image and noise added image 0.01789 0.01800
Original image and filtered image 0.00524 0.00575
The MSE value for 5-by-5 mean filter is recorded in Table 3. From the table, with the
almost same noise- added to the image, the Gaussian noise is seen to be more reduce from the
image but the both image filtered look more blurred.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
37
4.3 MEDIAN FILTER RESULT
4.3.1 Result of 3 by 3 filtered image
The median filter has uses in image processing because it is the simplest nonlinear
filter that can produce interesting results. Figure 19 shows an image with salt and pepper
noise that is filtered by 3 x 3 median filter, while Figure 20 shows Gaussian noise that is
filtered by the same median filter.
Both types of noise are reduced in the image, but by looking at Figure 19, it is clear
that salt and pepper noise has being removed more effectively than the Gaussian noise. From
these images, the median filter is not only good at removing the noise but also at preserving
the edges of the image. It is clear that the median filter works better when removing the salt
and pepper noise compared to removing Gaussian noise.
Figure 19: 3-by-3 median filtered image with salt & pepper noise
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
38
Figure 20: 3-by-3 median filtered image with Gaussian noise
The MSE value for the median filter is calculated between the original and the noise-
added image, and between the original image and the filtered image. The MSE for both types
of noise are recorded in Table 4. From the table, the filtered salt and pepper noise image has
the smallest MSE value, which is near to zero. By comparing Table 2 and Table 4, the
median filter is considerably more effective than mean filter in removing salt and pepper
noise.
Table 4: MSE value for median filter
Gaussian Salt and pepper
Original image and noise added image 0.01630 0.01750
Original image and filtered image 0.00473 0.00126
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
39
4.3.2 Result of 5 by 5 filtered image
This is result that is presented are for the 5-by-5 median filter applied to images with
both type of noise. Figure 21 shows the 5-by-5 median filter with salt and pepper noise and
Figure 22 shows the 5-by-5 median filter with Gaussian noise.
Figure 21: 5-by-5 median filtered image with salt & pepper noise
Figure 22: 5-by-5 median filtered image with Gaussian noise
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
40
From the image review, for salt and pepper noise clearly can see the noise is remove
from the image and still have some edge is preserved. For Gaussian noise, the image become
a bit blurred but still good when compare to the 5-by-5 mean filter.
The mean-square-errors support these observations. The MSE value is calculated for,
the original to the noise added image, and the original image to the filtered image. The values
are recorded in Table 5.
Table 5: MSE value for 5 by 5 median filter
Gaussian Salt and pepper
Original image and noise added image 0.01699 0.01768
Original image and filtered image 0.00407 0.00232
From the table, the salt and pepper noise is seen to work better than the Gaussian noise
when to remove the noise using median filter. By comparing to window of median filter, 3-
by-3 window is work better than the 5-by-5 window filter.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
41
4.4 RESULT OF 11-BY-11 WINDOW FILTER
The uses of larger windows for mean and median filter were also studied. A window
of size 11-by-11 was chosen. The result are not included here because the larger windows
caused the image to become more blurring, especially when using mean filter on salt and
pepper noise which is not effective at all. The results for 11-by11 mean filter and 11-by-
11median filter are included in Appendix A of this thesis.
4.5 UNSHARP MASK FILTER RESULT
The unsharp masking filter is one technique for edge enhancement. Figure 19 shows
the blurred image resulting from the application of a 3 by 3 mean filter to the input image.
The blurred image is then subtracted from the original image to obtain the edge difference
image, as shown in Figure 23. The final enhancement images based on using different scaling
constants is shown in Figure 24.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
42
Figure 23: Result of blurring with mean filter
Figure 24: The edge image
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
43
k= 0.2
MSE = 0.00009
k= 0.5
MSE =0.00056
K= 0.7
MSE = 0.00110
k= 0.8
MSE =0.00143
k=7
MSE = 0.109
K=11
MSE= 0.27126
Figure 25: Image edge enhancement with different scaling constant k
The results in Figure 25 show that by increasing the parameter k, the degree of
enhancement increases but the noise within the image becomes more apparent. The different
values of k that were used are 0.2, 0.5, 0.7, 0.8, 1.0 and 11. A value of 0.8 is upper value for
k, beyond which the noise effects become more severe.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
44
4.6 RESULT SUMMARY
In this section the findings of this project are summarized here. For noise removal
technique, two type of noise is added which is salt-and-pepper and Gaussian noise and two
type of filter is apply to remove this noise.
For linear filter example is mean filter, works better when removing the Gaussian noise
compared to removing the salt and pepper noise. For nonlinear filter example is median filter.
Median filter are introduced to overcome the limitation of mean filter so it works better when
removing the salt and pepper noise comparing to removing Gaussian noise. But median filter
not only remove the noise but also preserve the edges of image. For noise removal, only
small size of window is consider to use for removing noise. This can see when the bigger size
of window is applied the image tends to become more blurred when filtering job is done.
For edge enhancement, three different scaling factors are compared to find scaling
factor that gives the best enhancement of the edges in the image. The k value is measured
from k = 0.2 to 11. Results from Figure 25 shows that the degree of enhancement, 0.8 is
chosen because the edge is enhance nicely without amplify the false edge (noise). The false
edge is amplify can clearly see on when k = 11 compared to the original image.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
45
5 PROBLEM ENCOUNTERED AND COUNTERMEASURES
During the progress plan stage of this project a few of problem were encountered.
Corrective action was taken to ensure this project and thesis could be completed according to
the plan and design.
The first problem that was identified was that Murdoch University only has one
license for the MATLAB Image Processing Toolbox. This license only could be accessed
from a special server which is ENGEN2 and only one person can accessed the toolbox at the
time. To solve this problem, a request was sent to the technician, Will Stirling, to open this
server.
The second problem was the limited resources in the library. There were only a
limited number of book for image processing. A few useful books could not be found either
in hardcopy or e-book format from library resources. There are also a lot of journal articles
available but the books have more detail explanation about MATLAB code. The
countermeasure for this problem was to borrow a book from the supervisor.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
46
6 CONCLUSION AND RECOMMENDATIONS
6.1 CONCLUSION
In conclusion, different filters have different strengths and weaknesses. So different
types of filter are needed when solving image processing problems. Sometimes a nonlinear
filter may work better than a linear filter. In image restoration, the nonlinear filter can do a
better job that the linear filter. As seen in the results, while the linear filter can remove the
noise, the nonlinear filter not only removes the noise but also can preserve edges. This is
why, not just a single filter is needed to remove noise, but a range of filters are needed,
depending on the situation encountered.
In summary, this project had archieved its main objective to develop a simulation
program in MATLAB using the Image Processing Toolbox. Basic knowledge about image
processing was introduced in the literature review chapter. Both linear and nonlinear filters
for specific image processing tasks were designed: the mean filter as a linear filter type and
the median filter as a nonlinear filter type. The requirements of the filters to remove
unwanted artefacts (noise) and to enhance edges were identified.
Lastly, with this thesis, the main achievement when developing these programs, was
that the theoretical and practical knowledge gained from three years study at Murdoch could
be applied to a new problem. This is a significant achievement because it helped develop the
skills in programming, troubleshooting and system design.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
47
6.2 RECOMMENDATIONS
Due to the time limitation, there is still a lot of works that could be done in this area.
Following the investigations described in this thesis, a number of more advanced image
processing tasks could be investigated:
- The simulation programs were limited to basic applications of image processing;
the existing program could be extended to other application such as object
identification and image coding.
- The simulations used only a gray-level image. The next step would be to upgrade
to using color images.
- Other enhancement methods can be applied to enhance the edges of images such
as the Laplacian-based method (Solomon 2010).
- The simulation program was built by using the MATLAB Image Processing
Toolbox. It is possible to build simulators using other toolbox such as signal and
processing toolbox and computer vision toolboxes.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
48
7 REFERENCES
Abdul Rasak Zubair, Olasebikan Alade Fakolujo. "International Journal of Computer and Information
Technology." Image Edge Detection and Image Edge Enhancement: Numerical experiment on
High Pass Spatial Filtering, 2014: 772-781.
Ajay Kumar Boyat, Brijendra Kumar Joshi. "A Review Paper." Noise Models in Digital Image
Processing, April 2015.
Bovik, Alan C. The Essential Guide to Image Processing 2nd Edition. London, UK: Academic Press,
2009.
Jae S. Lim. Two- Dimensional Signal and Image Processing. Englewoods Cliff, NJ: Prentice Hall, 1990.
Jaehne, Bernd. Digital Image Processing. Heidelberg, Germany: Springer-Verlag Berlin, 2005.
Kaiwen Zhang, Shouzhong Wang, Xiepen Zhang. A New Metric for Quality Assesment of Digital Image
Based on Weighed-Mean Square Error, 2002.
Marques, Oge. Practical Image and Video Processing Using Matlab. Somerset, NJ , USA: John Wiley &
Son, 2011.
Russ, John C. The Image Processing Handbook 5th Edition. Raleigh, North Carolina: Taylor & Francis
Group, 2007.
SIPI. Signal and Image Processing Institute, University of Southern California. 2015.
http://sipi.usc.edu/database/ (accessed September 2015, 1).
Solomon Chris, Toby Brecken. In Fundamental of Digital Image Processing: A Practical Approach with
Example in Matlab. Hoboken, NJ: John Wiley & Son, 2010.
Tan, Li. Digital Signal Processing : Fundamentals and Applications. Burlington, MA, USA: Academic
Press, 2007.
The MathWorks Inc. 2016. http://au.mathworks.com/help/images/product-description.html
(accessed December 2015).
Zubair, Abdul Rasak. "International Journal of Research in Commence, IT & Management."
Comparison of Image Enhancement Techniques, 2012.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
49
8 APPENDIX
8.1 APPENDIX A
Result for a 11 by 11 mean filter
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
50
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
51
Result for a 11 by 11 median filter.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
52
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
53
8.2 APPENDIX B
Appendix B shows the Matlab code for noise removal by a mean filter.
clc; clear;
% Read image. I = imread('C:\Users\32112635\Documents\MATLAB\Thesis Ariesa\clock.tiff');
[r c]=size(I)
%convert image to double precesion Id = im2double (I);
% Apply salt and pepper noise to image using |imnoise| . Isp = imnoise(Id,'salt & pepper',0.05);
% Apply gaussian noise to image using |imnoise| . Ig = imnoise(Id,'gaussian',0,0.02);
%predefined filter NxN window (mean filter) n = input ('enter the window size of MEAN Filter, (n for NxN matrix):');
%specify window size mean = ones (n,n)/(n*n)
%imfilter=computes each element of the output,using double-precision
floating point. %for salt and pepper noise Ispf = imfilter (Isp,mean);
%for gaussian noise Igf = imfilter (Ig,mean);
% Display original image and noise image for comparison. figure (1) subplot (1,2,1), imshow(Isp) title ('Image with Noise (Salt&Pepper)') subplot (1,2,2), imshow(Ispf) title ('Filtered Image Using Mean Filter')
% Display original image and noise image for comparison. figure (2) subplot (1,2,1), imshow(Ig) title ('Image with Noise (Gaussian)') subplot (1,2,2), imshow(Igf) title ('Filtered Image Using Mean Filter')
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
54
%-------------------MSE-------------------- % 1st try % ORIGINAL - FILTERED IMAGE diffg = ( Id -Ispf).^2; MSE1= sum(sum(diffg))/ (r*c); fprintf('\n\nORIGINAL IMAGE AND FILTERED IMAGE') fprintf('\nThe Mse value for Salt and pepper noise is: %.5f', MSE1);
diffg = ( Id -Igf).^2; MSE2= sum(sum(diffg))/ (r*c); fprintf('\nThe Mse value for Gaussian noise is: %.5f', MSE2);
%-------------------MSE-------------------- % 2nd try % OORIGINAl - NOISY IMAGE
diffg = ( Id -Isp).^2; MSe1= sum(sum(diffg))/ (r*c); fprintf('\n\nORIGINAL IMAGE AND NOISE ADDED IMAGE'); fprintf('\nThe Mse value for salt and pepper noise is: %.5f', MSe1);
diffg = ( Id -Ig).^2; MSe2= sum(sum(diffg))/ (r*c); fprintf('\nThe Mse value for Gaussian noise is: %.5f', MSe2);
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
55
8.3 APPENDIX C
Appendix C shows the Matlab code for noise removal by a median filter.
clc; clear;
% Read image. I = imread('C:\Users\32112635\Documents\MATLAB\Thesis Ariesa\clock.tiff');
%convert image to double precesion Id = im2double (I); [r c] = size (I) % Apply salt and pepper noise to image using |imnoise| . Isp = imnoise(Id,'salt & pepper',0.05);
% Apply gaussian noise to image using |imnoise| . Ig = imnoise(Id,'gaussian',0,0.02);
%predefined filter NxN window (mean filter) n = input ('enter the window size of MEDIAN Filter, (n for NxN matrix):');
%specify window size
%B = medfilt2(A, [m n]) performs median filtering, where each output pixel % contains the median value in the m-by-n
% neighborhood around the corresponding pixel in the
% input image.
%median = medfilt2 ((image_noise,[n n]); %median filter with NxN window
% %for salt and pepper noise Ispf = medfilt2 (Isp,[n n]);
%for gaussian noise Igf = medfilt2 (Ig, [n n]);
% Display original image and noise image for comparison. figure (1) subplot (1,2,1), imshow(Isp) title ('Image with Noise (Salt&Pepper)') subplot (1,2,2), imshow(Ispf) title ('Filtered Image Using Median Filter')
% Display original image and noise image for comparison. figure (2) subplot (1,2,1), imshow(Ig) title ('Image with Noise (Gaussian)') subplot (1,2,2), imshow(Igf) title ('Filtered Image Using Median Filter')
%------------------Mean Square Error--------------- %--- calculates the MSE. % calculates the "square error" image. % change to double so that we can get negative differences. %--------------------------------------------------------\
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
56
%-------------------MSE-------------------- % 1st try % ORIGINAL - FILTERED IMAGE diffg = ( Id -Ispf).^2; MSE1= sum(sum(diffg))/ (r*c); fprintf('\n\nORIGINAL IMAGE AND FILTERED IMAGE') fprintf('\nThe Mse value for Salt and pepper noise is: %.5f', MSE1);
diffg = ( Id -Igf).^2; MSE2= sum(sum(diffg))/ (r*c); fprintf('\nThe Mse value for Gaussian noise is: %.5f', MSE2);
%-------------------MSE-------------------- % 2nd try % OORIGINAl - NOISY IMAGE
diffg = ( Id -Isp).^2; MSe1= sum(sum(diffg))/ (r*c); fprintf('\n\nORIGINAL IMAGE AND NOISE ADDED IMAGE'); fprintf('\nThe Mse value for salt and pepper noise is: %.5f', MSe1);
diffg = ( Id -Ig).^2; MSe2= sum(sum(diffg))/ (r*c); fprintf('\nThe Mse value for Gaussian noise is: %.5f\n\n', MSe2);
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
57
8.4 APPENDIX D
Appendix C shows the Matlab code for an unsharp mask filter.
clc; clear;
% Read image.
I = imread('C:\Users\32112635\Documents\MATLAB\Thesis Ariesa\clock.tiff');
[r c] = size (I)
%convert image to double precesion Id = im2double (I);
%--------smoothes by 3x3 mean filter mean = ones (3,3)/(3*3) smooth = imfilter (Id,mean);
edge= Id- smooth;
k = input ('enter the scaling constant,k:'); sharp1 = Id + k*(edge);
figure(1) imshow(smooth); title ('Blurred Image');
figure (2) subplot(1,2,1); imshow(edge); title('Edge Detection'); subplot (1,2,2) imshow(sharp1); title('Sharpened Image')
%-------------------MSE-------------------- % 1nd try % noisy image - filterd image % salt and pepper image
diffs1 = ( Id- sharp1).^2; mse1= sum(sum(diffs1))/ (r*c); fprintf('\n\nSharpened Image without noise'); fprintf('\nThe Mse value is: %.5f', mse1);
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
58
8.5 APPENDIX E
Result for an unsharp mask filter.
LINEAR AND NONLINEAR IMAGE PROCESSING FILTER
59