Previewpdf
Previewpdf
Ravishankar Chityala
IonPath and University of California Santa Cruz
Sridevi Pudipeddi
University of California, Berkeley
MATLAB R is a trademark of The MathWorks, Inc. and is used with permission. The MathWorks
does not warrant the accuracy of the text or exercises in this book. This book’s use or discus-
sion of MATLAB R software or related products does not constitute endorsement or sponsorship
by The MathWorks of a particular pedagogical approach or particular use of the MATLAB R
software.
Reasonable efforts have been made to publish reliable data and information, but the author and
publisher cannot assume responsibility for the validity of all materials or the consequences of their
use. The authors and publishers have attempted to trace the copyright holders of all material
reproduced in this publication and apologize to copyright holders if permission to publish in this
form has not been obtained. If any copyright material has not been acknowledged please write
and let us know so we may rectify in any future reprint.
Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, repro-
duced, transmitted, or utilized in any form by any electronic, mechanical, or other means, now
known or hereafter invented, including photocopying, microfilming, and recording, or in any infor-
mation storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, access www.copyright
.com or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA
01923, 978-750-8400. For works that are not available on CCC please contact mpkbookspermis-
sions@tandf.co.uk
Foreword xvii
Preface xix
Introduction xxv
Authors xxvii
1 Introduction to Python 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 What Is Python? . . . . . . . . . . . . . . . . . . . . . 3
1.3 Python Environments . . . . . . . . . . . . . . . . . . 5
1.3.1 Python Interpreter . . . . . . . . . . . . . . . . 5
1.3.2 Anaconda Python Distribution . . . . . . . . . 6
1.4 Running a Python Program . . . . . . . . . . . . . . . 6
1.5 Basic Python Statements and Data Types . . . . . . . 7
1.5.1 Data Structures . . . . . . . . . . . . . . . . . . 11
1.5.2 File Handling . . . . . . . . . . . . . . . . . . . 16
1.5.3 User-Defined Functions . . . . . . . . . . . . . . 18
1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 19
1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 20
vii
viii Contents
4 Spatial Filters 61
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 61
4.2 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . 62
4.2.1 Mean Filter . . . . . . . . . . . . . . . . . . . . 64
4.2.2 Median Filter . . . . . . . . . . . . . . . . . . . 68
4.2.3 Max Filter . . . . . . . . . . . . . . . . . . . . . 70
4.2.4 Min Filter . . . . . . . . . . . . . . . . . . . . . 72
4.3 Edge Detection using Derivatives . . . . . . . . . . . . 73
4.3.1 First Derivative Filters . . . . . . . . . . . . . . 74
4.3.1.1 Sobel Filter . . . . . . . . . . . . . . . 76
4.3.1.2 Prewitt Filter . . . . . . . . . . . . . . 78
4.3.1.3 Canny Filter . . . . . . . . . . . . . . 80
4.3.2 Second Derivative Filters . . . . . . . . . . . . . 82
4.3.2.1 Laplacian Filter . . . . . . . . . . . . . 83
4.3.2.2 Laplacian of Gaussian Filter . . . . . . 86
4.4 Shape Detecting Filter . . . . . . . . . . . . . . . . . . 89
4.4.1 Frangi Filter . . . . . . . . . . . . . . . . . . . . 89
4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 91
4.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 92
5 Image Enhancement 95
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 95
5.2 Pixel Transformation . . . . . . . . . . . . . . . . . . . 95
5.3 Image Inverse . . . . . . . . . . . . . . . . . . . . . . . 97
5.4 Power Law Transformation . . . . . . . . . . . . . . . 99
5.5 Log Transformation . . . . . . . . . . . . . . . . . . . 102
5.6 Histogram Equalization . . . . . . . . . . . . . . . . . 104
x Contents
8 Segmentation 167
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 167
8.2 Histogram-Based Segmentation . . . . . . . . . . . . . 168
8.2.1 Otsu’s Method . . . . . . . . . . . . . . . . . . 168
8.2.2 Renyi Entropy . . . . . . . . . . . . . . . . . . . 172
8.2.3 Adaptive Thresholding . . . . . . . . . . . . . . 176
8.3 Region-Based Segmentation . . . . . . . . . . . . . . . 179
8.3.1 Watershed Segmentation . . . . . . . . . . . . . 181
8.4 Contour-Based Segmentation . . . . . . . . . . . . . . 187
8.4.1 Chan-Vese Segmentation . . . . . . . . . . . . . 187
8.5 Segmentation Algorithm for Various Modalities . . . . 191
8.5.1 Segmentation of Computed Tomography Image 192
8.5.2 Segmentation of MRI Image . . . . . . . . . . . 192
8.5.3 Segmentation of Optical and Electron
Microscope Images . . . . . . . . . . . . . . . . 192
8.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 193
8.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 193
Bibliography 403
Index 417
Foreword
I first met one of the authors, Dr. Ravishankar (Ravi) Chityala, in 2006
when he was a PhD student at the Toshiba Stroke Research Center,
SUNY-Buffalo. Ravi’s PhD work in medical imaging was fruitful and
influential, and I have been following his post-PhD career ever since.
In reading this book, I was impressed by the fact that, despite Ravi’s
current focus on computing and visualization, his knowledge of medical
imaging has only deepened and expanded, which has enabled him, along
with his co-author, Dr. Sridevi Pudipeddi, to write a very competent
treatment of the subject of medical imaging. Thus, it is a pleasure for
me to write a foreword to this very good book.
This is a book that every imaging scientist should have on his
or her desk because image acquisition and processing is becoming a
standard method for qualifying and quantifying experimental measure-
ments. Moreover, I believe students and researchers need a course or a
book to learn both image acquisition and image processing using a sin-
gle source, and this book, as a well-rounded introduction to both topics,
serves that purpose very well. The topics treated are complex, but the
authors have done a great job of covering the most commonly used
image acquisition modalities, such as x-ray and computed tomography,
magnetic resonance imaging, and microscopes, concisely and effectively,
providing a handy compendium of the most useful information.
As Confucius said, “I see and I remember, I do and I understand;”
this book aims to provide hands-on learning that enables the reader to
understand the concepts explained in the book by means of applying
the various examples written in the Python code. But do not be dis-
couraged if you have never used Python or any other script language
xvii
xviii Foreword
Alexander Zamyatin
Distinguished Scientist
Toshiba Medical Research Institute USA, Inc.
Vernon Hills, Illinois
Preface
We received feedback from people who bought the first edition of the
book and also from experts in the topic while working on the second
edition of the book.
We added three new chapters and one new appendix. When the
first edition was written, machine learning (ML) and deep learning
(DL) were not yet mainstream. Today, problems that cannot be solved
by using traditional image processing and computer vision techniques
are being solved using ML and DL. So we added one chapter on neural
network and another chapter on convolutional neural network (CNN).
In these two chapters, we discuss the mathematical underpinnings of
these two networks. We also discuss solving these two networks using
Keras, a ML / DL library.
We also added a new chapter on affine transformation, a geometric
transformation that preserves lines. We also added an appendix on par-
allel computing using joblib, a Python module that allows distributing
tasks to multiple Python process that can run on multiple cores on a
given computer.
We added new algorithms to existing chapters and also improved
the explanation of the code. Some of the new algorithms introduced
are Frangi filter, Contrast Limited Adaptive Histogram Equaliza-
tion (CLAHE), Local contrast normalization, Chan-Vese segmentation,
Gray scale morphology etc.
When the first edition was written, we used Python 2.7 for testing
the code. As of January 2020, Python 2.7 is no longer supported. So we
modified the code for the latest version of Python 3. We also modified
the code for the latest version of numpy, scipy, scikit and OpenCV.
We hope you enjoy learning from the book.
xix
xx Preface
xxi
xxii Preface to the First Edition
Audience
The book is intended primarily for advanced undergraduate and
graduate students in applied mathematics, scientific computing, med-
ical imaging, cell biology, bioengineering, computer vision, computer
science, engineering and related fields, as well as to engineers, profes-
sionals from academia, and the industry. The book can be used as a
textbook for an advanced undergraduate or graduate course, a summer
seminar course, or can be used for self-learning. It serves as a self-
contained handbook and provides an overview of the relevant image
acquisition techniques and corresponding image processing. The book
also contains practice exercises and tips that students can use to remem-
ber key information.
Acknowledgments
We are extremely thankful to students, colleagues, and friends who
gave valuable input during the process of writing this book. We are
thankful to the Minnesota Supercomputing Institute (MSI) at the Uni-
versity of Minnesota. At MSI, Ravi Chityala had discussions with stu-
dents, staff and faculty on image processing. These discussions helped
him recognize the need for a textbook that combines both image pro-
cessing and acquisition.
We want to specially thank Dr. Nicholas Labello, University of
Chicago; Dr. Wei Zhang, University of Minnesota; Dr. Guillermo Mar-
ques, University Imaging Center, University of Minnesota; Dr. Greg
Metzger, University of Minnesota; Mr. William Hellriegel, University of
Minnesota; Dr. Andrew Gustafson, University of Minnesota; Mr. Abhi-
jeet More, Amazon; Mr. Arun Balaji; and Mr. Karthik Bharathwaj for
proofreading the manuscript and for providing feedback.
Preface to the First Edition xxiii
xxv
xxvi Introduction
P
summation
θ angle
|x| absolute value of x
e 2.718281
∗ convolution
log logarithm base 10
morphological erosion
⊕ morphological dilation
◦ morphological open
• morphological close
∪ union
λ wavelength
E energy
h Planck’s constant
c speed of light
µ attentuation coefficient
γ gyromagnetic ratio
NA numerical aperture
ν frequency
dx differential
∇ gradient
∂
∂x derivative along x-axis
∇2
R =∆ Laplacian
integration
CDF cumulative distribution function
CT computed tomography
DICOM digital imaging and communication in medicine
JPEG joint photographic experts group
MRI magnetic resonance imaging
PET positron emission tomography
PNG portable network graphics
PSF point spread function
RGB red, green, blue channels
TIFF tagged image file format
xxix
Part I
1
Chapter 1
Introduction to Python
1.1 Introduction
Before we begin discussion on image acquisition and processing
using Python, we will provide an overview of the various aspects of
Python. This chapter focuses on some of the basic materials covered
by many other books [Bea09], [Het10], [Lut06], [Vai09] and also from
the book, “Essential Python” ([PC18]), a book from the authors of this
book. If you are already familiar with Python and are currently using
it, then you can skip this chapter.
We begin with an introduction to Python. We will then discuss
the installation of Python with all the modules using the Anaconda
distribution. Once the installation has been completed, we can begin
exploring the various features of Python. We will quickly review the
various data structures such as list, dictionary, and tuples and state-
ments such as for-loop, if-else, iterators and list comprehension.
3
4 Image Processing and Acquisition using Python
1. It is free.
2. Indentation of the code is not optional. This makes the code read-
able. However, a code with multiple loops and other constructs
will be indented to the right, making it difficult to read the code.
The >> is the terminal prompt and >>> represents the Python
prompt.
The best approach to running Python programs under any oper-
ating systems is to use an Integrated Development Environment like
IDLE or Spyder as it provides an ability to edit the file and also run it
under the same interface.
Indentation
In Python, a code block is indicated by indentation. For example in
the code below, we first print a message, ‘We are computing squares of
numbers between 0 and 9’. Then we loop through values in the range
of 0 to 9 and store it in the variable ‘i’ and also print the square of ‘i’.
Finally we print the message, ‘We completed the task ...’ at the end.
In other languages, the code block under the for-loop would be
identified with a pair of curly braces {}. However, in Python we do
not use curly braces. The code block is identified by moving the line
print(i*i) four spaces to the right. You can also choose to use a tab
instead.
Comments
Comments are an important part of any programming language. In
Python, a single line comment is denoted by a hash # at the beginning
of a line. Multiple lines can be commented by using triple quoted strings
(triple single quotes or triple double quotes) at the beginning and at
the end of the block.
'''
This is
a multiline
comment
'''
Variables
Python is a dynamic language and hence you do not need to specify
the variable type as in C/C++. Variables can be imagined as contain-
ers of values. The values can be an integer, float, string, lists, tuples,
dictionary, set, etc.
>>> a = 1
>>> a = 10.0
>>> a = 'hello'
Introduction to Python 9
In the above example the integer value of 1, float value of 10.0, and
a string value of hello for all cases are stored in the same variable.
However, only the last assigned value is the current value for a.
Operators
Python supports all the common arithmetic operators such as
+, −, ∗, /. It also supports the common comparison operators such
as >, <, ==, ! =, >=, <=, etc. In addition, through various modules
Python provides many operators for performing trigonometric, mathe-
matical, geometric operations, etc.
Loops
The most common looping construct in Python is the for-loop
statement, which allows iterating through the collection of objects. Here
is an example:
In the above example the output of the for-loop are the numbers
from 1 to 5. The range function allows us to create values starting from
1 and ending with 5. Such a concept is similar to the for-loop normally
found in C/C++ or most programming languages.
The real power of for-loop lies in its ability to iterate through
other Python objects such as lists, dictionaries, sets, strings, etc. We
will discuss these Python objects in more detail subsequently.
>>> a = {
'lang':'python'
'ver': '3.6.6'
}
>>> for keys in a:
... print(a[key])
if a<10:
print('a is less than 10')
elif a<20:
print('a is between 10 and 20')
else:
print('a is greater than 20')
>>> d = [ ]
>>> if d:
... print('d is not empty')
... else:
... print('d is empty')
d is empty
Introduction to Python 11
In the above code, since d is empty, the else clause is true and we
enter the else block and print d is empty.
The real power of Python lies in the liberal usage of its data struc-
ture. The common criticism of Python is that it is slow compared to
C/C++. This is especially true if multiple for-loops are used in pro-
gramming Python. This can be alleviated by appropriate use of data
structures such as lists, tuples, dictionary and sets. We describe each
of these data structures in this section.
Lists
Lists are similar to arrays in C/C++. But, unlike arrays in C/C++,
lists in Python can hold objects of any type such as int, float, string and
including another list. Lists are mutable, as their size can be changed
by adding or removing elements. The following examples will help show
the power and flexibility of lists.
In the first line, a new list is created. This list contains two strings
and one floating-point number. In the second line, we use the pop func-
tion to remove the last element (index = −1). The popped element
is printed to the terminal. After the pop, the list contains only two
elements instead of the original three. We use append, and insert a
new element, “numpy” to the end of the list. Finally, in the next two
commands we print the value of the list in index 0 and the last posi-
tion indicated by using “−1” as the index. In the last command, we
introduce slicing and obtain a new list that contains only the first two
values of the list. This indicates that one can operate on the list using
methods such as pop, insert, or remove and also using operators such
as slicing.
A list may contain another list. Here is an example. We will consider
the case of a list containing four numbers and arranged to look like a
matrix.
>>> a = [[1,2],[3,4]]
>>> print(a[0])
[1,2]
>>> print(a[1])
[3,4]
>>> print(a[0][0])
1
In line 1, we define a list of the list. The values [1, 2] are in the
first list and the values [3, 4] are in the second list. The two lists are
combined to form a 2D list. In the second line, we print the value of
the first element of the list. Note that this prints the first row or the
first list and not just the first cell. In the fourth line, we print the value
of the second row or the second list. To obtain the value of the first
element in the first list, we need to index the list as given in line 6. As
you can see, indexing the various elements of the list is as simple as
calling the location of the element in the list.
Introduction to Python 13
>>> a = ['python','scipy','numpy']
>>> a.sort()
>>> a
['numpy','python','scipy']
List comprehensions
A list comprehension allows building a list from another list. Let us
consider the case where we need to generate a list of squares of numbers
from 0 to 9. We will begin by generating a list of numbers from 0 to 9.
Then we will determine the square of each element.
>>> a = list(range(10))
>>> print(a)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> b = [x*x for x in a]
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> b = []
>>> for x in a:
b.append(x*x)
>>> print(b)
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
Tuples
Tuples are similar to lists except that they are not mutable, i.e.,
the length and the content of the tuple cannot be changed at runtime.
Syntactically, the list uses [ ] while tuples use ( ). Similar to lists,
tuples may contain any data type including other tuples. Here are a
few examples:
>>> a = (1,2,3,4)
>>> print(a)
(1,2,3,4)
>>> b = (3,)
>>> c = ((1,2),(3,4))
Sets
A set is an unordered collection of unique objects. To create a set,
we need to use the function set or the operator {}. Here are some
examples:
>>> s1 = set([1,2,3,4])
>>> s2 = set((1,1,3,4))
>>> print(s2)
set([1,3,4])
>>> a = [1,2,3,4,3,5]
>>> b = set(a)
>>> print(b)
set([1,2,3,4,5])
>>> c = list(b)
>>> print(c)
[1,2,3,4,5]
Dictionaries
Dictionaries store key-value pairs. A dictionary is created by enclos-
ing a key-value pair inside { }.
>>> a = {
'lang':'python'
'ver': '3.6.6'
}
In the example above, we added a new key called creator and stored
the string, “Guido Von Rossum.”
In certain instances, the dictionary membership needs to be tested
using the ‘in’ operator. To obtain a list of all the dictionary keys, use
the keys() method.
>>> fo = open('myfile.csv')
>>> for i in fo.readlines():
... print(i)
>>> fo.close()
Python,3.6.6
Django, 3.0.5
Apache, 2.4
The first line opens a file and returns a new file object which is
stored in the file object “fo.” The method readlines in line 2, reads
all the lines of input. The for-loop then iterates over each of those lines,
and prints. The file is finally closed using the close method.
The output of the print command is a string. Hence, string manip-
ulation using methods like split, strip, etc., needs to be applied in order
to extract elements of each column. Also, note that there is an extra
newline character at the end of each print statement.
The first line imports the csv module. The second line opens and
reads the content of the csv file using the reader function in the csv
module. In every iteration of the loop, the content of one line of the csv
file is returned and stored in the variable ‘i’. Finally, the value of ‘i’ is
printed.
18 Image Processing and Acquisition using Python
Date | Time |
2020-01-02 00:00:00 | 10:15:00 |
2020-01-05 00:00:00 | 11:00:00 |
2020-01-07 00:00:00 | 15:00:00 |
import math
def circleproperties(r):
area = math.pi*r*r;
circumference = 2*math.pi*r
return area, circumference
1.6 Summary
• Python is a popular high-level programming language. It is used
for most common programming tasks such as scientific computa-
tion, text processing, building dynamic websites, etc.
20 Image Processing and Acquisition using Python
• Python can read most of the common text formats like CSV,
Microsoft Excel, etc.
1.7 Exercises
1. If you are familiar with any other programming language, list the
differences between that language and Python.
4. Print the content of the list from Question 3 and also the
corresponding index using the list's enumerate method in the
for-loop.
7. Look up documentation for the join method and join the con-
tents of the list [‘Minneapolis’,‘MN’,‘USA’] and obtain the string
‘Minneapolis, MN, USA.’
a = [1,2,3,4,2,3,5]
b = []
for i in a:
if i>2:
b.append(i)
print(b)
Rewrite the above code using list comprehension and reduce the
number of lines.
Bibliography
[ABC+ 16] Martin Abadi, Paul Barham, Jianmin Chen, Zhifeng Chen,
Andy Davis, Jeffrey Dean, Matthieu Devin, Sanjay Ghe-
mawat, Geoffrey Irving, Michael Isard, Manjunath Kudlur,
Josh Levenberg, Rajat Monga, Sherry Moore, Derek G.
Murray, Benoit Steiner, Paul Tucker, Vijay Vasudevan,
Pete Warden, Martin Wicke, Yuan Yu, and Xiaoqiang
Zheng. Tensorflow: A system for large-scale machine learn-
ing. In 12th USENIX Symposium on Operating Systems
Design and Implementation (OSDI 16), pages 265–283,
2016.
23
24 Bibliography
[CT65] J.W. Cooley and J.W. Tukey. An algorithm for the machine
calculation of complex Fourier series. Mathematics of Com-
putation, 19:297–301, 1965.
[Dom15] Pedro Domingos. The Master Algorithm: How the Quest for
the Ultimate Learning Machine Will Remake Our World.
Basic Books, 2015.
[GWE09] R.C. Gonzalez, R.E. Woods, and S.L. Eddins. Digital image
processing using MATLAB R , 2nd ed. Gatesmark Publish-
ing, TN, 2009.
Bibliography 27
[JS78] P.M. Joseph and R.D. Spital. A method for correcting bone
induced artifacts in computed tomography scanners. Jour-
nal of Computer Assisted Tomography, 2:100–108, 1978.
[LK87] L.A. Love and R.A. Kruger. Scatter estimation for a digital
radiographic system using convolution filtering. Medical
Physics, 14(2):178–185, 1987.
[LLM86] H. Li, M.A. Lavin, and R.J. Le Master. Fast hough trans-
form: A hierarchical approach. Computer Vision, Graphics,
and Image Processing, 36(2-3):139–161, 1986.
30 Bibliography
[PK81] S.K. Pal and R.A. King. Image enhancement using smooth-
ing with fuzzy sets. IEEE Transactions on Systems, Man,
and Cybernetics, 11(7):494–501, 1981.
[Rus11] J.C. Russ. The Image Processing Handbook, 6th ed. CRC
Press, Boca Raton, FL, 2011.
[Sch04] H.M. Schey. Div, Grad, Curl, and All That, 4th ed. W.W.
Norton and Company, New York, NY, 2004.