Giới thiệu sách Python: Image Processing and Acquisition using Python - TL

xủa lý ảnh sử dụng python

Image Processing and Acquisition using Python 

Ravishankar Chityala
Sridevi Pudipeddi

I Introduction to Images and Computing using
Python 1

1 Introduction to Python 3
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 3
1.2 What is Python? . . . . . . . . . . . . . . . . . . . . . 4
1.3 Python Environments . . . . . . . . . . . . . . . . . . 5
1.3.1 Python Interpreter . . . . . . . . . . . . . . . . 6
1.3.2 Enthought Python Distribution (EPD) . . . . . 6
1.3.3 PythonXY . . . . . . . . . . . . . . . . . . . . . 7
1.4 Running a Python Program . . . . . . . . . . . . . . . 8
1.5 Basic Python Statements and Data Types . . . . . . . 8
1.5.1 Data Structures . . . . . . . . . . . . . . . . . . 11
1.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 19
viiviii Contents
1.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 20
2 Computing using Python Modules 23
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 23
2.2 Python Modules . . . . . . . . . . . . . . . . . . . . . 23
2.2.1 Creating Modules . . . . . . . . . . . . . . . . . 24
2.2.2 Loading Modules . . . . . . . . . . . . . . . . . 24
2.3 Numpy . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.3.1 Numpy Array or Matrices? . . . . . . . . . . . . 30
2.4 Scipy . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
2.5 Matplotlib . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.6 Python Imaging Library . . . . . . . . . . . . . . . . . 33
2.7 Scikits . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
2.8 Python OpenCV Module . . . . . . . . . . . . . . . . 34
2.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 34
2.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 35
3 Image and its Properties 37
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 37
3.2 Image and its Properties . . . . . . . . . . . . . . . . . 38
3.2.1 Bit Depth . . . . . . . . . . . . . . . . . . . . . 38
3.2.2 Pixel and Voxel . . . . . . . . . . . . . . . . . . 39
3.2.3 Image Histogram . . . . . . . . . . . . . . . . . 41
3.2.4 Window and Level . . . . . . . . . . . . . . . . 42
3.2.5 Connectivity: 4 or 8 Pixels . . . . . . . . . . . . 43
3.3 Image Types . . . . . . . . . . . . . . . . . . . . . . . 44
3.3.1 JPEG . . . . . . . . . . . . . . . . . . . . . . . 44
3.3.2 TIFF . . . . . . . . . . . . . . . . . . . . . . . . 44
3.3.3 DICOM . . . . . . . . . . . . . . . . . . . . . . 45
3.4 Data Structures for Image Analysis . . . . . . . . . . . 49
3.4.1 Reading Images . . . . . . . . . . . . . . . . . . 49
3.4.2 Displaying Images . . . . . . . . . . . . . . . . . 50
3.4.3 Writing Images . . . . . . . . . . . . . . . . . . 50Contents ix
3.5 Programming Paradigm . . . . . . . . . . . . . . . . . 51
3.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 53
3.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 53
II Image Processing using Python 55
4 Spatial Filters 57
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 57
4.2 Filtering . . . . . . . . . . . . . . . . . . . . . . . . . . 58
4.2.1 Mean Filter . . . . . . . . . . . . . . . . . . . . 60
4.2.2 Median Filter . . . . . . . . . . . . . . . . . . . 64
4.2.3 Max Filter . . . . . . . . . . . . . . . . . . . . . 66
4.2.4 Min Filter . . . . . . . . . . . . . . . . . . . . . 68
4.3 Edge Detection using Derivatives . . . . . . . . . . . . 69
4.3.1 First Derivative Filters . . . . . . . . . . . . . . 71
4.3.2 Second Derivative Filters . . . . . . . . . . . . . 79
4.4 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 85
4.5 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 86
5 Image Enhancement 89
5.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 89
5.2 Pixel Transformation . . . . . . . . . . . . . . . . . . . 89
5.3 Image Inverse . . . . . . . . . . . . . . . . . . . . . . . 91
5.4 Power Law Transformation . . . . . . . . . . . . . . . 92
5.5 Log Transformation . . . . . . . . . . . . . . . . . . . 97
5.6 Histogram Equalization . . . . . . . . . . . . . . . . . 99
5.7 Contrast Stretching . . . . . . . . . . . . . . . . . . . 103
5.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 106
5.9 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 107
6 Fourier Transform 109
6.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 109
6.2 Definition of Fourier Transform . . . . . . . . . . . . . 110
6.3 Two-Dimensional Fourier Transform . . . . . . . . . . 113x Contents
6.3.1 Fast Fourier Transform using Python . . . . . . 115
6.4 Convolution . . . . . . . . . . . . . . . . . . . . . . . . 118
6.4.1 Convolution in Fourier Space . . . . . . . . . . 119
6.5 Filtering in Frequency Domain . . . . . . . . . . . . . 120
6.5.1 Ideal Lowpass Filter . . . . . . . . . . . . . . . 120
6.5.2 Butterworth Lowpass Filter . . . . . . . . . . . 123
6.5.3 Gaussian Lowpass Filter . . . . . . . . . . . . . 125
6.5.4 Ideal Highpass Filter . . . . . . . . . . . . . . . 127
6.5.5 Butterworth Highpass Filter . . . . . . . . . . . 130
6.5.6 Gaussian Highpass Filter . . . . . . . . . . . . . 132
6.5.7 Bandpass Filter . . . . . . . . . . . . . . . . . . 134
6.6 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 137
6.7 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 138
7 Segmentation 139
7.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 139
7.2 Histogram Based Segmentation . . . . . . . . . . . . . 139
7.2.1 Otsu’s Method . . . . . . . . . . . . . . . . . . 141
7.2.2 Renyi Entropy . . . . . . . . . . . . . . . . . . . 144
7.2.3 Adaptive Thresholding . . . . . . . . . . . . . . 149
7.3 Region Based Segmentation . . . . . . . . . . . . . . . 151
7.3.1 Watershed Segmentation . . . . . . . . . . . . . 153
7.4 Segmentation Algorithm for Various Modalities . . . . 161
7.4.1 Segmentation of Computed Tomography Image 161
7.4.2 Segmentation of MRI Image . . . . . . . . . . . 161
7.4.3 Segmentation of Optical and Electron Microscope Image . . . . . . . . . . . . . . . . . . . . 162
7.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 162
7.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 163
8 Morphological Operations 165
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 165
8.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . 165Contents xi
8.3 Dilation . . . . . . . . . . . . . . . . . . . . . . . . . . 166
8.4 Erosion . . . . . . . . . . . . . . . . . . . . . . . . . . 171
8.5 Grayscale Dilation and Erosion . . . . . . . . . . . . . 175
8.6 Opening and Closing . . . . . . . . . . . . . . . . . . . 176
8.7 Hit-or-Miss . . . . . . . . . . . . . . . . . . . . . . . . 179
8.8 Thickening and Thinning . . . . . . . . . . . . . . . . 184
8.8.1 Skeletonization . . . . . . . . . . . . . . . . . . 185
8.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 186
8.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 187
9 Image Measurements 189
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 189
9.2 Labeling . . . . . . . . . . . . . . . . . . . . . . . . . . 189
9.3 Hough Transform . . . . . . . . . . . . . . . . . . . . . 194
9.3.1 Hough Line . . . . . . . . . . . . . . . . . . . . 194
9.3.2 Hough Circle . . . . . . . . . . . . . . . . . . . 197
9.4 Template Matching . . . . . . . . . . . . . . . . . . . . 201
9.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 205
9.6 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 205
III Image Acquisition 207
10 X-Ray and Computed Tomography 209

