100% found this document useful (2 votes)
51 views

Image operators image processing in Python First Edition Kinser download pdf

Kinser

Uploaded by

dobarchaudy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
51 views

Image operators image processing in Python First Edition Kinser download pdf

Kinser

Uploaded by

dobarchaudy
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 52

Download the full version of the textbook now at textbookfull.

com

Image operators image processing in Python


First Edition Kinser

https://textbookfull.com/product/image-operators-
image-processing-in-python-first-edition-kinser/

Explore and download more textbook at https://textbookfull.com


Recommended digital products (PDF, EPUB, MOBI) that
you can download immediately if you are interested.

Image Operators: Image Processing in Python 1st Edition


Jason M. Kinser

https://textbookfull.com/product/image-operators-image-processing-in-
python-1st-edition-jason-m-kinser/

textbookfull.com

Mathematical Image Processing Kristian Bredies

https://textbookfull.com/product/mathematical-image-processing-
kristian-bredies/

textbookfull.com

Computing Colour Image Processing Alan Parkin

https://textbookfull.com/product/computing-colour-image-processing-
alan-parkin/

textbookfull.com

Contrastive Analysis of Discourse pragmatic Aspects of


Linguistic Genres 1st Edition Karin Aijmer

https://textbookfull.com/product/contrastive-analysis-of-discourse-
pragmatic-aspects-of-linguistic-genres-1st-edition-karin-aijmer/

textbookfull.com
PRICAI 2019 Trends in Artificial Intelligence 16th Pacific
Rim International Conference on Artificial Intelligence
Cuvu Yanuca Island Fiji August 26 30 2019 Proceedings Part
II Abhaya C. Nayak
https://textbookfull.com/product/pricai-2019-trends-in-artificial-
intelligence-16th-pacific-rim-international-conference-on-artificial-
intelligence-cuvu-yanuca-island-fiji-august-26-30-2019-proceedings-
part-ii-abhaya-c-nayak/
textbookfull.com

The Cambridge Handbook of Discourse Studies 1st Edition


Anna De Fina

https://textbookfull.com/product/the-cambridge-handbook-of-discourse-
studies-1st-edition-anna-de-fina/

textbookfull.com

Zonal marking from Ajax to Zidane the making of modern


soccer First Us Edition. Edition Cox

https://textbookfull.com/product/zonal-marking-from-ajax-to-zidane-
the-making-of-modern-soccer-first-us-edition-edition-cox/

textbookfull.com

