magnetic resonance image segmentation engineering project report for b.sc. degree
TRANSCRIPT
MAGNETIC RESONANCE IMAGE
SEGMENTATION
by
Mehmet Serdar TEKE
Engineering Project Report
Yeditepe University
Faculty of Engineering and Architecture
Department of Electrical and Electronics Engineering
Istanbul, 2012
ii
MAGNETIC RESONANCE IMAGE
SEGMENTATION
APPROVED BY:
Assoc. Prof. Dr. Cem ÜNSALAN ……………………
(Supervisor)
Prof. Dr. Canbolat UÇAK ……………………
Assoc. Prof. Dr. Cahit CANBAY …………………….
Assist. Prof. Dr. Esin ÖZTÜRK IŞIK …………………….
DATE OF APPROVAL: 09.01.2012
iii
TABLE OF CONTENTS
ACKNOWLEDGEMENTS ……………………………………………………………. vi
ABSTRACT ……………………………………………………………………............. vii
ÖZET …………………………………………………………………………….............viii
LIST OF FIGURES …………………………………………………………………….. ix
LIST OF SYMBOLS/ABBREVIATIONS …………………………………………….. xii
1. INTRODUCTION …………………………………………………………………….. 1
2. MAGNETIC RESONANCE IMAGING BASICS ……………………………... 2
2.1. Description of the Magnetic Resonance Imaging System ……………………... 2
2.2. The Working Principles of a Magnetic Resonance Imaging System ……………... 5
2.2.1. Main Magnetic Field …………………………………………………………….. 5
2.2.2. Radio-frequency Field …………………………………………………………….. 6
2.2.2.1. Longitudinal Relaxation …………………………………………………….. 6
2.2.2.2. Transverse Relaxation …………………………………………………….. 8
2.2.3. Linear Gradient Fields …………………………………………………………….. 10
2.3. Techniques Used in MR Imaging Systems …………………………………….. 11
2.3.1. Slice Selection …………………………………………………………………….. 11
2.3.2. Frequency Encoding …………………………………………………………….. 12
2.3.3. Phase Encoding …………………………………………………………….. 13
2.3.4. k-Space Formation …………………………………………………………….. 14
2.3.5. Data Acquisition …………………………………………………………….. 15
3. PREFILTERING PHASE BEFORE SEGMENTATION ……………………………... 18
iv
3.1. Bilateral Filtering …………………………………………………………….. 18
3.2. Median Filtering …………………………………………………………………….. 22
4. IMAGE SEGMENTATION …………………………………………………….. 26
4.1. Thresholding …………………………………………………………………….. 26
4.2. Threshold Detection Methods …………………………………………………….. 27
4.3. Multi-thresholding …………………………………………………………….. 30
5. COMBINATION OF PREFILTERING AND SEGMENTATION FOR FINAL
DESIGN ……………………………………………………………………………. 33
5.1. Graphical User Interface …………………………………………………………….. 33
5.2. Tests Applied to Different MR Images using Prepared GUI ……………………... 34
5.2.1. Tests on T1 Contrasted MR Image of Brain …………………………………….. 34
5.2.1.1. Bilateral Filtering …………………………………………………………….. 35
5.2.1.2. Median Filtering …………………………………………………………….. 39
5.2.2. Tests on MRA Image …………………………………………………………….. 40
5.2.2.1. Bilateral Filtering …………………………………………………………….. 40
5.2.2.2. Median Filtering …………………………………………………………….. 41
6. CONCLUSION …………………………………………………………………….. 43
REFERENCES …………………………………………………………………….. 44
APPENDIX A: THE SOURCE CODE FOR THE EFFECT OF WIDTH CHANGE
ON GAUSSIAN DISTRIBUTION IN MATLAB …………………………………….. 46
APPENDIX B: THE SOURCE CODE FOR BILATERAL FILTERING IN
MATLAB ……………………………………………………………………………. 47
APPENDIX C: THE SOURCE CODE FOR COMPARING HISTOGRAM SHAPES
OF ORIGINAL IMAGE AND BILATERAL-FILTERED IMAGE IN MATLAB……... 49
v
APPENDIX D: THE SOURCE CODE FOR MEDIAN FILTERING AND
HISTOGRAM COMPARISON BETWEEN ORIGINAL IMAGES AND
MEDIAN-FILTERED IMAGES IN MATLAB …………………………………….. 50
APPENDIX E: THE SOURCE CODE FOR SIMPLE THRESHOLDING IN
MATLAB ……………………………………………………………………………. 51
APPENDIX F: THE SOURCE CODE FOR IMAGE SEGMENTATION WITH
DIFFERENT THRESHOLD VALUES DEPENDING ON ITS HISTOGRAM IN
MATLAB ……………………………………………………………………………. 52
APPENDIX G: THE SOURCE CODE FOR OPTIMUM THRESHOLDING USING
THE IMAGE HISTOGRAM …………………………………………………………….. 54
APPENDIX H: THE SOURCE CODE FOR MULTI-THRESHOLDING IN
MATLAB ……………………………………………………………………………. 55
vi
ACKNOWLEDGEMENTS
Associate Professor Cem ÜNSALAN, who is my supervisor for this project,
showed me the ways to follow for reaching the purpose of this project. Without his
tremendous efforts to help me, this project would not have been materialized. Also,
Assistant Professor Esin ÖZTÜRK IŞIK from Biomedical Engineering helped me
understand important concepts of magnetic resonance imaging needed for the background
of this project.
vii
ABSTRACT
In this project, segmentation of various regions from an MR image showing the
anatomy of human body is executed. The problem to segment these regions is that intensity
values of different tissues in an MR image get through each other at region boundaries.
Therefore, two types of image filtering techniques which are bilateral and median filtering
are applied before segmentation procedure. Multi-thresholding is used as a segmentation
method after filtering procedure. Image eroding is executed to get rid of irrelevant regions
usually appearing as little dots after segmentation takes place. As a result, multi-
thresholding process after bilateral filtering is applied makes a slightly better job to
differentiate regions in an MR image than after median filtering is applied. However,
median filtering should also be useful before segmentation process as it is computationally
faster than bilateral filtering.
viii
ÖZET
Bu projede, MR imgelerindeki çeşitli bölgelerin bölütleme işlemi kullanılarak
birbirlerinden ayrılması hedeflenmiştir. MR imgelerindeki farklı dokular farklı seviyedeki
gri tonlarına sahip olduklarından bu işlem uygulanabilmektedir. Ancak, bu dokuların gri
tonları kesiştikleri bölümlerde birbirlerine çok yaklaştıklarından bölütleme işleminden
önce MR imgesinin filtrelenmesi gerekmektedir. Medyan ve çift yönlü imge filtreleme
yöntemleri bu amaç için kullanılmıştır. Bu filtrelerden herhangi biri uygulandıktan sonra
bölütleme işlemi filtrelenmiş imgenin üstünde denenmiştir. Bölütleme yöntemi olarak
çoklu eşik uygulaması kullanılmıştır. Bu işlemin sonunda elde edilen imgeye, istenen
dokuyla alakası olmayan ve genellikle küçük noktalar şeklinde beliren noktaları ortadan
kaldırmak için imge aşındırma işlemi uygulanmıştır. Sonuç olarak, çoklu eşik
uygulamasından önce kullanılan çift yönlü filtrenin medyan filtreden biraz daha iyi bir
sonuç verdiği ortaya çıkmıştır. Ancak, medyan filtre de bu uygulama için kullanışlı olabilir
çünkü bilgisayar ortamında çift yönlü filtreden daha hızlı bir sonuç vermektedir.
ix
LIST OF FIGURES
Figure 1. The magnetic field created in a solenoid ……………………………... 3
Figure 2. Gradient coils in the magnetic resonance imaging system ……………... 3
Figure 3. The whole magnetic resonance imaging system ……………………... 4
Figure 4. Longitudinal relaxation …………………………………………….. 7
Figure 5. Longitudinal relaxation plots for fat and blood ……………………... 8
Figure 6. Transverse relaxation …………………………………………………….. 9
Figure 7. Transverse relaxation plots for fat and blood ……………………... 9
Figure 8. Selective excitation technique …………………………………….. 12
Figure 9. Phase encoding and frequency encoding gradient waveforms ……... 13
Figure 10. Representation for the 2D Fourier transformation of k-space ……... 14
Figure 11. The relationship among the gradient field, the spatial frequency, and the
duration of the gradient …………………………………………….. 15
Figure 12. Gradient waveform, the corresponding change in spatial frequency, and
the echo obtained at the end of the process ……………………………... 16
Figure 13. Total magnet field composed of the static field B0 and the gradient
fraction …………………………………………………………….. 17
Figure 14. Effect of width on Gaussian distribution ……………………………... 19
Figure 15. Original MR Image and its output image after applying bilateral filter….. 20
Figure 16. Histogram shape analysis for a T2 weighted brain MR image for bilateral
filtering …………………………………………………………….. 21
Figure 17. Histogram shape analysis for a T1 weighted brain MR image for bilateral
filtering …………………………………………………………….. 22
x
Figure 18. Median filtering edge preserving property ……………………………... 22
Figure 19. Original MR Image and its output image after applying median filter……23
Figure 20. Histogram shape analysis for a T2 weighted brain MR image for median
filtering …………………………………………………………….. 24
Figure 21. Histogram shape analysis for a T1 weighted brain MR image for median
filtering …………………………………………………………….. 24
Figure 22. Simple thresholding where (a) is the original T2 contrasted MR image,
(b) is the gray-matter and cerebrospinal fluid segmentation of the
original MR image, (c) is the white-matter segmentation of the original
MR image …………………………………………………………….. 27
Figure 23. Bimodal histogram example …………………………………………….. 28
Figure 24. Image segmentation using different threshold values ……………... 29
Figure 25. Zoomed histogram in Figure 15 between the intensity values 60 and 189
showing the lowest-frequency intensity with a line at intensity 150……... 30
Figure 26. Optimum thresholding using the image histogram ……………………... 30
Figure 27. A T1 contrasted brain MR image and its histogram distribution ……... 31
Figure 28. T1 contrasted MR image of brain and its histogram with thresholding
intensity levels …………………………………………………….. 32
Figure 29. Multi-thresholding of T1 contrasted MR image of brain ……………... 32
Figure 30. GUI prepared for final design …………………………………….. 33
Figure 31. Selection of test image …………………………………………….. 34
Figure 32. Selected image shown in GUI with its histogram ……………………... 35
Figure 33. Bilateral filtered image and its histogram in GUI ……………………... 35
Figure 34. CSF segmentation from T1 contrasted MR image of brain before and
xi
after filtering …………………………………………………………….. 36
Figure 35. Gray matter segmentation from T1 contrasted MR image of brain before
and after filtering …………………………………………………….. 36
Figure 36 White matter segmentation from T1 contrasted MR image of brain before
and after filtering …………………………………………………….. 37
Figure 37. Final CSF extraction after applying erosion ……………………………... 37
Figure 38. Final gray matter extraction after applying erosion ……………………... 38
Figure 39. Final white matter extraction after applying erosion ……………………... 38
Figure 40. CSF extraction from T1 contrasted MR image of brain before and after
median filtering …………………………………………………….. 39
Figure 41. Gray matter extraction from T1 contrasted MR image of brain before
and after median filtering …………………………………………….. 39
Figure 42. White matter extraction from T1 contrasted MR image of brain before
and after median filtering …………………………………………….. 40
Figure 43. Vessel extraction from MRA image before and after bilateral filtering….. 41
Figure 44. Vessel extraction from MRA image before and after median filtering…... 42
xii
LIST OF SYMBOLS/ABBREVIATIONS
B Magnetic Field Strength
dl Differential Length
µ Permeability
I Electrical Current
ω Angular Resonance Frequency
f Resonance Frequency
γ Gyromagnetic Ratio
Mz Magnetic Moment along Direction z
M0 Initial Magnetic Moment
t Time
T1 Longitudinal Relaxation Time
T2 Transverse Relaxation Time
Gx Gradient Field Strength along Direction x
B0 Main Magnetic Field Strength
Bt Total Magnetic Field Strength
GRO Readout Gradient Field
GPE Phase Encoding Gradient Field
kRO Spatial Frequency in Frequency Encoding Direction
kPE Spatial Frequency in Phase Encoding Direction
xiii
MR Magnetic Resonance
RF Radio Frequency
NMR Nuclear Magnetic Resonance
MATLAB Software Development Package of the Mathworks Inc. Standing for Matrix
Laboratory
CSF Cerebrospinal Fluid
GUI Graphical User Interface
MRA Magnetic Resonance Angiography
1
1. INTRODUCTION
In this project, it is aimed to extract certain areas from various MR images. For
instance, MR image of a brain consists of three major parts which are white matter, gray
matter, and cerebrospinal fluid. Each of these regions has a different range of intensities.
However, their intensity ranges get through each other at the boundaries of the image.
Therefore, some filtering is needed to differentiate these regions.
Two types of filters are used to considerably differentiate regions included in an
MR image. These filters are bilateral filter and median filter which are to be explained in
the upcoming parts of this report. The reason why these two filters are used before
segmenting regions from an MR image is that they smooth image while preserving edges.
Smoothing makes intensity values of the same regions in an image closer to each other and
preserving edges makes intensity values at the region boundaries in an image
differentiable. After applying one of these filters, segmentation process takes place. For the
segmentation process, histogram dependent multi-thresholding technique is used. This
method is to be explained in more details in the upcoming parts of this report. Erosion
process takes place after segmentation to get rid of some irrelevant regions appearing as
little dots. Final image showing extracted region is obtained after erosion process.
As MR images showing human anatomy are input images for this project, some
background information is to be given at the beginning of this report for the understanding
of why different anatomical regions have different intensity values in an MR image. Also,
techniques used to fill an MR image up are to be explained in this background.
2
2. MAGNETIC RESONANCE IMAGING BASICS
2.1. Description of the Magnetic Resonance Imaging System
Magnetic resonance imaging is a powerful noninvasive medical imaging modality
that uses a large magnetic field, gradient coils and radio-frequency coils [2].
The large magnetic field is created by a large electric current depending on the
Ampere’s law which states that for any closed loop path, the sum of length elements times
the magnetic field in the direction of length element is equal to the permeability times the
electric current enclosed in the loop [1].
(1)
As it is seen in Equation 1, the magnitude of the magnetic field, that is B, is directly
proportional to the amount of the current passing through the wire that is I. In this
equation, µ is the permeability of the medium which is constant and dl is the differential
length of the wire [1].
The large magnet that surrounds the whole MR device might be considered as a
large solenoid which creates a constant magnetic field inside itself. In Figure 1, shape of a
solenoid is illustrated.
3
Figure 1. The magnetic field created in a solenoid [1]
Gradient coils in the magnetic resonance imaging system are used to localize the
signal coming from body. Since main magnet produces a magnetic field in only one
direction inside itself as seen in Figure 1, gradient coils are necessary to construct an image
of body. There are three gradient coils in the magnetic resonance imaging system as the
world is a three-dimensional environment. Gradient coils must work in harmony to obtain
the image. For instance, one of three gradient coils is used to select the slice of body and
the other two gradient coils are used to fill the image up by using detected signals [2].
Figure 2. Gradient coils in the magnetic resonance imaging system [11]
4
When we take a look at Figure 2, it is seen that configuration of the gradient coils is
such that each gradient coils can create a magnetic field along one direction. Moreover,
they are named depending on the direction along which they produce the magnetic field.
For example, Z coil in this figure creates a magnetic field along z-direction when it is
active [2].
Radio-frequency coils are used to excite the protons which are hydrogen atoms in
body. Since the body is mostly composed by water which has the chemical formula of
H2O, hydrogen is the most abundant element in body. That is why magnetic resonance
imaging can be applied in clinical applications. The excitation of protons is to be explained
in details in the upcoming section of the report [2].
Figure 3. The whole magnetic resonance imaging system [12]
Figure 3 shows all components in the magnetic resonance imaging system. The
most important three parts of this system; which are magnet, gradient coils, and RF coil;
5
have already been explained. The other parts are used to process the signal coming from
body.
2.2. The Working Principles of a Magnetic Resonance Imaging System
Magnetic resonance imaging is based on the principles of nuclear magnetic
resonance that is a spectroscopic technique used by scientists to obtain microscopic
chemical and physical information about molecules. In the magnetic resonance imaging
system, the water molecule or simply the hydrogen element that consists of only one
proton is utilized according to nuclear magnetic resonance [3].
Atoms that have an odd number of protons possess a nuclear spin angular
momentum, and therefore exhibit MR phenomenon according to NMR physics. These
protons can be visualized as spinning charged spheres that give rise to a small magnetic
moment because of the fact that moving point charges, such as protons and electrons,
produce complicated but well known magnetic fields that depend on the charge, velocity,
and acceleration of particles. Since hydrogen, with a single proton, is the most abundant in
biological specimens, hydrogen based NMR is mostly used as the magnetic resonance
imaging technique [2].
2.2.1. Main Magnetic Field
The main magnetic field is created by a large solenoid discussed in the section 2.1.
In the absence of an external magnetic field, the spins are oriented randomly and the net
macroscopic magnetic moment is zero. However, in the presence of an external magnetic
field created by the large solenoid, magnetic moment vectors of spins tend to align in the
direction of magnetic field creating a net magnetic moment and the nuclear spins exhibit
resonance at a frequency which is called as the Larmor frequency [2].
6
(2)
(3)
In Equation 2, ω is the Larmor frequency in radians, γ is the gyromagnetic ratio,
that is a known constant unique for each type of atom, and B is the applied magnetic field
in the large solenoid. In Equation 3, f is the Larmor frequency in Hertz. According to both
equations, Larmor frequency is directly proportional to applied magnetic field. The Larmor
frequency is an important concept to excite the protons. The RF signal sent from the RF
coil must be at the Larmor frequency to be able to image protons and therefore body [2].
2.2.2. Radio-frequency Field
RF pulse in the magnetic resonance imaging system is to excite protons in the
biologic specimen. It is turned on for a short time and then turned off. After it is turned off,
relaxation process starts to begin.
2.2.2.1. Longitudinal Relaxation
Before applying the radio-frequency field, protons have a precession movement and
align in the z-direction due to main magnetic field. When a 900 of RF pulse is applied to
those protons, they start to lose magnitude in z-direction and gets closer to x-y plane as
time passes. After enough time passes, they reach x-y plane and rotate on x-y plane with
zero magnitude along z-direction since the net magnetic moment is rotated by 900 using RF
pulse. After RF pulse is removed, net magnetic moment of protons starts to rise on the z-
direction. This procedure is described as longitudinal relaxation or T1 relaxation. The
reason why it is called longitudinal relaxation is that the longitudinal axis is used for z-axis
[2].
7
(4)
The Equation 4 is obtained by using the Bloch equation, which is used to create
mathematical models for the signal acquired through the magnetic resonance imaging
processes, for a 900 of RF pulse. The Bloch equation is also given in Equation 5 [2].
(5)
Figure 4. Longitudinal relaxation [13]
In Figure 4, movement of net magnetic moment is illustrated on the left hand side
of figure after turning off RF pulse. In other words, process of longitudinal relaxation is
schematized on the left side of this figure. On the right hand side of Figure 4, change in net
magnetic moment vector on z-axis is plotted versus time according to Equation 4.
Each tissue of human body has a different T1 which enables us to image body. In
order to explain this concept, an example might be useful. T1 of fat is 180ms while T1 of
blood is 800ms. According to these values, fat has a greater magnetic moment on z-axis
than blood which means that, at a specific time for both tissues, fat has a greater intensity
value on z-axis than blood. Thus, in T1 contrasted MR image, fat is brighter than blood. To
be able to increase the intensity difference between fat and blood for T1 relaxation, signal
8
should be acquired when their difference is at its highest value. Figure 5 shows the T1
relaxation plots for both tissues and their difference [5].
Figure 5. Longitudinal relaxation plots for fat and blood [5]
2.2.2.2. Transverse Relaxation
Before applying radio-frequency field, protons have a precession movement and
align in z-direction due to main magnetic field. When a 900 of RF pulse is applied to those
protons, they start to lose magnitude in z-direction and gets closer to x-y plane as time
passes. After enough time passes, they reach x-y plane and rotate on x-y plane with zero
magnitude along z-direction since net magnetic moment is rotated by 900 using RF pulse.
After RF pulse is removed, the net magnetic moment of protons starts to rise on the z-
direction. Thus, net magnetic moment of protons gets smaller on the x-y plane. This
procedure is described as transverse relaxation or T2 relaxation. The reason why it is called
transverse relaxation is that x-y plane is known as transverse plane [2].
(6)
The Equation 6 is also obtained by using the Bloch equation, which is given in the
Equation 5.
9
Figure 6. Transverse relaxation [13]
In Figure 6, movement of net magnetic moment is illustrated on the left hand side
of figure after turning off RF pulse. In other words, process of transverse relaxation is
schematized on the left side of this figure. On the right hand side of Figure 6, change in net
magnetic moment vector on x-y plane is plotted versus time according to Equation 6.
Each tissue of human body has a different T2 which enables us to image body. In
order to explain this concept, an example might be useful. T2 of fat is 90ms while T2 of
blood is 180ms. According to these values, fat has a smaller magnetic moment on x-y
plane than blood which means that, at a specific time for both tissues, fat has a lower
intensity value on x-y plane than blood. Thus, in T2 contrasted MR image, fat is darker
than blood. To be able to increase the intensity difference between fat and blood for T2
relaxation, signal should be acquired when their difference is at its highest value. Figure 7
shows the T2 relaxation plots for both tissues and their difference [5].
Figure 7. Transverse relaxation plots for fat and blood [5]
10
2.2.3. Linear Gradient Fields
If applied magnetic field is only main static field in z-direction, then all spins
possess the same resonance frequency that is the Larmor frequency shown in Equation 2.
When excited by RF coils, they behave like oscillators inducing signals at that frequency.
As the excitation RF coil encompasses entire region of interest, it is not possible to excite a
selected portion of volume nor is possible to distinguish the signals generated from
different spatial locations if only main magnetic field exists [2].
In magnetic resonance imaging, spatial localization is achieved by applying linear
gradient fields in addition to main magnetic field that is B0. For instance, if a gradient field
is applied in x-direction that is Gx, then applied field becomes as in Equation 7 where Bt is
total magnetic field applied in the system [2].
(7)
It should be noted that this gradient field leaves combined magnetic field still
pointing in the z-direction but now the field strength varies with x-location. Thus, Gx can
be defined as in Equation 8 where Bz is magnetic field applied along z-direction [2].
(8)
Thus, frequency of spins becomes a function of x-location as given in Equation 9
where ω0 is the Larmor frequency equal to γB0.
(9)
11
Since oscillation frequency is a function of x-location, spins in one x-location are
excited when RF pulse is applied as it is needed to match the Larmor frequency to excite
spins. Therefore, localization of signal acquired is succeeded in the process [2].
The idea of creating an inhomogeneous field with a gradient on purpose plays a
vital role in MR imaging. By making magnetic field inhomogeneous, resonant frequency
of spins changes instantaneously when magnetic field strength changes enabling us to
localize incoming signal [2].
2.3. Techniques Used in MR Imaging Systems
2.3.1. Slice Selection
According to Larmor equation given in Equation 2, excitation frequency is directly
proportional to applied magnetic field. This can be utilized to selectively excite distinct
parts of body. A gradient field in z-direction, Gz, causes a linearly varying magnetic field
along z-axis and a defined slice can be excited using an RF pulse of certain frequency
width shown in Figure 8 [2].
By either increasing gradient strength or decreasing RF bandwidth, slice thickness
can be reduced. As gradient strength is increased, slope of changing magnetic field is
increased causing the reduction in slice thickness. Similarly, decreasing RF bandwidth
causes slice thickness to become smaller as seen in Figure 8 [4].
In magnetic resonance imaging, slice selection allows for a two dimensional
imaging by making slice as small as possible. As thickness of slice becomes smaller, error
in two-dimensional image gets smaller. To make error zero, slice must be at a unique point
on z-axis, not in a width on it, which is possible with an infinite gradient field which is not
possible.
12
Figure 8. Selective excitation technique [4]
2.3.2. Frequency Encoding
After applying slice selection, encoding of spatial information is to be applied in
two dimensions. This is succeeded by magnetic field gradients in respective directions.
These are differentiated by time of gradient switching before or during data acquisition. In
frequency encoding, a readout gradient, GRO, is turned on during data acquisition. Thus,
gradient direction is called the readout direction [4].
GRO produces an additional and linearly varying magnetic field. Because of the
proportionality between magnetic field and frequency depending on the Larmor equation,
the latter also changes linearly. Therefore, spins at different positions emit radiation with
different frequencies which can be distinguished using Fourier transform. Each frequency
is related to a specific position on readout direction. Also, the intensity of radiation with
this frequency is proportional to the number of spins at corresponding position [4].
13
2.3.3. Phase Encoding
Phase encoding gradient, GPE, is turned on and off before data acquisition in order
to set spatial-dependent phase of spins and kPE, that is the spatial frequency in phase
encoding direction. For N points in PE direction, it is needed to have exactly N values of
kPE obtained by stepwise modification of GPE that is shown on the left hand side of Figure
9. As a result, N acquisition cycles are needed with GPE modified by a constant ΔGPE per
time step as shown in Figure 9. By using a constant gradient duration τ, kPE changes by a
value ΔkPE in each data acquisition cycle. GRO amplitude during data acquisition is the
same for entire MR imaging process. However, a continuous variation of kRO is realized by
acquiring the data points at different time points of i*Δt, where i changes from 1 to N.
Figure 9 also explains this concept. Then, all values of kRO are measured within a single
acquisition cycle. Therefore, the total imaging time is mainly defined by the number of
phase encoding steps. Considering k-space, full row filling during each cycle is achieved
which implies that the row number is defined by the number of phase encoding steps [4].
Slice selection, frequency encoding, and phase encoding are used together to build
an MR image. Each one is applied using one of gradient coils as there are three gradients
due to three-dimensional environment of the world. In summary, slice selection procedure
is applied at the beginning. Then, phase encoding and frequency encoding processes are
applied together to form k-space which is used to obtain the image by inverse Fourier
transform.
Figure 9. Phase encoding and frequency encoding gradient waveforms [4]
14
2.3.4. k-Space Formation
k-space is a two-dimensional space for the raw data matrix in MR imaging. These
data can be converted into an image using Fourier transform illustrated in Figure 10.
Imaging is associated with two reciprocal parameters which are position and spatial
frequency. An MR image is a position-dependent intensity distribution obtained using slice
selection, frequency encoding, and phase encoding. Therefore, it corresponds to the
position domain. Accordingly, the variable k describes the other domain of the Fourier pair
and it is the spatial frequency [4].
Figure 10. Representation for the 2D Fourier transformation of k-space [4]
The value kx is the number of phase cycles per meter distance from origin through
where a magnetization vector passes because of application of gradient, Gx. Thus, kx has
the unit of cycles per meter similar to the unit of frequency that is cycles per second. That
is why k is called the spatial frequency [4].
Phase changes of a magnetization vector depend on gradient magnetic field strength
and its duration. Therefore, kx is directly proportional to gradient strength and its duration
as illustrated in Figure 11 [4].
15
Figure 11. The relationship among the gradient field, the spatial frequency, and the
duration of the gradient [4]
2.3.5. Data Acquisition
The acquisition procedure is applied during positive readout gradient switching as
seen at the bottom of Figure 12. The raw data signal which is called an echo is shown in a
box at the bottom Figure 12. On the top of Figure 12, the variable kRO is drawn as a
function of time. As it is seen in this figure, kRO changes from minimum to maximum
within the acquisition interval between the time points A and B due to Equations 10 and 11
which imply that the area under gradient waveform is directly proportional to the spatial
frequency [4].
(10)
(11)
The negative start value at A is produced by turning on a negative gradient before
the acquisition. The change in magnetization vector produced by readout gradient is
different for different positions on the readout axis. Thus, it produces a net de-phasing of
the initial magnetization vector. Subsequently, turning on a positive gradient reverses these
16
phase manipulations until the initial phases are recovered by making areas of negative and
positive gradients equal to each other. At this point, kRO is equal to zero and the echo is at
its maximum. By continuing to apply the readout gradient at positive direction, it again
produces a net de-phasing and a continuously decreasing signal as an echo. The whole
process is illustrated in figure 12 [4].
Figure 12. Gradient waveform, the corresponding change in spatial frequency, and the
echo obtained at the end of the process [4]
The meaning of positive and negative gradients is illustrated in Figure 13 which
shows total magnetic field composed of the static field B0 and the gradient field fraction.
When gradient field is turned on, an additional magnetic field is applied in B0 direction
with its amplitude linearly changing along the gradient direction. The origin in this figure
is generally set as the center of object imaged using MR.
Examining Figure 13, increasing line shows magnetic field strength of positive
gradient addition to main magnetic field and decreasing line shows magnetic field strength
of negative gradient addition to main magnetic field. For positive gradient application, a
17
magnetic field anti-parallel to B0 is overlaid when x<0 and a magnet field parallel to B0 is
overlaid when x>0. For negative gradient application, a magnetic field parallel to B0 is
overlaid when x<0 and a magnetic field anti-parallel to B0 is overlaid when x>0. Thus, the
magnetization vector is static at x=0 while neighboring spins on both sides are moving
symmetrically but in different directions on x-y plane [4].
Figure 13. Total magnet field composed of the static field B0 and the gradient fraction [4]
18
3. PREFILTERING PHASE BEFORE SEGMENTATION
Filtering is the most fundamental operation of image processing and computer
vision. Basically, filtering means that the value of filtered image at a given location is a
function of the values of the input image in a small neighborhood of the same location. For
example, Gaussian low-pass filtering computes a weighted average of pixel values in the
neighborhood, in which the weights decrease with distance from the neighborhood center,
smoothing the input image [6].
3.1. Bilateral Filtering
Bilateral filtering applies smoothing on images while it preserves edges in input
image. To make both smoothing and edge preserving, a bilateral filter needs to be
nonlinear. It combines gray levels or colors depending on both their geometric closeness
and their photometric similarity and also prefers near value to distant values in both
domain and range. This process of bilateral filtering makes itself nonlinear [6].
The bilateral filter is a normalized convolution where weighting for each pixel y is
determined by the spatial distance from the center x, as wells as its relative intensity. The
spatial and intensity weighting functions f and g are typically Gaussian distributions. These
functions are multiplied together to produce the weighting for each pixel. For input image
I, output image O, and neighborhood window N, the output of bilateral filtering is defined
in Equation 12 [7].
(12)
19
By applying Gaussian distribution function for both f and g functions in Equation
12, Equation 13 is obtained, where C is the normalization constant, σd and σr are
parameters controlling the fall-off of weights in spatial and intensity domains, respectively
[8].
(13)
To understand the effect of fall-off weights, Gaussian function should be examined.
In mathematics, a Gaussian function has the form shown in Equation 14 in which x is the
variable, µ is the center, and σ is the controlling parameter for the width of the bell.
(14)
Effect of σ on Gaussian function is illustrated on Figure 14 which is created on
MATLAB and whose source code is given in appendix A. As seen in Figure 14, the width
is increased by σ.
Figure 14. Effect of width on Gaussian distribution
20
By examining back the bilateral filtering function given in Equation 13, σr controls
edge detection process of the bilateral filter. This is because the difference between
intensities of x and y gets very large on edges. Therefore, σr executes edge preserving
function of bilateral filter. Since bilateral filter computes a weighted sum of neighboring
pixels, it still executes smoothing for small intensity differences. This is because, for
similar intensity values, second part of Equation 13 becomes ineffective since I(x)-I(y) gets
equal to almost zero and σr has no effect on the filter.
Due to the fact that bilateral filters apply smoothing on images while preserving
edges, it is commonly used before segmentation procedures take place.
Figure 15 shows a brain MR image and its output obtained by applying bilateral
filter. The source code for this procedure in MATLAB is given at the end of the report in
appendix B with its corresponding function.
Figure 15. Original MR Image and its output image after applying bilateral filter
As it is seen in Figure 15, neighbor pixels having similar intensity values are
smoothed and neighbor pixels having large intensity value differences keep their
differences preserving edges.
21
It is needed to see the difference between the histograms of original image and
bilateral-filtered image. To be able to see this difference, a MATLAB code is written.
Figure 16 and Figure 17 shows this for two images and their bilateral-filtered versions.
Also, the source code for this operation in MATLAB is added to the report in appendix C.
As it seen in Figures 16 and 17, peaks are amplified after applying bilateral filters
to both images, that is the result of edge preserving for high intensity differences and
smoothing for small intensity differences due to the application of bilateral filter.
For histogram based segmentation, the change in histogram shape after applying
bilateral filter is significantly important since it makes our job easier for determining
threshold values. Also, the rate of segmentation error is reduced after applying bilateral
filter to original image because of the same reason. In the upcoming of this report,
differences between segmented original image and segmented bilateral-filtered image are
to be examined after trying another image filtering process which is the median filter.
Figure 16. Histogram shape analysis for a T2 weighted brain MR image for bilateral
filtering
22
Figure 17. Histogram shape analysis for a T1 weighted brain MR image for bilateral
filtering
3.2. Median Filtering
Median filtering is also a type of filter which smoothes the image while preserving
edges. As it calculates the median of neighboring pixels’ intensities, not the mean, it
preserves edges unlike mean filters which calculates the mean of neighboring pixels’
intensities not considering large intensity differences between the central pixel and its
neighboring pixels. However, median filter takes large intensity differences between
central pixel and neighboring pixels into consideration to preserve edges. To examine this
property of median filter, an example is useful.
Figure 18. Median filtering edge preserving property
23
Considering Figure 18, median filter’s edge preserving property is to be explained.
As seen in this figure, large intensity differences between pixels on the right of edge and
on the left of edge. While calculating the median, all numbers are sorted from the smallest
to the largest. For this example, intensities of pixels are sorted as 24-43-47-120-123-124-
125-125-127. The middle number in this sorting, which is 123, is to be new intensity value
for central pixel after applying median filter. If mean filter is applied to this window, new
intensity value for central pixel becomes 95 which is the average of all values. Therefore,
mean filter still makes smoothing at edges while median filter preserves edges and still
does the smoothing for small intensity differences.
In MATLAB, median filtering is applied to the image on which bilateral filtering is
also applied in section 3.1. The result is shown in Figure 19. Also, histogram differences
between original images and median-filtered images are shown in Figures 20 and 21. The
source code for these operations is added to the report in appendix D.
Figure 19. Original MR Image and its output image after applying median filter
24
Figure 20. Histogram shape analysis for a T2 weighted brain MR image for median
filtering
Figure 21. Histogram shape analysis for a T1 weighted brain MR image for median
filtering
25
Examining Figure 19, edges are preserved and smoothing is also applied for small
intensity differences. However, it is seen in that bilateral filter is more effective in this
operation than median filter comparing Figures 15 and 19. In histogram shape analysis,
this is better seen while comparing Figures 17 and 21. In this comparison, it is clearly seen
that, peaks are much more amplified and edge-neighboring pixels are much more
suppressed after applying bilateral filter in the comparison of Figures 17 and 21.
26
4. IMAGE SEGMENTATION
Image segmentation is basically an image processing algorithm which subdivides
an image into its constituent regions or objects. The level of detail to which the subdivision
is carried depends on the problem being solved. Thus, segmentation should stop when the
objects or regions of interest in an application have been detected [10].
4.1. Thresholding
Gray-level thresholding is the simplest segmentation process. Many objects or
image regions are characterized by constant reflectivity or light absorption of their
surfaces. Therefore, a brightness constant or threshold can be determined to segment
objects and background. Thresholding is computationally inexpensive and fast. It is the
oldest image segmentation method and is still widely used in simple applications [9].
Complete segmentation of an image might result from thresholding in simple
scenes. Thresholding is the transformation of an input image f to an output, or segmented,
binary image g as seen in Equation 15 where T is the threshold and i,j together represent
one pixel. According to Equation 15, all pixels in the input image are covered for
segmentation [9].
(15)
Examining Equation 15, g(i,j) = 1 for image elements of objects and g(i,j) = 0 for
image elements of background, or vice versa. As the resulting image is a binary image, it
has only black, for binary 0, and white, for binary 1, regions helping us strictly divide the
image into its constituents [9].
27
Figure 22 shows a simple thresholding of a brain MR image implemented using
MATLAB, software for engineering design. In Figure 22(a), original MR image is
visualized. In Figure 22(b), gray-matter and cerebrospinal fluid segmentation of original
image is executed and, in Figure 22(c), white-matter segmentation of original image is
succeeded. Note that the same threshold values are used in both segmentations. The source
code of this program in MATLAB is added to the report in appendix E.
Figure 22. Simple thresholding where (a) is the original T2 contrasted MR image, (b) is the
gray-matter and cerebrospinal fluid segmentation of the original MR image, (c) is the
white-matter segmentation of the original MR image
4.2. Threshold Detection Methods
Useful methods of threshold detection are based on histogram shape analysis. If an
image consists of objects of approximately the same gray-level that differs from the gray
level of background, then resulting histogram is bi-modal. Pixels of objects form one of its
peaks as pixels of background form the second peak. In Figure 23, a typical example is
shown, which is obtained using MATLAB. The chosen threshold must meet minimum
segmentation error requirements. It makes intuitive sense to determine the threshold as the
gray-level that has minimum histogram value between the two mentioned maxima [9].
28
Figure 23. Bimodal histogram example
To the image whose histogram is obtained using MATLAB in Figure 23 where
there are two maxima at intensities of around 40 and 255, different threshold values are
applied for segmentation. The result obtained is shown in Figure 24. As seen in Figure 24,
results obtained with threshold values of 50 and 200 are not good due to segmentation
errors. When the histogram of this image is analyzed using Figure 23, it is realized that
maximum points are at the intensities of about 40 and 250. Thus, these threshold values
lead to high segmentation errors. A threshold value between 100 and 175 should be ideal to
minimize the segmentation error depending on image histogram as illustrated in Figure 24
with the intensities of 100 and 150. The source code for obtaining Figures 23 and 24 is
added to the report in appendix F.
29
Figure 24. Image segmentation using different threshold values
To find the optimum threshold, which results in minimum segmentation error for
the corresponding finger-print image, intensity value that has minimum-frequency value
between two peaks is to be chosen as the threshold for the segmentation. Figure 25 is used
for this purpose. The histogram shown in Figure 23 is taken and zooming procedure on it is
performed between the intensity values of 66 and 178. Using the zoomed histogram, the
lowest-frequency intensity is determined as 150 as shown in Figure 25. Applying threshold
of 150 on MRA image for segmentation, the result in Figure 26 is obtained in which the
original image is also shown. The source code for obtaining this figure in MATLAB is
added to the report in appendix G.
30
Figure 25. Zoomed histogram in Figure 15 between the intensity values 60 and 189
showing the lowest-frequency intensity with a line at intensity 150
Figure 26. Optimum thresholding using the image histogram
4.3. Multi-thresholding
In the histogram of an image, there might be more than two peaks corresponding to
different regions. A T1 contrasted MR image might be an example for this situation.
31
Actually, three regions are included in a brain MR image. They are CSF standing for
cerebrospinal fluid, white matter, and gray matter. The histogram of a T1 contrasted MR
image is illustrated in Figure 27 where corresponding intensity levels for each tissue are
shown.
Figure 27. A T1 contrasted brain MR image and its histogram distribution
32
Two thresholds are needed to divide this kind of image into its constituent regions.
The smallest intensity levels between two peaks are chosen for minimum segmentation
error. This job is executed in MATLAB. Figure 28 shows T1 contrasted MR image and its
histogram. In Figure 29, segmented images with multi-thresholding are shown. The source
code for obtaining Figures 28 and 29 in MATLAB is added to the report in appendix H.
Figure 28. T1 contrasted MR image of brain and its histogram with thresholding intensity
levels
Figure 29. Multi-thresholding of T1 contrasted MR image of brain
33
5. COMBINATION OF PREFILTERING AND SEGMENTATION FOR
FINAL DESIGN
To be able to see the benefit of filtering before segmentation, segmentation is
needed to be executed after filtering. For this purpose, a GUI, graphical user interface, is
made using MATLAB.
5.1. Graphical User Interface
In the graphical user interface prepared, there are four major parts. The first one is
to load a sample image. Load image button is used for this purpose. This button shows the
sample image and its histogram after selecting sample image. The second part is for the
pre-filtering procedure There are two filtering processes which are bilateral filtering and
median filtering in this part. Parameters of these filtering processes are to be entered by the
user. The third part is for segmentation. There are four parameters which are smaller
threshold values and larger threshold values for original image and filtered image. These
four threshold values are to be used for multi-thresholding. The fourth and the last part is
for erosion process to get rid of the unwanted regions after segmentation applied. Complete
GUI is shown in Figure 30.
Figure 30. GUI prepared for final design
34
5.2. Tests Applied to Different MR Images using Prepared GUI
5.2.1. Tests on T1 Contrasted MR Image of Brain
T1 contrasted MR image of brain used in previous parts of the project is also used in
GUI prepared for final design.
Figure 31. Selection of test image
After pushing load image button in GUI, window in Figure 31 appears to select a
sample image. In this window, T1 contrasted MR image of brain is selected to apply the
experiments.
After selecting T1 contrasted MR image of brain from this menu, selected image is
shown in GUI with its histogram as seen in Figure 32. After this procedure, one of the
filters from pre-filtering menu is to be chosen. Therefore, two parts of this experiment
exist.
35
Figure 32. Selected image shown in GUI with its histogram
5.2.1.1. Bilateral Filtering
Before clicking on bilateral filtering button, bilateral filtering parameters should be
chosen. These parameters are explained in Section 3.1. After entering these parameters,
bilateral filtering button is to be clicked on for bilateral filtering procedure. After clicking
on this button, filtered image and its histogram are shown in GUI as seen in Figure 33.
Figure 33. Bilateral filtered image and its histogram in GUI
36
After bilateral filtering is applied to test image for pre-filtering, segmentation
procedure takes place. Two threshold values for original and filtered images are entered for
multi-thresholding depending on image histograms. After they are entered, multi-
thresholding button is pushed for segmentation. For different threshold values, results are
shown in Figures 34, 35, and 36.
Figure 34. CSF segmentation from T1 contrasted MR image of brain before and after
filtering
Figure 35. Gray matter segmentation from T1 contrasted MR image of brain before and
after filtering
37
Figure 36. White matter segmentation from T1 contrasted MR image of brain before and
after filtering
After segmentation is applied, erosion procedure takes place to remove irrelevant
parts, usually occurring as dots, from the segmented image. After erosion process is
applied, final results obtained are shown in Figures 37, 38, and 39.
Figure 37. Final CSF extraction after applying erosion
38
Figure 38. Final gray matter extraction after applying erosion
Figure 39. Final white matter extraction after applying erosion
39
5.2.1.2. Median Filtering
After entering median filtering parameters and median filtering is applied as the
pre-filtering process, multi-thresholding is applied for different threshold values depending
on image histograms. Then, erosion procedure is applied. Results obtained are shown in
Figures 40, 41, and 42.
Figure 40. CSF extraction from T1 contrasted MR image of brain before and after median
filtering
Figure 41. Gray matter extraction from T1 contrasted MR image of brain before and after
median filtering
40
Figure 42. White matter extraction from T1 contrasted MR image of brain before and after
median filtering
5.2.2. Tests on MRA Image
MRA image used in previous parts of the project is also used in GUI prepared for
final design.
5.2.2.1. Bilateral Filtering
After bilateral filtering is applied to test image for pre-filtering, segmentation
procedure takes place. Two threshold values for original and filtered image are entered for
multi-thresholding depending on image histogram. After they are entered, multi-
thresholding button is pushed for segmentation. Then, erosion procedure is applied. Results
obtained are shown in Figure 43.
41
Figure 43. Vessel extraction from MRA image before and after bilateral filtering
5.2.2.2. Median Filtering
After median filtering is applied to test image for pre-filtering, segmentation
procedure takes place. Two threshold values for original and filtered image are entered for
multi-thresholding depending on image histogram. After they are entered, multi-
thresholding button is pushed for segmentation. Then, erosion procedure is applied. Results
obtained are shown in Figure 44.
42
Figure 44. Vessel extraction from MRA image before and after median filtering
43
6. CONCLUSION
Before beginning this project, it was targeted to segment various regions from an
MR image with an acceptable rate of error. To reduce error of segmentation, bilateral and
median filters are applied as pre-filtering procedures before segmenting relevant regions
from MR image. After applying both filters, segmentation and erosion procedures take
place to obtain final segmented image.
Filtering affects segmentation procedure in a good manner. When filtered and non-
filtered images are segmented, resultant images show that there are unwanted segmented
parts for non-filtered images. These parts are usually seen as dots in irrelevant regions.
These dots are considerably reduced after applying both filters.
Both filters change the histogram of MR images making regions included in it more
differentiable for multi-thresholding, which is used as a segmentation technique in this
project, than unfiltered image. Therefore, threshold values of multi-thresholding are
determined in such a way that segmentation error especially at boundaries of regions is
reduced.
By comparing results obtained using bilateral filter to results obtained using median
filter, it is observed that bilateral filtered and segmented MR image has less irrelevant
regions than median filtered and segmented image meaning that bilateral filtering is
preferable before segmentation. However, median filtering makes a good job before
segmentation, too. As it is computationally faster than bilateral filtering, it might be useful
in applications where fast image processing is needed.
Median filtering causes some relevant regions not to be included in segmented
images while bilateral filtering makes a better job of preserving relevant parts in
segmented image than median filtering. However, difference between the results obtained
after both filters is not too much as both filters are good enough for segmentation process.
44
REFERENCES
[1] Nave, C. R., Electricity and Magnetism, Ampere’s Law, http://hyperphysics.phy-
astr.gsu.edu/hbase/hframe.html.
[2] Nishimura, D. G., Principles of Magnetic Resonance Imaging, Stanford University,
2010.
[3] Flögel, U. and Jacoby, C., Nuclear Magnetic Resonance, http://www.nmr.uni-
duesseldorf.de/sets/theory.html.
[4] Flögel, U. and Jacoby, C., MR Imaging, http://www.nmr.uni-
duesseldorf.de/sets/theory2.html.
[5] Flögel, U. and Jacoby, C., Relaxation, http://www.nmr.uni-
duesseldorf.de/sets/theory3.html.
[6] Tomasi, C. and Manduchi R., Bilateral Filtering for Gray and Color Images,
Proceedings of the IEEE International Conference on Computer Vision, pp. 839-
846, http://www.cs.duke.edu/~tomasi/papers/tomasi/tomasiIccv98.pdf, January
1998.
[7] Weiss, B., Fast Median and Bilateral Filtering, Proc. SIGGRAPH,
http://www.shellandslate.com/download/fastmedian_5506.pdf, 2006.
[8] Zhang, M., Bilateral Filter in Image Processing Master Thesis,
etd.lsu.edu/docs/available/etd-05132009-120913/unrestricted/Zhang_thesis.pdf,
August 2009.
[9] Sonka, M., Hlavac, V., and Boyle, R., Image Processing, Analysis, and Machine
Vision 3rd
Edition, Cengage-Engineering, 2007.
[10] Gonzalez, R.C. and Woods, R.E., Digital Image Processing 3rd
Edition, Pearson
Prentice Hall, New Jersey, 2008.
[11] Internet source whose link is http://www.ovaltech.ca/philyexp.html
[12] Internet source whose link is http://www.rell.com/pages/Product-Group-
Category.aspx?applicationId=197
45
[13] Internet source whose link is http://www.med.univ-
rennes1.fr/cerf/edicerf/BASES/BA004_cv_rb_9.html
46
APPENDIX A: THE SOURCE CODE FOR THE EFFECT OF WIDTH
CHANGE ON GAUSSIAN DISTRIBUTION IN MATLAB
%% THE SOURCE CODE FOR THE EFFECT OF WIDTH CHANGE ON GAUSSIAN
DISTRIBUTION
%% IN MATLAB
x=-5:0.01:5; %set variable range
sigma=[0.2,0.4,0.8,1.6,3.2]; %set width of the bell
center=0; %set center point
y1=10.*exp(-((x-center).^2)/(2.*((sigma(1)).^2)));
y2=10.*exp(-((x-center).^2)/(2.*((sigma(2)).^2)));
y3=10.*exp(-((x-center).^2)/(2.*((sigma(3)).^2)));
y4=10.*exp(-((x-center).^2)/(2.*((sigma(4)).^2)));
y5=10.*exp(-((x-center).^2)/(2.*((sigma(5)).^2)));
figure;
plot(x,y1,'r');
hold on;
plot(x,y2,'b');
hold on;
plot(x,y3,'g');
hold on;
plot(x,y4,'m');
hold on;
plot(x,y5,'k');
title('Effect of Width Change on Gaussian Distribution');
xlabel('x');
ylabel('f(x)');
sigma_legend=legend('sigma=0.2','sigma=0.4','sigma=0.8','sigma=1.6','sigma=3.2');
set(sigma_legend,'Location','NorthWest');
47
APPENDIX B: THE SOURCE CODE FOR BILATERAL FILTERING
IN MATLAB
B.1. Function Written on MATLAB for Bilateral Filtering
% bilateral_filter2 applies two dimensional bilateral filtering using
% the method outlined in:
% B = bilateral_filter2(A,W,SIGMA) performs 2-D bilateral filtering
% to the image 'A'. 'A' should be a double precision matrix of size
% NxMx1 (size NxMx1 for grayscale image) with normalized values in
% the closed interval [0,1]. The half-size of the Gaussian bilateral
% filter window is defined by W. The standard deviations of the
% bilateral filter are given by SIGMA, where the spatial-domain
% standard deviation is given by SIGMA(1) and the intensity-domain
% standard deviation is given by SIGMA(2).
%% Pre-process input and select appropriate filter.
function B = bilateral_filter2(A,w,sigma)
%% Apply bilateral filtering.
%% Pre-compute Gaussian distance weights.
[X,Y] = meshgrid(-w:w,-w:w);
G = exp(-(X.^2+Y.^2)/(2*sigma(1)^2));
% Create waitbar.
h = waitbar(0,'Applying bilateral filter...');
set(h,'Name','Bilateral Filter Progress');
%% Apply bilateral filter.
dim = size(A);
B = zeros(dim);
for i = 1:dim(1)
for j = 1:dim(2)
% Extract local region.
iMin = max(i-w,1);
iMax = min(i+w,dim(1));
jMin = max(j-w,1);
jMax = min(j+w,dim(2));
I = A(iMin:iMax,jMin:jMax);
% Compute Gaussian intensity weights.
H = exp(-(I-A(i,j)).^2/(2*sigma(2)^2));
% Calculate bilateral filter response.
F = H.*G((iMin:iMax)-i+w+1,(jMin:jMax)-j+w+1);
B(i,j) = sum(F(:).*I(:))/sum(F(:));
48
end
waitbar(i/dim(1));
end
% Close waitbar.
close(h);
B.2. Main Function Written on MATLAB for Bilateral Filtering
% Main_Program makes the use of 'bilateral_filter2' function.
% Bilateral filtering is applied by the function 'bilateral_filter2',
% that is written for 2-dimensional bilateral filtering in the second
% part of this source code
%% Reading test images
% Note that they must be double precision in the interval [0,1].
img1 = double(imread('proje3_test.jpg'))/255;
img1 = rgb2gray(img1);
%% BILATERAL FILTERING
%% Introduce AWGN into test images.
% Note that this will show the benefit of bilateral filtering.
img1 = img1+0.03*randn(size(img1));
img1(img1<0) = 0; img1(img1>1) = 1;
%% Set bilateral filter parameters.
w = 5; % bilateral filter half-width
sigma = [3 0.1]; % bilateral filter standard deviations
%% Apply bilateral filter to the image.
bflt_img1 = bilateral_filter2(img1,w,sigma);
%% Display grayscale input image and filtered output.
figure; clf;
set(gcf,'Name','Grayscale Bilateral Filtering Results');
subplot(1,2,1);
imagesc(img1);
axis image; colormap gray;
title('Input Image');
subplot(1,2,2);
imagesc(bflt_img1);
axis image; colormap gray;
title('Result of Bilateral Filtering');
drawnow;
49
APPENDIX C: THE SOURCE CODE FOR COMPARING
HISTOGRAM SHAPES OF ORIGINAL IMAGE AND BILATERAL-
FILTERED IMAGE IN MATLAB
%% THE SOURCE CODE FOR COMPARING HISTOGRAM SHAPES OF ORIGINAL
IMAGE AND
%% BILATERAL-FILTERED IMAGE IN MATLAB
%% IMAGE READING
img1=imread('proje1_test.jpg');
img2=imread('proje3_test.jpg');
img3=imread('proje1_test_result.jpg');
img4=imread('proje3_test_result.jpg');
img1=rgb2gray(img1);
img2=rgb2gray(img2);
img3=rgb2gray(img3);
img4=rgb2gray(img4);
%% PLOTTING THE HISTOGRAM
figure;
subplot(2,2,1);imshow(img1,'DisplayRange',[]);title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,2);imhist(img1,256);title('HISTOGRAM OF ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,3);imshow(img3,'DisplayRange',[]);title('BILATERAL-FILTERED
IMAGE',...
'FontWeight','bold');
subplot(2,2,4);imhist(img3,256);title('HISTOGRAM OF BILATERAL-FILTERED
IMAGE',...
'FontWeight','bold');
figure;
subplot(2,2,1);imshow(img2,'DisplayRange',[]);title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,2);imhist(img2,256);title('HISTOGRAM OF ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,3);imshow(img4,'DisplayRange',[]);title('BILATERAL-FILTERED
IMAGE',...
'FontWeight','bold');
subplot(2,2,4);imhist(img4,256);title('HISTOGRAM OF BILATERAL-FILTERED
IMAGE',...
'FontWeight','bold');
50
APPENDIX D: THE SOURCE CODE FOR MEDIAN FILTERING
AND HISTOGRAM COMPARISON BETWEEN ORIGINAL IMAGES
AND MEDIAN-FILTERED IMAGES IN MATLAB
%% THE SOURCE CODE FOR MEDIAN FILTERING AND HISTOGRAM
COMPARISON
%% BETWEEN ORIGINAL IMAGES AND MEDIAN-FILTERED IMAGES IN
MATLAB
%% IMAGE READING
img1=imread('proje1_test.jpg');
img2=imread('proje3_test.jpg');
img1=rgb2gray(img1);
img2=rgb2gray(img2);
%% MEDIAN FILTERING
img1_median=medfilt2(img1);
img2_median=medfilt2(img2);
%% SHOWING ORIGINAL AND MEDIAN-FILTERED IMAGES
figure;
subplot(1,2,1);imshow(img2,'DisplayRange',[]);title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(1,2,2);imshow(img2_median,'DisplayRange',[]);title('MEDIAN-FILTERED
IMAGE',...
'FontWeight','bold');
%% HISTOGRAM COMPARISON
figure;
subplot(2,2,1);imshow(img1,'DisplayRange',[]);title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,2);imhist(img1);title('HISTOGRAM OF ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,3);imshow(img1_median,'DisplayRange',[]);title('MEDIAN-FILTERED
IMAGE',...
'FontWeight','bold');
subplot(2,2,4);imhist(img1_median);title('HISTOGRAM OF MEDIAN-FILTERED
IMAGE',...
'FontWeight','bold');
figure;
subplot(2,2,1);imshow(img2,'DisplayRange',[]);title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,2);imhist(img2);title('HISTOGRAM OF ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,3);imshow(img2_median,'DisplayRange',[]);title('MEDIAN-FILTERED
IMAGE',...
'FontWeight','bold');
subplot(2,2,4);imhist(img2_median);title('HISTOGRAM OF MEDIAN-FILTERED
IMAGE',...
'FontWeight','bold');
51
APPENDIX E: THE SOURCE CODE FOR SIMPLE THRESHOLDING
IN MATLAB
%% THE SOURCE CODE FOR SIMPLE THRESHOLDING IN MATLAB
%% IMAGE READING
img=imread('proje1_test.jpg ');
img=rgb2gray(img);
%% WHITE-MATTER SEGMENTATION BY SIMPLE THRESHOLDING
img1=img;
[m n]=size(img1);
for i=1:m;
for j=1:n;
if img1(i,j)<=80;
img1(i,j)=0;
else
img1(i,j)=255;
end
end
end
%% GRAY-MATTER SEGMENTATION BY SIMPLE THRESHOLDING
img2=img;
[m n]=size(img2);
for i=1:m;
for j=1:n;
if img2(i,j)>80;
img2(i,j)=0;
else
img2(i,j)=255;
end
end
end
%% PLOTTING THE ORIGINAL IMAGE AND SEGMENTED IMAGES
figure;
subplot(1,3,1);imshow(img,'DisplayRange',[]);
title('(a)',...
'FontWeight','bold');
subplot(1,3,2);imshow(img1,'DisplayRange',[]);
title('(b)',...
'FontWeight','bold');
subplot(1,3,3);imshow(img2,'DisplayRange',[]);
title('(c)',...
'FontWeight','bold');
52
APPENDIX F: THE SOURCE CODE FOR IMAGE SEGMENTATION
WITH DIFFERENT THRESHOLD VALUES DEPENDING ON ITS
HISTOGRAM IN MATLAB
%% THE SOURCE CODE FOR IMAGE SEGMENTATION
%% WITH DIFFERENT THRESHOLD VALUES
%% DEPENDING ON ITS HISTOGRAM IN MATLAB
%% IMAGE READING
img=imread('proje2_test.jpg');
img=rgb2gray(img);
%% PLOTTING THE HISTOGRAM
figure;
subplot(1,2,1);imshow(img,'DisplayRange',[]);
title('Original Image',...
'FontWeight','bold');
subplot(1,2,2);imhist(img,256);
title('Histogram of Original Image',...
'FontWeight','bold');
ylabel('Frequency');
%% THRESHOLDING USING HISTOGRAM
img1=img;
[m n]=size(img1);
for i=1:m;
for j=1:n;
if img1(i,j)<=50;
img1(i,j)=0;
else
img1(i,j)=255;
end
end
end
img2=img;
[m n]=size(img2);
for i=1:m;
for j=1:n;
if img2(i,j)<=100;
img2(i,j)=0;
else
img2(i,j)=255;
end
end
end
img3=img;
[m n]=size(img3);
for i=1:m;
for j=1:n;
if img3(i,j)<=150;
img3(i,j)=0;
53
else
img3(i,j)=255;
end
end
end
img4=img;
[m n]=size(img4);
for i=1:m;
for j=1:n;
if img4(i,j)<=200;
img4(i,j)=0;
else
img4(i,j)=255;
end
end
end
%% PLOTTING THE SEGMENTED IMAGES
figure;
subplot(2,2,1);imshow(img1,'DisplayRange',[]);
title('Thresholded with 50',...
'FontWeight','bold');
subplot(2,2,2);imshow(img2,'DisplayRange',[]);
title('Thresholded with 100',...
'FontWeight','bold');
subplot(2,2,3);imshow(img3,'DisplayRange',[]);
title('Thresholded with 150',...
'FontWeight','bold');
subplot(2,2,4);imshow(img4,'DisplayRange',[]);
title('Thresholded with 200',...
'FontWeight','bold');
54
APPENDIX G: THE SOURCE CODE FOR OPTIMUM
THRESHOLDING USING THE IMAGE HISTOGRAM
%% THE SOURCE CODE FOR OPTIMUM THRESHOLDING USING THE IMAGE
HISTOGRAM
%% IMAGE READING
img=imread('proje2_test.jpg');
img=rgb2gray(img);
%% SEGMENTATION WITH OPTIMUM THESHOLD
img1=img;
[m n]=size(img1);
for i=1:m;
for j=1:n;
if img1(i,j)<=150;
img1(i,j)=0;
else
img1(i,j)=255;
end
end
end
%% PLOTTING THE ORIGINAL IMAGE AND SEGMENTED IMAGE
figure;
subplot(1,2,1);imshow(img,'DisplayRange',[]);
title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(1,2,2);imshow(img1,'DisplayRange',[]);
title('SEGMENTED IMAGE WITH OPTIMUM THRESHOLD',...
'FontWeight','bold');
55
APPENDIX H: THE SOURCE CODE FOR MULTI-THRESHOLDING
IN MATLAB
%% THE SOURCE CODE FOR MULTI-THRESHOLDING IN MATLAB
%% IMAGE READING
img=imread('proje4_test.jpg ');
img=rgb2gray(img);
%% PLOTTING THE HISTOGRAM
figure;
subplot(1,2,1);imshow(img,'DisplayRange',[]);
title('Original Image',...
'FontWeight','bold');
subplot(1,2,2);
imhist(img,256);
title('Histogram of Original Image',...
'FontWeight','bold');
ylabel('Frequency');
%% MULTITHRESHOLDING
img1=img;
[m n]=size(img1);
for i=1:m;
for j=1:n;
if img1(i,j)<=120;
if img1(i,j)>41;
img1(i,j)=255;
end
else
img1(i,j)=0;
end
end
end
img2=img;
[m n]=size(img2);
for i=1:m;
for j=1:n;
if img2(i,j)<=41;
if img2(i,j)>0;
img2(i,j)=255;
end
else
img2(i,j)=0;
end
end
end
img3=img;
[m n]=size(img3);
for i=1:m;
for j=1:n;
56
if img3(i,j)>120;
img3(i,j)=255;
else
img3(i,j)=0;
end
end
end
%% PLOTTING THE ORIGINAL IMAGE AND SEGMENTED IMAGES
figure;
subplot(2,2,1);imshow(img,'DisplayRange',[]);
title('ORIGINAL IMAGE',...
'FontWeight','bold');
subplot(2,2,2);imshow(img1,'DisplayRange',[]);
title('GRAY MATTER SEGMENTATION',...
'FontWeight','bold');
subplot(2,2,3);imshow(img2,'DisplayRange',[]);
title('CSF SEGMENTATION',...
'FontWeight','bold');
subplot(2,2,4);imshow(img3,'DisplayRange',[]);
title('WHITE MATTER SEGMENTATION',...
'FontWeight','bold');