10.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 209
10.2 History . . . . . . . . . . . . . . . . . . . . . . . . . . 209
10.3 X-Ray Generation . . . . . . . . . . . . . . . . . . . . 210
10.3.1 X-Ray Tube Construction . . . . . . . . . . . . 210
10.3.2 X-Ray Generation Process . . . . . . . . . . . . 212
10.4 Material Properties . . . . . . . . . . . . . . . . . . . . 216
10.4.1 Attenuation . . . . . . . . . . . . . . . . . . . . 216
10.4.2 Lambert Beer Law for Multiple Materials . . . 218
10.5 X-Ray Detection . . . . . . . . . . . . . . . . . . . . . 219
10.5.1 Image Intensifier . . . . . . . . . . . . . . . . . 220
10.5.2 Multiple-Field II . . . . . . . . . . . . . . . . . 221xii Contents
10.5.3 Flat Panel Detector (FPD) . . . . . . . . . . . . 223
10.6 X-Ray Imaging Modes . . . . . . . . . . . . . . . . . . 224
10.6.1 Fluoroscopy . . . . . . . . . . . . . . . . . . . . 224
10.6.2 Angiography . . . . . . . . . . . . . . . . . . . . 224
10.7 Computed Tomography (CT) . . . . . . . . . . . . . . 226
10.7.1 Reconstruction . . . . . . . . . . . . . . . . . . 227
10.7.2 Parallel Beam CT . . . . . . . . . . . . . . . . . 227
10.7.3 Central Slice Theorem . . . . . . . . . . . . . . 228
10.7.4 Fan Beam CT . . . . . . . . . . . . . . . . . . . 232
10.7.5 Cone Beam CT . . . . . . . . . . . . . . . . . . 233
10.7.6 Micro-CT . . . . . . . . . . . . . . . . . . . . . 234
10.8 Hounsfield Unit (HU) . . . . . . . . . . . . . . . . . . 236
10.9 Artifacts . . . . . . . . . . . . . . . . . . . . . . . . . . 237
10.9.1 Geometric Misalignment Artifacts . . . . . . . . 238
10.9.2 Scatter . . . . . . . . . . . . . . . . . . . . . . . 238
10.9.3 Offset and Gain Correction . . . . . . . . . . . . 240
10.9.4 Beam Hardening . . . . . . . . . . . . . . . . . 241
10.9.5 Metal Artifacts . . . . . . . . . . . . . . . . . . 242
10.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 243
10.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 244
11 Magnetic Resonance Imaging 247
11.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 247
11.2 Laws Governing NMR and MRI . . . . . . . . . . . . 248
11.2.1 Faraday’s Law . . . . . . . . . . . . . . . . . . . 248
11.2.2 Larmor Frequency . . . . . . . . . . . . . . . . . 249
11.2.3 Bloch Equation . . . . . . . . . . . . . . . . . . 250
11.3 Material Properties . . . . . . . . . . . . . . . . . . . . 251
11.3.1 Gyromagnetic Ratio . . . . . . . . . . . . . . . 251
11.3.2 Proton Density . . . . . . . . . . . . . . . . . . 252
11.3.3 T1 and T2 Relaxation Times . . . . . . . . . . . 253
11.4 NMR Signal Detection . . . . . . . . . . . . . . . . . . 255
11.5 MRI Signal Detection or MRI Imaging . . . . . . . . . 256Contents xiii
11.5.1 Slice Selection . . . . . . . . . . . . . . . . . . . 258
11.5.2 Phase Encoding . . . . . . . . . . . . . . . . . . 258
11.5.3 Frequency Encoding . . . . . . . . . . . . . . . 259
11.6 MRI Construction . . . . . . . . . . . . . . . . . . . . 259
11.6.1 Main Magnet . . . . . . . . . . . . . . . . . . . 259
11.6.2 Gradient Magnet . . . . . . . . . . . . . . . . . 260
11.6.3 RF Coils . . . . . . . . . . . . . . . . . . . . . . 261
11.6.4 K-Space Imaging . . . . . . . . . . . . . . . . . 262
11.7 T1, T2 and Proton Density Image . . . . . . . . . . . . 263
11.8 MRI Modes or Pulse Sequence . . . . . . . . . . . . . 265
11.8.1 Spin Echo Imaging . . . . . . . . . . . . . . . . 265
11.8.2 Inversion Recovery . . . . . . . . . . . . . . . . 266
11.8.3 Gradient Echo Imaging . . . . . . . . . . . . . . 267
11.9 MRI Artifacts . . . . . . . . . . . . . . . . . . . . . . . 268
11.9.1 Motion Artifact . . . . . . . . . . . . . . . . . . 269
11.9.2 Metal Artifact . . . . . . . . . . . . . . . . . . . 271
11.9.3 Inhomogeneity Artifact . . . . . . . . . . . . . . 271
11.9.4 Partial Volume Artifact . . . . . . . . . . . . . . 272
11.10 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 272
11.11 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 273
12 Light Microscopes 275
12.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 275
12.2 Physical Principles . . . . . . . . . . . . . . . . . . . . 276
12.2.1 Geometric Optics . . . . . . . . . . . . . . . . . 276
12.2.2 Numerical Aperture . . . . . . . . . . . . . . . . 277
12.2.3 Diffraction Limit . . . . . . . . . . . . . . . . . 278
12.2.4 Objective Lens . . . . . . . . . . . . . . . . . . 280
12.2.5 Point Spread Function (PSF) . . . . . . . . . . 281
12.2.6 Wide-Field Microscopes . . . . . . . . . . . . . 282
12.3 Construction of a Wide-Field Microscope . . . . . . . 282
12.4 Epi-Illumination . . . . . . . . . . . . . . . . . . . . . 284
12.5 Fluorescence Microscope . . . . . . . . . . . . . . . . . 284xiv Contents
12.5.1 Theory . . . . . . . . . . . . . . . . . . . . . . . 284
12.5.2 Properties of Fluorochromes . . . . . . . . . . . 285
12.5.3 Filters . . . . . . . . . . . . . . . . . . . . . . . 287
12.6 Confocal Microscopes . . . . . . . . . . . . . . . . . . 288
12.7 Nipkow Disk Microscopes . . . . . . . . . . . . . . . . 289
12.8 Confocal or Wide-Field? . . . . . . . . . . . . . . . . . 291
12.9 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 292
12.10 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 293
13 Electron Microscopes 295
13.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 295
13.2 Physical Principles . . . . . . . . . . . . . . . . . . . . 296
13.2.1 Electron Beam . . . . . . . . . . . . . . . . . . 297
13.2.2 Interaction of Electron with Matter . . . . . . . 298
13.2.3 Interaction of Electrons in TEM . . . . . . . . . 299
13.2.4 Interaction of Electrons in SEM . . . . . . . . . 300
13.3 Construction of EM . . . . . . . . . . . . . . . . . . . 301
13.3.1 Electron Gun . . . . . . . . . . . . . . . . . . . 301
13.3.2 Electromagnetic Lens . . . . . . . . . . . . . . . 303
13.3.3 Detectors . . . . . . . . . . . . . . . . . . . . . . 304
13.4 Specimen Preparations . . . . . . . . . . . . . . . . . . 306
13.5 Construction of TEM . . . . . . . . . . . . . . . . . . 307
13.6 Construction of SEM . . . . . . . . . . . . . . . . . . . 308
13.7 Summary . . . . . . . . . . . . . . . . . . . . . . . . . 309
13.8 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . 311
A Installing Python Distributions 313
A.1 Windows . . . . . . . . . . . . . . . . . . . . . . . . . 313
A.1.1 PythonXY . . . . . . . . . . . . . . . . . . . . . 313
A.1.2 Enthought Python Distribution . . . . . . . . . 316
A.1.3 Updating or Installing New Modules . . . . . . 316
A.2 Mac or Linux . . . . . . . . . . . . . . . . . . . . . . . 318
A.2.1 Enthought Python Distribution . . . . . . . . . 318Contents xv
A.2.2 Installing New Modules . . . . . . . . . . . . . . 318
B Parallel Programming Using MPI4Py 323
B.1 Introduction to MPI . . . . . . . . . . . . . . . . . . . 323
B.2 Need for MPI in Python Image Processing . . . . . . . 324
B.3 Introduction to MPI4Py . . . . . . . . . . . . . . . . . 325
B.4 Communicator . . . . . . . . . . . . . . . . . . . . . . 326
B.5 Communication . . . . . . . . . . . . . . . . . . . . . . 327
B.5.1 Point-to-Point Communication . . . . . . . . . 327
B.5.2 Collective Communication . . . . . . . . . . . . 329
B.6 Calculating the Value of PI . . . . . . . . . . . . . . . 331
C Introduction to ImageJ 333
C.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 333
C.2 ImageJ Primer . . . . . . . . . . . . . . . . . . . . . . 334
D MATLAB R and Numpy Functions 337
D.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . 337
Bibliography 341
Index 351