The Last 8 1st Edition Laura Pohl [Pohl

https://textbookfull.com/product/the-last-8-1st-edition-laura-pohl-
pohl/

textbookfull.com

Linux for Developers Jumpstart Your Linux Programming


Skills William "Bo" Rothwell

https://textbookfull.com/product/linux-for-developers-jumpstart-your-
linux-programming-skills-william-bo-rothwell/

textbookfull.com
Non Hermitian Hamiltonians in Quantum Physics Selected
Contributions from the 15th International Conference on
Non Hermitian Hamiltonians in Quantum Physics Palermo
Italy 18 23 May 2015 1st Edition Fabio Bagarello
https://textbookfull.com/product/non-hermitian-hamiltonians-in-
quantum-physics-selected-contributions-from-the-15th-international-
conference-on-non-hermitian-hamiltonians-in-quantum-physics-palermo-
italy-18-23-may-2015-1st-edition-fa/
textbookfull.com
Image Operators
Image Operators
Image Processing in Python

Jason M. Kinser
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2019 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business
No claim to original U.S. Government works

Printed on acid-free paper


International Standard Book Number-13: 978-1-4987-9618-7 (Hardback)
This book contains information obtained from authentic and highly regarded sources. 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, reproduced, transmitted, or utilized in
any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying, micro-
filming, and recording, or in any information storage or retrieval system, without written permission from the publishers.
For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.
copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-
750-8400. CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations
that have been granted a photocopy license by the CCC, a separate system of payment has been arranged.
Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for identifi-
cation and explanation without intent to infringe.

Library of Congress Cataloging-in-Publication Data

Names: Kinser, Jason M., 1962- author.


Title: Image operators: image processing in Python/Jason M. Kinser.
Description: First edition. | Boca Raton, FL: CRC Press/Taylor & Francis Group, [2019] |
“A CRC title, part of the Taylor & Francis imprint, a member of the Taylor & Francis Group,
the academic division of T&F Informa plc.” | Includes bibliographical references and index.
Identifiers: LCCN 2018017140 (print) | LCCN 2018017764 (ebook) | ISBN 9780429451188 (eBook) |
ISBN 9780429835940 (Adobe PDF) | ISBN 9780429835933 (ePUB) |
ISBN 9780429835926 (Mobipocket) | ISBN 9781498796187 (hardback: acid-free paper)
Subjects: LCSH: Image processing—Digital techniques. | Python (Computer program language)
Classification: LCC TA1637 (ebook) | LCC TA1637 .K48 2019 (print) | DDC 006.6—dc23
LC record available at https://lccn.loc.gov/2018017140

Visit the Taylor & Francis Web site at


http://www.taylorandfrancis.com

and the CRC Press Web site at


http://www.crcpress.com
This book is dedicated to my beautiful wife – Sue Ellen.
Contents

Python Codes ................................................................................................................................... xv


Preface.............................................................................................................................................xxi
Software and Data.........................................................................................................................xxiii
Author ............................................................................................................................................ xxv

PART I Image Operators

Chapter 1 Introduction ............................................................................................................. 3


1.1 Scripting in Python ......................................................................................... 3
1.2 Installation ...................................................................................................... 4
1.2.1 Example Codes .................................................................................. 4
1.2.2 Establishing a Work Space................................................................. 4
1.2.3 The Spyder Interface.......................................................................... 5
1.2.4 Intent of the Text ................................................................................ 5

Chapter 2 Operator Nomenclature ........................................................................................... 7


2.1 Image Notation ............................................................................................... 7
2.2 Operators......................................................................................................... 8
2.2.1 Creation Operators ............................................................................. 8
2.2.2 Channel Operators ............................................................................. 9
2.2.3 Informational Operators................................................................... 12
2.2.4 Intensity Operators........................................................................... 14
2.2.5 Geometric Operators........................................................................ 16
2.2.6 Transformation Operators ................................................................ 16
2.2.7 Expansion Operators........................................................................ 17
2.3 Combinations and Reduced Notation ........................................................... 18
2.4 Summary....................................................................................................... 19

Chapter 3 Scripting in Python ................................................................................................ 21


3.1 Basic Python Skills ....................................................................................... 21
3.1.1 Variables........................................................................................... 21
3.1.2 Strings .............................................................................................. 22
3.1.3 Type Conversions with Strings ........................................................ 23
3.2 Tuples, List, Dictionaries, and Sets .............................................................. 23
3.2.1 Tuple ................................................................................................ 23
3.2.2 Slicing .............................................................................................. 23
3.2.3 Lists.................................................................................................. 25
3.2.4 Dictionaries ...................................................................................... 25
3.2.5 Sets................................................................................................... 26
3.3 Flow Control ................................................................................................. 26
3.3.1 The if Command .............................................................................. 27
3.3.2 The while Command........................................................................ 28

vii
viii Contents

3.3.3 Break and Continue.......................................................................... 29


3.3.4 The For Loop ................................................................................... 29
3.3.5 The map and lambda Functions ....................................................... 31
3.3.6 Image Operators and Control........................................................... 31
3.4 Input and Output ........................................................................................... 32
3.4.1 Reading and Writing Text Files ....................................................... 32
3.4.2 Pickling Files ................................................................................... 32
3.5 Defining Functions........................................................................................ 33
3.5.1 Function Components ...................................................................... 33
3.5.2 Returns ............................................................................................. 34
3.5.3 Default Arguments........................................................................... 35
3.5.4 Function Help................................................................................... 35
3.6 Modules ........................................................................................................ 36
3.7 Errors ............................................................................................................ 38
3.8 NumPy .......................................................................................................... 39
3.8.1 Creating Arrays................................................................................ 39
3.8.1.1 Zeros and Ones ................................................................ 39
3.8.1.2 Random............................................................................ 40
3.8.1.3 Geometric Shapes............................................................ 41
3.8.1.4 Conversion of Numerical Data ........................................ 41
3.8.2 Manipulating Arrays ........................................................................ 42
3.8.2.1 Display Option................................................................. 42
3.8.2.2 Converting Arrays ........................................................... 42
3.8.2.3 Simple Math .................................................................... 42
3.8.2.4 Multiplying Vectors ......................................................... 43
3.8.2.5 Multiplying Matrices ....................................................... 44
3.8.2.6 Array Functions ............................................................... 44
3.8.2.7 Decisions ......................................................................... 47
3.8.2.8 Advanced Slicing............................................................. 48
3.8.2.9 Universal Functions ......................................................... 48
3.8.2.10 Sorting ............................................................................. 49
3.8.3 Indices .............................................................................................. 51
3.9 SciPy ............................................................................................................. 52
3.9.1 Loading and Saving Images............................................................. 53
3.9.2 Examples from ndimage .................................................................. 54
3.9.2.1 Rotation and Shift............................................................ 54
3.9.2.2 Center of Mass................................................................. 55
3.10 Summary....................................................................................................... 56

Chapter 4 Digital Images ....................................................................................................... 59


4.1 Images in Python .......................................................................................... 59
4.2 Resolution ..................................................................................................... 59
4.2.1 Intensity Resolution ......................................................................... 59
4.2.2 Spatial Resolution ............................................................................ 61
4.3 Digital Formats ............................................................................................. 63
4.3.1 Bitmaps ............................................................................................ 63
4.3.2 JPEG ................................................................................................ 63
4.3.3 GIF ................................................................................................... 63
4.3.4 TIFF ................................................................................................. 64
Contents ix

4.3.5 PNG.................................................................................................. 65
4.3.6 Other Compressions......................................................................... 65
4.4 Summary....................................................................................................... 65

Chapter 5 Color ...................................................................................................................... 67


5.1 The RGB Color Model ................................................................................. 67
5.2 The HSV Color Model.................................................................................. 69
5.3 The YUV Family .......................................................................................... 72
5.4 CIE L*a*b*................................................................................................... 73
5.5 Improvements in Recognition....................................................................... 74
5.6 Summary....................................................................................................... 77

PART II Image Space Manipulations


Chapter 6 Geometric Transformations ................................................................................... 81
6.1 Selections...................................................................................................... 81
6.2 Linear Translation......................................................................................... 83
6.2.1 Simple Shifting ................................................................................ 83
6.2.2 NonInteger Shifts ............................................................................. 84
6.3 Scaling .......................................................................................................... 85
6.4 Rotation......................................................................................................... 87
6.5 Dilation and Erosion ..................................................................................... 88
6.6 Coordinate Mapping ..................................................................................... 90
6.7 Polar Transformations................................................................................... 90
6.7.1 Theory .............................................................................................. 91
6.7.2 Python Implementation.................................................................... 92
6.7.3 Example ........................................................................................... 94
6.8 Pincushion and Barrel Transformations ...................................................... 95
6.9 Other Transformations.................................................................................. 96
6.9.1 Generic Transformations.................................................................. 97
6.9.2 Affine Transformation...................................................................... 98
6.10 Summary....................................................................................................... 99

Chapter 7 Image Morphing .................................................................................................. 101


7.1 Warp............................................................................................................ 101
7.1.1 Marking Fiducial Points................................................................. 101
7.1.2 Image Dancer ................................................................................. 101
7.1.3 Delaunay Tessellation .................................................................... 103
7.1.4 Applying the Warp ......................................................................... 104
7.2 Average Face............................................................................................... 106
7.3 Image Morphing ......................................................................................... 107

Chapter 8 Principle Component Analysis ............................................................................ 111


8.1 The Purpose of PCA ................................................................................... 111
8.2 Covariance Matrix ...................................................................................... 111
8.3 Eigenvectors................................................................................................ 112
8.4 PCA............................................................................................................. 113
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
x Contents

8.4.1 Distance Tests ................................................................................ 116


8.4.2 Organization Example ................................................................... 116
8.4.3 RGB Example ................................................................................ 121
8.5 First Order Nature of PCA.......................................................................... 124
8.6 Summary..................................................................................................... 124

Chapter 9 Eigenimages ........................................................................................................ 127


9.1 Eigenimages................................................................................................ 127
9.1.1 Large Covariance Matrix ............................................................... 128
9.1.2 Python Implementation.................................................................. 128
9.1.3 Face Recognition Example ............................................................ 130
9.1.4 Natural Eigenimages...................................................................... 131

PART III Frequency Space Manipulations

Chapter 10 Image Frequencies............................................................................................... 137


10.1 Complex Numbers ...................................................................................... 137
10.2 Theory......................................................................................................... 138
10.3 Digital Fourier Transform........................................................................... 138
10.3.1 FFT in Python ................................................................................ 139
10.3.2 Signal Reconstruction .................................................................... 139
10.4 Properties of a Fourier Transform............................................................... 140
10.4.1 DC Term......................................................................................... 140
10.4.2 Conservation of Energy ................................................................. 141
10.4.3 Replication ..................................................................................... 142
10.4.4 Addition ......................................................................................... 142
10.4.5 Shift................................................................................................ 143
10.4.6 Scale............................................................................................... 143
10.4.7 Power Spectrum ............................................................................. 144
10.5 Displaying the Transform ........................................................................... 144
10.6 Simple Shapes............................................................................................. 145
10.6.1 Rectangle........................................................................................ 145
10.6.2 Circle.............................................................................................. 146
10.7 Frequency Bands ........................................................................................ 147
10.8 Windowing.................................................................................................. 149
10.9 Summary..................................................................................................... 152

Chapter 11 Filtering in Frequency Space............................................................................... 153


11.1 Frequency Filtering..................................................................................... 153
11.1.1 Low-pass Filter .............................................................................. 153
11.1.2 High-pass Filter.............................................................................. 154
11.1.3 Band-pass Filter ............................................................................. 155
11.2 Directional Filtering.................................................................................... 156
11.3 Fingerprint Example ................................................................................... 158
11.4 Artifact Removal......................................................................................... 160
11.5 Summary..................................................................................................... 163
11.6 Problems ..................................................................................................... 163
Contents xi

Chapter 12 Correlations ......................................................................................................... 165


12.1 Justification and Theory.............................................................................. 165
12.2 Theory......................................................................................................... 165
12.2.1 Computations in Fourier Space...................................................... 166
12.3 Implementation in Python........................................................................... 167
12.3.1 Brute Force..................................................................................... 167
12.3.2 Method Based on Fourier Transforms ........................................... 168
12.3.3 Example – Geometric Shapes ........................................................ 169
12.3.4 Example – Boat Isolation............................................................... 170
12.4 Composite Filtering .................................................................................... 174
12.5 SDF and MACE.......................................................................................... 175
12.5.1 Fractional Power Filter (FPF) ........................................................ 176
12.5.1.1 Theory............................................................................ 176
12.5.1.2 Manipulating α .............................................................. 177
12.5.1.3 Example......................................................................... 178
12.5.1.4 The Constraints.............................................................. 180
12.5.1.5 Dual FPFs ...................................................................... 182
12.6 Restrictions of Correlations ........................................................................ 184
12.7 Summary..................................................................................................... 184

PART IV Texture and Shape

Chapter 13 Edge Detection .................................................................................................... 189


13.1 Edges........................................................................................................... 189
13.2 The Sobel Filters......................................................................................... 190
13.3 Difference of Gaussians.............................................................................. 191
13.4 Corners........................................................................................................ 193

Chapter 14 Hough Transforms ............................................................................................... 199


14.1 Detection of a Line ..................................................................................... 199
14.2 Detection of a Circle................................................................................... 202
14.3 Application ................................................................................................. 204
14.4 Summary..................................................................................................... 205

Chapter 15 Noise.................................................................................................................... 209


15.1 Random Noise ............................................................................................ 209
15.2 Salt and Pepper Noise................................................................................. 209
15.3 Camera Noise.............................................................................................. 212
15.4 Colored Noise ............................................................................................. 212
15.5 Comparison of Noise Removal Systems .................................................... 212
15.5.1 Smoothing ...................................................................................... 213
15.5.2 Low-Pass Filtering ......................................................................... 214
15.5.3 Erosion and Dilation ...................................................................... 214
15.5.4 Median Filter.................................................................................. 215
15.5.5 Wiener Filter .................................................................................. 216
15.6 Other Types of Noise .................................................................................. 217
15.7 Summary..................................................................................................... 217
xii Contents

Chapter 16 Texture Recognition ............................................................................................ 221


16.1 Data............................................................................................................. 221
16.2 Edge Density............................................................................................... 221
16.2.1 Statistical Method .......................................................................... 221
16.2.2 The Method of Rosenfeld and Thurston ........................................ 223
16.2.3 Wavelet Decomposition and Texture ............................................. 227
16.2.4 Gray-Level Co-Occurrence Matrix ................................................ 230
16.2.4.1 Angular Second Moment............................................... 232
16.2.4.2 Contrast.......................................................................... 232
16.2.4.3 Correlation..................................................................... 233
16.2.4.4 Variance ......................................................................... 234
16.2.4.5 Entropy .......................................................................... 234
16.2.4.6 The Remaining Haralick Metrics .................................. 235
16.3 Filter-Based Methods.................................................................................. 238
16.3.1 Law’s Filters................................................................................... 238
16.4 Summary..................................................................................................... 240

Chapter 17 Gabor Filtering .................................................................................................... 243


17.1 Gabor Filtering............................................................................................ 243
17.2 Edge Response............................................................................................ 245
17.3 Texture Extraction with Gabor Filters ........................................................ 246
17.4 Gabor Filters in Fourier Space.................................................................... 249
17.5 Summary..................................................................................................... 249

Chapter 18 Describing Shape................................................................................................. 251


18.1 Contour Methods ........................................................................................ 251
18.1.1 Chain Code..................................................................................... 251
18.1.2 The Polygon Method...................................................................... 252
18.1.3 Metrics Used to Describe Shape .................................................... 252
18.1.4 Fourier Descriptors ........................................................................ 255
18.1.5 Wavelets ......................................................................................... 258
18.1.6 Elastic Matching ............................................................................ 258
18.2 Region Methods.......................................................................................... 262
18.2.1 Eigenvectors and Eigenvalues........................................................ 262
18.2.2 Shape Metrics................................................................................. 265
18.3 Describing Structure ................................................................................... 267
18.3.1 Curvature Flow .............................................................................. 267
18.3.2 Medial Axis.................................................................................... 269
18.4 Problems ..................................................................................................... 271

PART V Basis

Chapter 19 Basis Sets............................................................................................................. 275


19.1 Discrete Cosine Transform ......................................................................... 276
19.2 Zernike Polynomials................................................................................... 279
19.3 Empirical Mode Decomposition................................................................. 282
19.4 Image Analysis with Basis Sets.................................................................. 285
Contents xiii

Chapter 20 Pulse Images and Autowaves .............................................................................. 293


20.1 Pulse-Coupled Neural Network .................................................................. 293
20.1.1 Mammalian Visual Cortex ............................................................. 293
20.1.2 PCNN............................................................................................. 293
20.1.2.1 Theory............................................................................ 294
20.1.2.2 Pulse Streams................................................................. 294
20.1.2.3 Applications................................................................... 295
20.1.2.4 Operator Notation.......................................................... 296
20.2 Intersecting Cortical Model ........................................................................ 296
20.2.1 Centripetal Autowaves ................................................................... 297
20.2.2 ICM ................................................................................................ 297
20.3 Texture Classification with the ICM........................................................... 298
20.4 Summary..................................................................................................... 300

Appendix A Operators ............................................................................................................. 303


Appendix B Operators in Symbolic Order............................................................................... 325
Appendix C Lengthy Codes..................................................................................................... 327
Bibliography ................................................................................................................................. 333
Index.............................................................................................................................................. 335
Python Codes
1.1 Positioning Python to the user’s directory ............................................................................. 4
1.2 Positioning Python to the user’s directory ............................................................................. 5
2.1 Corresponding Python outline ............................................................................................... 7
2.2 Swapping the color channels ............................................................................................... 11
2.3 Converting an RGB image to a grayscale image ................................................................. 12
2.4 A few informational operations ........................................................................................... 13
2.5 Computing the average of selected pixels............................................................................ 13
2.6 Determining which channel has the most energy ................................................................ 14
2.7 Isolating the man’s cane....................................................................................................... 16
3.1 Creating an integer............................................................................................................... 21
3.2 Simple math functions ......................................................................................................... 22
3.3 Type casting ......................................................................................................................... 22
3.4 Creating strings .................................................................................................................... 22
3.5 Type conversions.................................................................................................................. 23
3.6 Building a name ................................................................................................................... 23
3.7 Tuple .................................................................................................................................... 24
3.8 Extraction............................................................................................................................. 24
3.9 Slicing selected elements ..................................................................................................... 24
3.10 Using a list ........................................................................................................................... 25
3.11 Using a dictionary ................................................................................................................ 26
3.12 Some dictionary functions ................................................................................................... 26
3.13 Some set functions ............................................................................................................... 27
3.14 A simple if statement ......................................................................................................... 27
3.15 A multiple line if statement................................................................................................ 27
3.16 The if-else statement ....................................................................................................... 28
3.17 The elif statement.............................................................................................................. 28
3.18 A compound if statement ................................................................................................... 28
3.19 A while statement............................................................................................................... 29
3.20 Usgin break command........................................................................................................ 29
3.21 Using the continue command ........................................................................................... 30
3.22 The for loop........................................................................................................................ 30
3.23 Using the range command.................................................................................................. 30
3.24 Using the range command in a for loop ........................................................................... 31
3.25 Using the map and lambda functions ................................................................................. 31
3.26 Choosing parameters in a function ...................................................................................... 31
3.27 Choosing parameters in a function ...................................................................................... 32
3.28 Writing to a file .................................................................................................................... 32
3.29 Reading to a file ................................................................................................................... 32
3.30 Pickling a file ....................................................................................................................... 33
3.31 Reading a pickle file............................................................................................................. 33
3.32 Reading a pickle file............................................................................................................. 33
3.33 Defining a function .............................................................................................................. 34
3.34 Return a value from a function ............................................................................................ 34
3.35 Return a tuple from a function ............................................................................................. 34
3.36 Default arguments ................................................................................................................ 35

xv
xvi Python Codes

3.37 Function help ....................................................................................................................... 35


3.38 Showing help ....................................................................................................................... 36
3.39 Initial commands.................................................................................................................. 36
3.40 Reading a module ................................................................................................................ 37
3.41 Shortcut name ...................................................................................................................... 37
3.42 From import ......................................................................................................................... 37
3.43 Executing commands in either version of Python ............................................................... 38
3.44 Divide by 0 error .................................................................................................................. 38
3.45 Traceback through a module................................................................................................ 38
3.46 Try-except ............................................................................................................................ 39
3.47 Creation of vectors............................................................................................................... 40
3.48 Creating tensors ................................................................................................................... 40
3.49 Accessing data in a matrix ................................................................................................... 40
3.50 Creating random arrays........................................................................................................ 41
3.51 Using a random seed............................................................................................................ 41
3.52 Creating a solid rectangle..................................................................................................... 41
3.53 Creating arrays from data..................................................................................................... 42
3.54 Setting the number of decimal places that are printed to the console.................................. 42
3.55 Converting between vectors and matrices............................................................................ 43
3.56 Math operations for vectors ................................................................................................. 43
3.57 Multiplication with vectors .................................................................................................. 44
3.58 The inner product of two matrices ....................................................................................... 44
3.59 Maximum values in an image .............................................................................................. 45
3.60 Application of several functions .......................................................................................... 46
3.61 Locating the maximum ........................................................................................................ 46
3.62 Using the nonzero function ................................................................................................ 47
3.63 Advanced slicing for arrays ................................................................................................. 48
3.64 Advanced slicing for arrays with multiple dimensions........................................................ 49
3.65 Mathematical functions for an array .................................................................................... 49
3.66 Sorting data in an array ........................................................................................................ 50
3.67 Sorting images according to a user-defined criteria............................................................. 50
3.68 Example of the indices function .......................................................................................... 51
3.69 Creating a solid circle .......................................................................................................... 52
3.70 The Circle function.............................................................................................................. 52
3.71 Loading an image................................................................................................................. 53
3.72 Rearranging the color channels............................................................................................ 53
3.73 Saving an image ................................................................................................................... 54
3.74 An example of melding the operators and functions from ndimage.................................... 54
3.75 Finding the center of mass ................................................................................................... 55
4.1 Loading the image using Python Image Library.................................................................. 59
4.2 Loading the image using commands from imageio ............................................................. 59
4.3 Reducing the intensity resolution......................................................................................... 61
5.1 Creating an image that suppresses the background ............................................................. 69
5.2 Converting between HSV and RGB values ......................................................................... 70
5.3 The vectorize function applies the operation to all pixels................................................... 70
5.4 Modifying the hue channel .................................................................................................. 71
5.5 The RGB to YIQ conversion................................................................................................ 72
5.6 Getting the Cb and Cr channels from the rocket image....................................................... 73
6.1 Using the Window and Plop operators................................................................................. 82
6.2 Demonstrating the Downsample and Concatenate operators .............................................. 83
Python Codes xvii

6.3 Shifting an image ................................................................................................................. 84


6.4 Noninteger shifts.................................................................................................................. 86
6.5 Scaling the image................................................................................................................. 86
6.6 Rotation using scipy.ndimage .............................................................................................. 87
6.7 Multiple rotations................................................................................................................. 88
6.8 Dilation operations............................................................................................................... 89
6.9 The perimeters are created by computing the difference between two dilations................. 90
6.10 Creation of the image in Figure 6.10 ................................................................................... 91
6.11 The RPolar function............................................................................................................. 93
6.12 The IRPolar function............................................................................................................ 93
6.13 The LogPolar function ......................................................................................................... 93
6.14 Finding the perimeter of the cell.......................................................................................... 96
6.15 The Barrel function ............................................................................................................. 97
6.16 An example using scipy.ndimage.geometric–transform .................................................. 98
6.17 An example using scipy.ndimage.affine_transform ................................................ 98
7.1 Starting Dancer .................................................................................................................. 102
7.2 The DelaunayWarp function ............................................................................................ 103
7.3 Reading a CSV file............................................................................................................. 103
7.4 Extracting information from the tessellation ..................................................................... 104
7.5 Finding a simplex............................................................................................................... 104
7.6 Commands to warp an image............................................................................................. 106
7.7 Morphing two images ........................................................................................................ 108
8.1 Testing the eigenvector engine in NumPy ......................................................................... 113
8.2 Proving that the eigenvectors are orthonormal .................................................................. 113
8.3 Projection of data into a new space.................................................................................... 115
8.4 Projection of data into a new space.................................................................................... 116
8.5 The first two dimensions in PCA space ............................................................................. 117
8.6 The ScrambleImage function ........................................................................................... 118
8.7 The Unscramble function ................................................................................................. 119
8.8 Various calls to the Unscramble function ......................................................................... 120
8.9 The LoadImage and IsoBlue functions ............................................................................ 122
9.1 The EigenImages function ................................................................................................ 129
9.2 The ProjectEigen function................................................................................................ 129
10.1 The Rect2Polar and Polar2Rect functions....................................................................... 137
10.2 Forward and inverse FFT ................................................................................................... 139
10.3 The DC term ...................................................................................................................... 142
10.4 Conservation of energy ...................................................................................................... 142
10.5 Computing the original image ........................................................................................... 142
10.6 The shifting property.......................................................................................................... 143
10.7 The script for Equation (10.28).......................................................................................... 148
10.8 Creating the mask .............................................................................................................. 151
10.9 Using the KaiserMask function........................................................................................ 152
11.1 An example of a low-pass filter ......................................................................................... 154
11.2 An example of a high-pass filter ........................................................................................ 155
11.3 An example of a band-pass filter ....................................................................................... 155
11.4 An example of a band-pass filter with soft edges .............................................................. 156
11.5 The Wedge function .......................................................................................................... 157
11.6 An example of line filtering ............................................................................................... 158
11.7 The MaskinF function....................................................................................................... 159
11.8 The MultiWedges function ............................................................................................... 160
xviii Python Codes

11.9 The ColorCode1 function ................................................................................................. 160


11.10 Removal of the screen from the baseball image ................................................................ 162
12.1 Smoothing through a correlation with a small solid block ................................................ 167
12.2 The Correlate1D function................................................................................................. 169
12.3 The Correlate2DF function .............................................................................................. 169
12.4 Correlating shapes.............................................................................................................. 169
12.5 Loading and creating the necessary images....................................................................... 171
12.6 The LocateDock function.................................................................................................. 172
12.7 The Overlay function ........................................................................................................ 173
12.8 The SubtractDock function .............................................................................................. 174
12.9 The IDboats function ........................................................................................................ 174
12.10 The FPF function............................................................................................................... 176
12.11 Testing the FPF function ................................................................................................... 177
12.12 Computing an FPF ............................................................................................................. 178
12.13 The LoadTach function..................................................................................................... 179
12.14 The MakeTachFPF function............................................................................................. 180
12.15 Running the functions in the tachometer problem............................................................. 180
13.1 Shifting a simple array ....................................................................................................... 189
13.2 Extracting the vertical edges .............................................................................................. 190
13.3 Using the Sobel function to create an edge enhancement ................................................. 191
13.4 Application of the DoG filter ............................................................................................. 193
13.5 The Harris function........................................................................................................... 194
13.6 Applying the Harris detector to simple geometric shapes ................................................. 195
14.1 The LineHough function................................................................................................... 200
14.2 Creating Figure 14.1 .......................................................................................................... 201
14.3 Creating Figure 14.2 .......................................................................................................... 201
14.4 Running the Hough transform on an image with a line ..................................................... 201
14.5 The Hough transform applied to a different image............................................................ 202
14.6 Creating a line that is at a different orientation.................................................................. 202
14.7 Circle Hough transform applied to multiple rings ............................................................. 204
14.8 The detection of the cane ................................................................................................... 206
15.1 Adding random noise......................................................................................................... 210
15.2 Smoothing in Python ......................................................................................................... 210
15.3 Salt noise............................................................................................................................ 211
15.4 Applying colored noise ...................................................................................................... 212
15.5 The AddNoise function ..................................................................................................... 213
15.6 The Lopass function .......................................................................................................... 214
15.7 The ErosionDilation function ........................................................................................... 215
15.8 Applying a median filter .................................................................................................... 216
15.9 Applying a Wiener filter .................................................................................................... 217
16.1 Simple texture measure through the ratio of the mean and standard deviation ................. 223
16.2 Compute the edge density.................................................................................................. 223
16.3 Measuring the four moments ............................................................................................. 225
16.4 The FourMoments function.............................................................................................. 225
16.5 Beginning the comparison of textures ............................................................................... 226
16.6 The WvlIteration function................................................................................................ 228
16.7 Creating an output after a single iteration in wavelet decompostion ................................. 228
16.8 The WaveletDecomp function .......................................................................................... 229
16.9 The GetParts function....................................................................................................... 230
16.10 The WaveletEnergies function.......................................................................................... 230
Python Codes xix

16.11 The Cooccurrence function .............................................................................................. 231


16.12 The HHomogeneity function ............................................................................................ 232
16.13 The HContrast function.................................................................................................... 233
16.14 The HCorrelation function ............................................................................................... 234
16.15 The HVariance function.................................................................................................... 234
16.16 The HEntropy function..................................................................................................... 235
16.17 The Haralick function....................................................................................................... 236
16.18 Using the Haralick function.............................................................................................. 237
16.19 The five Law’s vectors ....................................................................................................... 239
16.20 The BuildLawsFilters function ........................................................................................ 239
16.21 The LawsJets function ...................................................................................................... 240
17.1 The GaborCos function .................................................................................................... 244
17.2 The Filts function .............................................................................................................. 244
17.3 The ManyCorrelations function ...................................................................................... 245
17.4 Complete steps to create an image, Gabor filters, and the correlations ............................. 246
17.5 The RandomJets function................................................................................................. 247
17.6 The entire process of gathering correlations, extracting jets, and mapping in
PCA space ......................................................................................................................... 248
18.1 The PerimeterPoints function .......................................................................................... 254
18.2 The ShowPerimPoints function........................................................................................ 254
18.3 The ChainLength function ............................................................................................... 255
18.4 The Curvature function .................................................................................................... 255
18.5 The FourierDescriptors function ..................................................................................... 257
18.6 The ReadFiducial function ............................................................................................... 259
18.7 The RemoveCenterBias function ..................................................................................... 260
18.8 The RemoveRotateBias function...................................................................................... 261
18.9 The RemoveScaleBias function ........................................................................................ 261
18.10 The GridDifference function ............................................................................................ 262
18.11 The Shape1 function.......................................................................................................... 263
18.12 The ExtractStats function................................................................................................. 263
18.13 Computing four geometric values...................................................................................... 266
18.14 The metrics for the six shapes............................................................................................ 267
18.15 The CurveFlow function ................................................................................................... 268
18.16 Running iterations of curvature flow ................................................................................. 269
18.17 Computing the medial axis ................................................................................................ 270
19.1 Using the 1D discrete cosine transform ............................................................................. 276
19.2 An example of a 1D DCT .................................................................................................. 277
19.3 The dct2d function ............................................................................................................ 278
19.4 The idct2d function ........................................................................................................... 278
19.5 Modified 2D EMD ............................................................................................................. 284
19.6 Reconstruction ................................................................................................................... 285
20.1 The original PCNN Python class ....................................................................................... 295
20.2 Typical execution of the PCNN ......................................................................................... 295
C.1 Programs to convert RGB to XYZ and then to CIE L*a*b* ............................................. 327
C.2 The Zernike function ........................................................................................................ 328
C.3 The Plop function .............................................................................................................. 329
C.4 The Warp function ............................................................................................................ 330
C.5 The KaiserMask function ................................................................................................. 331
Visit https://textbookfull.com
now to explore a rich
collection of eBooks, textbook
and enjoy exciting offers!
Preface
Image processing and analysis is a burgeoning field that is gaining renewed interest in recent years.
The need for image analysis tools is ever increasing. Along with this is also the need to be able to
efficiently and explicitly describe processes used in analyzing images. Unfortunately, the current
state of publications is that each author has their own way of describing processes. Two different
authors describing the same process will often provide vastly different ways of communicating their
proposed process.
The recent development of high-powered scripting languages such as Python compounds the
issue. Publications can consume more real estate in explaining the process than it takes to write
the Python script to execute the process. Furthermore, the descriptions can be imprecise, because
some authors prefer to describe their processes through textual descriptions. Readers attempting to
replicate their results may find it a difficult process as not all of the steps are clearly explained.
The purpose of this text is to provide a unified mathematical language that coincides with Python
scripting. Image operators represent processes in a image analysis sequence, and these are associated
with Python scripts. Thus, a concise mathematical description of a process is easily translated into
Python scripts through this correlation. The conversion of Python scripts to image operators is nearly
as easy. Thus, this text introduces the initial set of image operators, complete with associated Python
scripts and examples.

Jason Kinser, D.Sc.


George Mason University
Fairfax, VA, USA
[email protected]

xxi
Software and Data
Software and data used in this text are available at:
https://jmkinser49.wixsite.com/imageoperators
Software and images copyright (c) Jason M. Kinser 2018. Software and images provided on this
site may be used for educational purposes. All other rights are reserved by the author.

xxiii
Random documents with unrelated
content Scribd suggests to you:
We arrived at a large building, with lattice windows several feet
from the ground, surrounded by a high wall enclosing what appeared
to be farm-buildings, with a large double gate where carts and cattle
could pass. After knocking loudly, an old Turk appeared. Telling him I
was an English traveller and had lost my way, I begged to have
shelter for the night anywhere in the farm-yard; a feed for my horses
and some bread and coffee, if nothing else in the way of food was
procurable, for myself and servant, for we were very hungry.
The Turk replied that his master was ——— Bey, who had
formerly been in the service of the Porte; that he was a landed
proprietor; that as his family dwelt with him, no man could be
admitted into the house; but he offered to ask the Bey’s permission
to allow us to pass the night in one of the outhouses and to put up
our horses.
The gate was again closed, and after waiting a few minutes, a
Turkish gentleman, dressed in a handsome fur pelisse and fez,
appeared.
After the usual salutations, he said, ‘Are you an Englishman?’ I
replied that I was, without making myself known as one of the
Secretaries of the British Embassy. He bade me welcome in a hearty
manner, and turning to the old man who had just opened the gate,
directed that my attendant should be lodged in a room in the farm-
buildings and given whatever he might require, and that the horses
should be stalled and fed.
Then taking me to the door of his house and opening it with a
large key, he stopped on the threshold and said, ‘You are an English
gentleman, and therefore a man of honour. I am about to do that
which no Mohammedan will or ought to do, and admit you to my
harem amongst my family. I have heard how English gentlemen visit
the houses of friends and live as men of honour with their families,
without restraint. I shall do the same, for I have special reasons for
my conduct, which I will relate when you have rested. It is my
earnest hope that you should feel as if you were with one of your
own countrymen; but I beg you to keep secret from every one your
visit to my house, and never to mention whom you may see within it.’
He then led me up a narrow staircase into a well-lighted room,
handsomely furnished with beautiful carpeting, comfortable divans,
mirrors, Turkish tables, arms hung on the wall, and a couch with
pretty embroidered cushions and silk quilt, which he said was to be
my bed.
Again and again he bade me welcome, adding, ‘I shall leave you
to repose—you must be hungry. Supper is ordered. A pipe and
coffee will be brought to you, order what you please;’ then as he
withdrew he repeated again in a kind manner, ‘An English Effendi is
always a man of honour.’
Whilst inspecting the room and wondering what all this meant, I
heard a gentle step, and a tall graceful figure of a girl about
seventeen entered. She was dressed handsomely in a jacket used
by Turkish ladies, with a bodice open in front, like the square dresses
now worn by English ladies of an evening. She had on yellow silk
‘shalvas’ fastened by a white muslin sash, the ends of which were
prettily embroidered. Her complexion was olive, with very large dark
eyes and long eyelashes; her nose aquiline, and her mouth like a
ring set in ruby lips. She looked grave and sad, but blushes diffused
her cheeks as she bowed gracefully, and with a sweet smile put a
‘chebúk’ to my mouth, and then retired. Her hair was braided in
tresses around her head and adorned with coins. Two long braids
hung down her back.
This vision of a Turkish maiden seemed like a dream, and whilst
pondering over the pretty figure that had just left and wondering
whether she would return, another damsel appeared bearing a cup
of coffee in a ‘finjan’ studded with precious stones. Bending before
me, she put it on the little table.
She appeared to be about fifteen, dressed like the bearer of the
‘chebúk,’ but of a fairer complexion, with dark blue eyes, her nose
retroussé. She was not so demure in her looks or manner, and
standing before me blushing and smiling with a mirthful expression,
said in a very sweet voice, ‘My father bids me ask if there is anything
you wish for, and to say your supper will soon be ready.’ After
thanking her, I held my tongue, remembering I was an ‘honourable
man.’ She retired, turning at the threshold to look at me, with a pretty
smile of mischief. Shortly afterwards the elder damsel reappeared,
bringing sherbet. I thanked her, and she bowed and withdrew.
Then the host followed to announce that supper was ready and
inquiring whether I had been properly attended to, led me to a lower
room, remarking that he thought I should be better able to enjoy my
repast without his presence, but that he hoped in the evening to
converse with me.
During the supper I was waited on by both the fair maidens, who
brought me in succession a number of savoury dishes, with fruit and
sweets of all kinds, for which the Turks are famous.
I partook of everything largely, to the evident amusement and
pleasure of the maidens. The elder was no longer so demure in her
manner, and the eyes of the younger sparkled with fun as she waited
on me; but I indulged in no conversation further than to thank them
now and then, saying ever to myself, ‘my host says I am an
honourable man;’ but I fear my looks betrayed my admiration.
After supper the Bey conducted me to my apartment, where
coffee was brought to us by the damsels, both of whom, I learnt,
were his daughters. I expressed to the Bey my warmest thanks for
his hospitality, and for the great confidence he had shown by
admitting me amongst his family. Upon this, he said he would relate
why he had broken through the Mohammedan custom and usages
and bidden me welcome in his harem. He was fulfilling a vow made
years ago, that whenever he had an opportunity, he would
endeavour to give proof of his gratitude for kindness received from
the captain of a British merchant vessel.
‘When I was a young man,’ continued the Bey, ‘before I was
married, I went on a pilgrimage to Mecca. On my return I embarked
from Alexandria on a Turkish vessel bound to Constantinople. We
encountered a heavy gale; the vessel was old and rotten; leaks were
sprung, and the captain, crew, and myself who was the only
passenger, had barely time to get into the ship’s boat, when the
vessel sunk. I lost all my clothes and money, with the exception of a
few piastres. We expected every moment the boat would be
swamped by the heavy seas breaking around us, when a ship hove
in sight. Signals of distress were made, and she came to our
assistance, and we were all taken safely on board. She proved to be
an English vessel bound for Salonica. The captain, a kind-hearted
but rough-looking sailor, gave us dry clothes, dressed me in a warm
suit of his own and supplied us with food.
‘We arrived at Salonica and I was enabled through the pratique
master, who understood a little English, to express my gratitude. I
offered to pay for my passage and food. The captain was indignant,
and said he would not accept a farthing; but, on the contrary, having
learnt that I had no money to continue my voyage and had no friends
at Salonica, put a small sum into my hands which would enable me
to proceed to Constantinople.
‘You,’ he continued, ‘are the first Englishman to whom I have had
an opportunity of showing feelings of gratitude, long pent-up, to your
countryman who saved my life.’
I then told him that I was one of the Secretaries of the British
Embassy at Constantinople, and what had been the object of my
visit to Broussa. I said I should make known to the ‘great Elchi’ his
hospitality and kindness. He again impressed upon me his anxious
wish that I should keep my reception in his household a secret, and,
above all, the fact that I had been waited on by his daughters—for he
said it would be a serious matter if this was known to his co-
religionists. He consented, however, to my telling the Ambassador
confidentially all that had happened; but to my Turkish friends at
Constantinople I was only to mention that I and my servant had
received shelter for the night. He also requested me not to tell my
Greek attendant that I had seen any women in the house.
At sunrise next day I was up, and going into the courtyard gave
directions to my servant to have the horses ready for a start as soon
as I had breakfasted. He informed me that he had been well taken
care of. I gave the Greek several gold piastres, which I directed
should be distributed amongst the dependants of the Bey. He
informed me that he had learnt from the old gatekeeper that the Bey
had only one wife, and no other inmates of the harem except his two
daughters and some black slaves.
A good breakfast was ready for me as I re-entered the house, and
again the pretty damsels waited on me without the presence of their
father; and though I had lost my heart (it was an easy matter in those
days) to the blue-eyed little maiden, I refrained from saying more
than expressions of thanks in the most polite Turkish, keeping
steadfastly in mind that an ‘English gentleman is an honourable
man.’
On going away, the Bey accompanied me to the door, and whilst I
reiterated my warmest thanks, he put into my hand a little sealed
packet, observing, ‘You will pardon me for returning the handsome
“bakshish” you had directed the Greek to distribute amongst my
dependants. The latter have made known and returned to me what
they had received; I shall reward them, but I cannot allow that you
should do so. It would have given me,’ he added, ‘great pain if they
had retained the money, and it would have deprived me of the
pleasure and satisfaction I have felt in welcoming an Englishman to
my house.’ I said not a word, and put the money into my pocket. As I
left the house I could not help looking back as long as the lattice
windows were in sight, and thought I espied bright eyes peering out
at the parting guest; but I refrained from waving hand or
handkerchief.
Sir Stratford Canning, to whom I related this adventure
confidentially on my arrival, made known to the Porte that I had
received hospitality and great kindness from this Bey when
benighted on my return from Broussa, and expressed a hope that
the Porte would in some suitable form mark approval of such
kindness shown to a member of the Embassy.
The Turkish Government announced their satisfaction and thanks
for the report I had presented, through the Ambassador, giving the
result of my inquiry into the conduct of Pasha and Consul, and sent
to H.E. a Sultan’s ‘berat’ or edict, placing the Bey under the special
protection of the Porte and of His Imperial Majesty, and
recommending him to the good offices of the Pasha and other
officials.
This ‘berat’ I forwarded in a letter to the Bey; but, alas! I could not
send the messages I should have wished to have done to Fatima
and the ‘dil bere’ (heart-robber) Aisha.
That year Sir Stratford Canning, accompanied by all his family
and all the members of the Embassy except myself, who was left in
charge for a few days, made an excursion to Broussa and were
received with great attention and hospitality by the Pasha. In 1844
the latter was removed from his government and returned to
Constantinople, where he resided in a large kiosk on the Bosphorus.
In the summer of 1844, having obtained leave of absence from
Her Majesty’s Government, I made arrangements to embark in a
French steamer bound for Marseilles.
On taking leave of the Ambassador, he told me he had ordered
his ‘kaik’ to convey me from Buyukdere, where his Excellency then
resided, to the steamer in Pera harbour, but that he was anxious I
should call on the ex-Pasha of Broussa and present to him a gold
chronometer, worth about £60, as a token of his—Sir Stratford’s—
friendship, and acknowledgement of the hospitality shown to himself
and family on his visit.
His Excellency added, ‘I am especially anxious you should
present this gift, and renew your acquaintance with the Pasha, and
thus remove any feeling that might possibly exist in his mind
regarding the inquiry made by you into his and the Consul’s conduct,
and the decision that was come to by the Porte in consequence of
your report; for the Pasha will probably be employed again by the
Government, and when you return to the Embassy it is desirable that
you should both be on friendly terms.’ In pursuance of these
instructions I called on the Pasha, who received me very kindly and
told me he had a lively recollection of my visit to Broussa, and of my
statement in the report that both he and the Consul were in the
wrong and had been quarrelling upon trivial matters.
He laughed and added, ‘You were quite right; the Consul and I
made it up and became good friends, so I feel indebted to you for not
having unduly taken the part of your Consul.’
I presented the chronometer, with a suitable message from the
Ambassador, and then told the Pasha I could not wait for the usual
pipes and coffee, as I had to embark in a steamer which was about
to start. He replied that he should only detain me for a moment, and
left the room. He returned immediately, bringing a small green
leathern case, suspended in a little muslin kerchief, which he put into
my hands, saying, ‘You are going to your own country and you may
not return, so I beg you to keep this as a little souvenir of my
friendship.’
Though I knew not what were the contents of the leathern case, I
thought by the shape it was a ‘finjan’ or Turkish saucer for holding a
small cup; but as it is against the established regulations for a
diplomatic officer to accept presents from a foreign official, I told the
Pasha my scruples, and that it would affect me injuriously if I
accepted even the smallest gift.
He said all he could to induce me to waive my objection; but
finding me resolute, he became very grave, took the little muslin
kerchief containing the case from me, as I held it towards him, and
handed me back at the same time the case containing the watch,
saying that ‘if a Secretary of the Embassy cannot receive a little
token of friendship on going away, from a man who no longer holds
any appointment, neither can I, a retired Governor, accept this
chronometer from the Ambassador; have the goodness to make this
known to his Excellency, with my best thanks and excuses.’ As I felt
that Sir Stratford would be excessively annoyed if the gift were
returned, and might think I had not managed to present the gift in a
proper manner, I came (after some parley) to a compromise with the
Pasha, that he should retain the chronometer and I his gift; that I
should write a note at once to the Ambassador explaining all that
had happened, and dispatch it to his Excellency by the ‘kaik’ that
brought me; that if the Ambassador disapproved of my accepting the
gift I should be allowed to return it to the Pasha, with a letter of
explanation, and in such case he would retain the chronometer; but if
his Excellency approved, there was an end of the matter, and under
any circumstances, I told the Pasha, I was much pleased and most
grateful for his kind intention.
I sent off the ‘kaik’ to the Embassy, and proceeded in another to
the French steamer, which was about to leave. On getting into the
‘kaik,’ I opened the little leather case. It contained a small gold
‘finjan’ encrusted with rose diamonds, worth about £70.
Just as the steamer was on the point of leaving, the
Ambassador’s ‘kaik’ came alongside, with a messenger bringing a
note from Lady Stratford Canning. The note stated that the
Ambassador entirely approved all I had done, and directed that I
should keep the Pasha’s gift.
On my way to England I stopped at Paris for a few days to make
the acquaintance of Admiral Lalande, who had commanded the
French fleet which had been sent to Besika Bay during the Egyptian
question in 1840. He was married to the sister of my brother-in-law,
the late M. Mauboussin.
The Admiral received me very cordially. He was looking ill, and
told me it was probable that he would be in his grave before the end
of three weeks, as he was suffering from an internal disease and
lived entirely upon milk. He was not confined to his couch but walked
about the room whilst conversing, as if full of vigour both in mind and
body. He asked me if I should see, whilst in London, Lord Ponsonby;
saying he was very anxious to send him ‘the message of a dying
man.’ I replied that I should make a point of calling on his Lordship,
from whom I had received much kindness. The Admiral then
observed that he entertained the highest opinion of Lord Ponsonby,
though he was aware that he had successfully opposed French
views and projects in Egypt, and had assumed an ascendency over
the minds of the Sultan and his advisers which redounded to his
credit as a diplomatist, though antagonistic to France.
‘From the fact,’ said the Admiral, ‘of my having such a high
opinion of the character of your Ambassador, it has been very painful
to me to have learnt, from communications which have been
imparted to me by my Government, that Lord Ponsonby is under the
impression that I took a prominent part in inducing the Turkish
Admiral to be a traitor to his sovereign and deliver over the Turkish
fleet to Mehemet Ali. It was of course,’ he said, ‘an event to which
much importance was attached by those who had desired to support
Mehemet Ali’s independence; but,’ he added, ‘no possible advantage
to French interests would ever have induced me to advise or
encourage any man to turn traitor to his sovereign, and I hold the
Turkish Admiral in utter contempt for that act of infamy. I am now,’ he
continued, ‘as I have told you, a dying man; in a few days you will
hear I have passed away, and I desire that you should convey to
Lord Ponsonby the following message:—“I swear, as a dying man,
that whatever may have been done by other French officials, I took
no part in the matter, nor indeed was I aware, until the Turkish fleet
was delivered over to Mehemet Ali, of the intention of the Turkish
Admiral.”
‘It is my anxious desire, as I have the highest opinion of Lord
Ponsonby as an honourable man, that any erroneous impression on
this subject should be removed from his mind before I die, and that
he should give me credit also for being an honourable man, and
incapable of counselling any one to turn traitor to his sovereign.
‘Take his Lordship,’ he added, ‘this message, and let me know
before I die whether he gives credence to my declaration.’ This I
promised to do.
Admiral Lalande related to me that, during the time the French
and British fleet lay together in Besika Bay, he had become very
intimate with Admiral Sir Robert Stopford, that they dined frequently
together and had become fast friends.
He observed that I was no doubt aware that it was then expected,
at any moment, that a declaration of war would take place, and that
an engagement would follow between the two fleets. ‘It is all settled
now,’ he said, ‘and we are at peace, so I can tell you confidentially
that we two old men talked over the probability of a sudden
declaration of war one evening after dinner, and as we each
expressed a sincere desire that no undue advantage should be
taken by either through receiving earlier tidings of a rupture, we
concerted that a private signal should be hoisted on our respective
flagships, the object of which should be unknown to the officers of
our fleets, when either of us received tidings that war was declared,
so that each might be prepared, without undue advantage, to take
measures for a fair fight. Every morning and evening we were wont
to look for this signal. At that time,’ the admiral continued, ‘the
French fleet was in first-rate order, and we had one vessel more than
the English at anchor, as the latter had a vessel or two on the coast
of Egypt.
‘Your fleet,’ he said, ‘was also in admirable order, but we were
quite your match; and I tell you frankly that though I have no
unfriendly feeling towards your nation, I die a disappointed man in
that I lost the opportunity of a fight; for I had hoped, if not victorious,
to have been able to wage such a battle as would have wiped out
the defeats our squadrons and ships had almost always experienced
in the last great war.’
Lord Ponsonby was in town when I arrived; he took the greatest
interest in the message I brought him, and requested me to inform
Admiral Lalande it was perfectly correct that he had been led to
believe he had induced, or secretly encouraged, the Turkish Admiral
to deliver up the fleet to Mehemet Ali; but that Admiral Lalande’s
declaration was sufficient to convince him that he was mistaken, and
that he greatly regretted having joined with others in putting forward
such an accusation. He requested me also to say that he was much
pleased and gratified that the Admiral should have desired to have
this matter cleared up, and told me to thank him and to express a
hope that he would yet live for many years to serve his country.
I wrote to Admiral Lalande and made known Lord Ponsonby’s
reply. My letter reached him a few days before his death, which
occurred within the three weeks, as he had prognosticated.
In the same year I was directed, by order of Her Majesty’s
Government, to accompany Colonel Barnett to Egypt, on his
appointment as Political Agent and Consul-General, and remained
there several months. After a few weeks’ residence at Cairo, I was
offered by Lord Palmerston, through Colonel Barnett, the post of
Consul at Alexandria, which the latter endeavoured to persuade me
to accept as he urged it would lead to my being appointed his
successor; but the climate of Egypt did not agree with me and I
declined, preferring to return to the Embassy at Constantinople.
Commodore Porter was at this time Minister of the United States
at Constantinople.
He was a distinguished officer, who had rendered important
services during the war with Great Britain. The commodore was very
eccentric, a type of the rough sailor of by-gone days, but pleasant
and amusing, and, when spinning yarns about actions between
British and United States ships, always careful to avoid—even when
the story related regarded the capture by himself of one of our ships
—any expression which he thought might wound my susceptibilities
as a ‘Britisher.’
He lived at San Stefano, a village about ten miles from
Constantinople. I had made the acquaintance of his nephew, Mr.
George Porter, the Secretary of the United States Legation, who
frequently invited me—when there was a passage of quail—to a
day’s shooting and to dinner with his uncle; but I was the only
member of the Diplomatic Corps at Constantinople thus favoured.
Since he had presented his credentials to the Sultan, and made
the usual formal visits to his colleagues, he called upon no one—not
even upon the Vizir or any member of the Turkish Government.
One day, after dinner, I happened to relate to the Commodore a
political event that had recently occurred, in which he appeared to
take great interest; so, finding him in good humour, I took the liberty
of observing that, as he had mentioned he never visited or received
visits from members of the Turkish Government or of the Diplomatic
Corps, I thought he must find it a difficult matter to keep his
Government properly and correctly informed upon passing events,
which were at that time of the greatest importance to the political
world.
The Commodore replied, his eyes twinkling with humour, ‘I am
very careful to keep my Government fully informed of all that takes
place, and I receive replies expressing satisfaction with my
interesting reports and the foresight they declare I show in predicting
events which are likely to happen.’
‘Now,’ said the Commodore, ‘I will make known to you, in the
greatest confidence, how I acquire the information which enables me
to draw up those very able reports. I take Galignani’s Messenger,
which reaches me regularly, and this paper—as you know—contains
extracts from the English and foreign journals, with reports from their
correspondents at Constantinople, regarding the various questions
which are taking place and other occurrences of a political character.
I have all these under my careful consideration, and, assisted by the
local knowledge of my nephew, draw my conclusions and transmit,
with some slight alteration in language, copies of the articles which
appear in the Galignani. I may be sometimes three or four days later
than my colleagues in forwarding reports of passing events to our
respective Governments, but I flatter myself that the digest of the
views entertained by the able reporters at Constantinople is
preferable to, and less likely to mislead the United States
Government than the reports which many of my excellent
colleagues, carried away sometimes by personal motives, may
transmit to their Governments.’
CHAPTER VI.

MISSION TO TANGIER.
In 1844 Mr. Hay went to England on leave, and visited also
Stockholm and Copenhagen. At this latter capital he met the ‘fair girl’
who was to be his future wife, as Leila had predicted. Whilst in
Stockholm, he was presented to King Oscar by our Minister, Mr.
Cartwright, and in the course of conversation with His Majesty about
Morocco, pointed out the advisability of abolishing the old
Convention between Morocco and Sweden, and Morocco and
Denmark, which stipulated that $25,000 (£5,000) should be paid
annually to the Sultan, in order that vessels under the flags of these
two nations should pass the Straits unmolested by Moorish cruisers;
these cruisers having virtually ceased to exist, though the
Convention remained in force.
A rupture of relations between France and Morocco was at this
time imminent, and Mr. Hay’s father, then Political Agent at Tangier,
had been sent, with the knowledge of the French Government, to the
city of Marákesh on a mission to endeavour to induce the Sultan to
accept the French demands. On hearing of this expedition Mr. Hay
wrote to Lord Aberdeen, who was then Secretary for Foreign Affairs,
to offer his services temporarily in Morocco. This offer was accepted.
That Mr. Hay, while at Constantinople, had gained the kindly
opinion of Sir Stratford and Lady Canning may be gathered from the
following letter written to him after his departure from Constantinople,
when Lady Canning learnt that he had been sent to Tangier. The
note was accompanied by the gift of a beautiful cushion in Turkish
embroidery.

You must not leave Constantinople, my dear Mr. Hay, without some little
memento from me to remind you in future days of our life spent together on the
Bosphorus in which, though it may have had some cloudy moments, I hope the
bright ones have preponderated and will alone be remembered by you. We shall
miss you sadly; for your labours have not been thrown away on Sir Stratford, and
you have helped to keep us all in good humour with our neighbours, and for all this
I thank you much. Let us hear of you often, and believe that we shall feel interest
in all that concerns you.
Yours very sincerely,
E. C. Canning.

Mr. Hay arrived at Tangier shortly after the bombardment of that


town by the Prince de Joinville. Notwithstanding the promises made
by the French Government that hostilities should not be commenced
until his father returned from the Court, where he had actually
succeeded in obtaining consent to the chief demands of the French,
the Prince had bombarded Tangier. This unexpected outbreak of
hostilities placed in jeopardy the life of the elder Mr. Hay, who was
still in the interior and who had to pass, on his return from Marákesh
to Tangier, through districts inhabited by wild tribes.
Some of the difficulties with which his father had been confronted
in dealing with the Sultan are touched on in the following letter
written on the return journey from his mission to Marákesh, dated
Camp on the Wad Nefis, July 26, 1844:—

’Tis a sad thing that all folks in Europe, my masters in Downing Street may not
be excepted, have hardly any just conception of the difficulties of my position. It
would take a volume—not small—to relate the bother and the tricks and bad faith
with which I have had to contend—and as to going fast, as Mr. Bulwer has
everlastingly urged, who among mortal men can make Moors go fast, nay, nor
hardly move at all—in the straight path of honour and sound policy? . . .
Alas! I know not what to think. I had hoped the French would have waited until
my report reached Tangier or myself arrived there and told them all. So they are
now preparing to cast fire and the sword on this unhappy country of ignorant
barbarians.
The Moors are mere children, vain children; obstinate, through a shocking
bigotry and ignorance scarcely credible. They have, I believe, had at least two
collisions with the French on their frontier; but all their acts of folly were, I am
certain, without authority. Alas, again, for the poor Sultan; he cannot manage his
own people! If the war do burst forth here, when shall it end? There will be an
internal revolution forthwith, I am almost sure! And drivellers in pomposity and self-
sufficiency would ever publish that all was well.

The elder Mr. Hay did not long survive the effects of the journey,
with all its worry and vexation; but succumbed shortly after his
return, to low fever and other complaints. During his illness, which
lasted several months, Mr. John Hay was directed by Lord Aberdeen
to take charge of political affairs in Morocco, whilst Mr. H. Murray, the
Consul, conducted the consular duties.
The crisis was one of considerable importance. In addition to the
internal difficulties of Morocco, questions with foreign Powers
embarrassed the Sultan’s Government. Denmark and Sweden had
sent squadrons in this year to Moorish waters, demanding the
abrogation of the treaty referred to in Mr. Hay’s audience of King
Oscar.
The Spanish Government had also a question pending with
Morocco regarding the neutral ground and frontier of Ceuta; and, for
the settlement of this question, Sir Henry Bulwer, then H.B.M.’s
Minister at Madrid, had been appointed special Plenipotentiary.
In the following letter to his late chief at Constantinople Mr. Hay
gives an account of the state of affairs which he found on his arrival
at Tangier.

Sept. 12, 1844.


My dear Sir Stratford,
I received yesterday Y.E.’s kind letter of 27th ult.
Your Excellency will no doubt have learnt, both from H.M.’s Government and
the newspapers, accounts of passing events in this country, so I only relate the
more recent that I have witnessed.
The day before yesterday the French squadron arrived, consisting of two line-
of-battle ships and five steamers, having on board the Prince de Joinville, Mons.
de Nion, the French Chargé d’Affaires, and the Duc de Glücksberg (Decazes), an
adjunct Plenipotentiary sent for the purpose of meeting the Moorish
Plenipotentiary, Sid Buselham Ben Ali, to arrange the conditions for peace. I
received, the same morning, a letter from Mr. Bulwer acquainting me with the
nature of the French demands, which proved to be identic with those already
granted to my father by the Sultan of Morocco during his late mission.
The Moorish Plenipotentiary, Sid Buselham, has received orders from the
Sultan to be guided by our counsels in replying to the various demands of the
French. I accordingly went to see the Sid and made known to him the nature of the
French demands, telling him they were just and such as could be granted without
lowering the dignity of the Sultan. I pointed out the proper answers to be made,
and urged him to settle the matter the very day that the demands were presented;
and thus it happened that three hours did not elapse from the time they were
made, until the French flag was hoisted and flying at the Residence of their Chargé
d’Affaires and was saluted by the batteries.
The substance of the demands was as follows:—
‘That Abd-el-Kader be considered as a common enemy and, if taken by either
party, be confined in a State prison at some distant port. The frontier to be marked
out as in the time of the Turks. The withdrawal of the French troops from Ujda,
except 3,000 men. A new treaty to be made embracing the above conditions, and,
when ratified, Ujda and the Island of Mogador are to be given up by the French
and all prisoners exchanged and set free.
The question that may now be asked is—What has been the object of the
French in all this? For their demands remain the same and the concessions are
the same as before the war: and although they say the Sultan is faithless, they
never gave time to test whether he would be so or not, after having pledged
himself to a British agent to act with good faith—but this, it strikes me, is the sore
point.
French supremacy is aimed at, throughout Eastern and Western Barbary, and
an arrangement effected through the good offices of a British agent militates
against that supremacy.
The foolish language of a British officer high in rank, on the other side of the
water, declaring that England would never allow a gun to be fired at a Moorish
port, roused the worst feelings towards us throughout the French squadron,
participated in by the Prince; so, five hours before my father’s arrival from the
Moorish Court (although hourly expected, and feeling that to bombard Tangier after
hearing vivâ voce that the Sultan had granted their demands, would be un peu trop
fort), having bombarded Tangier in the presence of two British ships of war and, I
may say, of our garrison at Gibraltar, off they go to Mogador—the mouth, as the
Moors call it, of British commerce with Central Africa, where we have a
considerable trade. They destroy the forts, and the destruction of the town is
completed by the wild tribes, who burn, pillage, and murder, committing barbarities
on a par with the wanton and uncalled-for proceedings of the French. After striking
this blow at British trade, the French embark and return here to make peace!!
Well it is, that peace is made; for the country is in a state of revolution; the
Sultan totters on his throne, and in a few weeks such a state of anarchy would
have ensued that no Europeans could have remained in the country. There would
have been no Government to treat with, and of the five millions of people, only
robbers and pirates would have come to the front.
How would England have liked this? How would other countries? How would
France?—pledged as she is to us not to take possession of any part of Morocco.
What would she gain but to have roused a spirit of revenge amongst these wild
inhabitants of a country capable of maintaining ten Abd-el-Kaders, as soon as they
learn how to war with disciplined armies? An army of 20,000 men, well disciplined,
might march from one end of the Empire to the other, but to hold the country
200,000 would not suffice.
The French interest, therefore, was not to have weakened the Sultan’s power,
but to have given him time to put in execution his promises and to have helped him
in so doing, if required; but the shaft was shot at ‘Albion la perfide’—Albion, whose
agent here, ever since the conquest of Algiers, has been instructed to hold, and
has held, but one language, that of urging the Sultan not to give ground of offence
to his powerful neighbour, and above all not to support or mix himself up with Abd-
el-Kader.
The Spanish affair is also concluded. My father brought back very full
concessions, and on Mr. Bulwer’s arrival at Gibraltar, with full powers from Spain,
all matters were settled at once by the Moors, and I had the satisfaction of having
used my humble efforts in effecting this.
The Danish and Swedish affairs are in a fair way of being settled amicably, and,
although I must not blow my own trumpet, yet I am sure your Excellency will be
pleased to learn that I have gained some credit at home for the part I have taken in
these affairs which, in consequence of my father’s serious illness, have been
entirely under my guidance, as being the sole medium of communication both
verbally and in writing. I can assure your Excellency that I daily feel the benefit
reaped from the excellent school of diplomacy in which I passed my probation in
the East; and if I have been of use to Her Majesty’s Government, the lessons I
there learnt have been my guide; if I have failed, it has been my own fault.
There is one more remark which I wish to add—that I look upon Morocco as a
field upon which there will often be like cause for anxiety to Europe, and especially
to Great Britain; and how can it be otherwise when we consider the conflicting
characters of the people on the frontier? Such being the case, it becomes more
urgent than ever that some understanding be come to with France by England, for
preserving the integrity of this Empire, and that their agents here should be
persons that act up to the peaceful spirit of their instructions—otherwise a bone of
dissension will ever be found in West Barbary.

Owing to his father’s illness and subsequent death, the settlement


of the complicated questions alluded to in this letter devolved on Mr.
Hay. In this task he acquitted himself with credit, as is proved by the
satisfaction of his official chief at Madrid, and the recognition of his

You might also like