Pythondoc
Pythondoc
Python Scripts
D.1 Installation
The Python interpreter is pre-installed on Mac OS and Linux. The latest
Python 2.7x can be obtained for most operating systems from http://www.
python.org/
471
472 Python Scripts
All Python scripts which accompany this book, including the auxil package,
can be downloaded or cloned from the GitHub repository, see the links on
http://ms-image-analysis.appspot.com/static/homepage/software.html
or on
http://mcanty.homepage.t-online.de/software.html
The IR-MAD script iMad.py requires the dynamic library prov_means.dll
(Windows) or libprov_means.so (Linux, Mac OS). Compiled versions for 32-
bit and 64-bit Python (Windows) and Python on 32-bit and 64-bit Linux are
included with the software, together with the source code prov_means.c. The
libraries should be placed in the OS path, e.g., C:\Windows or /usr/lib.
To install the auxil package, open a console in the unpacked directory and
type the following:
python setup.py install
The scripts themselves are organized according to book chapter and can be
run from the command line. Convenient environments are idle (included in
most Python distributions) and ipython; see http://ipython.org/.
D.1.2 Eclipse
For those who wish to program the examples given in the exercises, or mod-
ify/improve the scripts provided here, Eclipse (http://www.eclipse.org/) to-
gether with the plug-in Pydev (http://pydev.org/) provide an excellent, plat-
form-independent Python programming development environment, very sim-
ilar to that for IDL. The environment includes syntax highlighting, code com-
pletion and debugging.
the freely available VMWare Player to host a Linux virtual machine and run
Docker or, alternaively, install boot2docker from the Docker website.
From Ubuntu, for example, assuming Docker is installed, run
sudo docker run -d -p433:8888 -v my-dir:/crc/imagery
--name=crc mort/crcdocker
to download a Docker image and start a container in daemon mode serving
the iPython notebook kernel. Here my-dir is the location of your images on
your host machine. The -v option shares this directory with the directory
/crc/imagery in the running container. Any changes made in the container
will be immediately reflected in the host directory.
Point your browser to http://localhost:433 to see the iPython notebook
home page. Then open a new notebook and get help for the various scripts
with
• etc ...
The bash script /crc/normalize in the container can be used for automatic
radiometric normalization of Landsat TM full scenes. The script takes four
required parameters and one optional one:
1. the spectral band to use for warping (registering) the target image to
the reference image
For example:
!./ normalize 4 [1,2,3,4,5,7] reference.tif target.tif [500,500,2000,2000]
Similarly, the bash script c-correction.sh will run solar illumination cor-
rection with the c-correction method discussed in Chapter 5. The input pa-
rameters are:
1. the spatial subset to include in the calculation
For example:
!./ c-correction.sh [0,0,1000,1000] [1,2,3,4,5,7] 3 135 57 ms-image dem-image
D.2 Documentation
D.2.1 Utilities
The auxil package contains the following modules:
auxil.auxil.py
A collection of auxiliary routines for processing multispectral imagery.
auxil.congrid.py
Arbitrary re-sampling of an array to new dimension sizes. Mimics the
CONGRID() function in IDL.
auxil.header.py
Defines an object class representing the text fields of an ENVI format
header.
auxil.png.py
A pure Python PNG coder/decoder, see http://pythonhosted.org/pypng/
png.html
auxil.polsar.py
Defines an object class to store fully polarimetric SAR data in multilook
covariance matrix form.
auxil.supervisedclass.py
Defines object classes for supervised image classification: Bayes maximum
likelihood, neural networks with backpropagation and scaled conjugate gradi-
ent training, and a support vector machine.
The user is first queried for a working directory, image filename and a
training sample size. If the latter is 0, then 100 representative training pixel
vectors are chosen by the k-means algorithm. Otherwise, a random sample of
the desired size is used. Next, the number of kernel principal components to
retain is entered and the destination output file selected. The user can then
choose between a linear or Gaussian kernel. The Gaussian kernel parameter
γ is calculated as γ = 1/(2σ 2 ), where σ = hkg(ν) − g(ν 0 )ki is the average
Euclidean distance between the training observations. Finally, the output
destination is selected. After centering on the training data and diagonalizing
the kernel matrix, a plot of the eigenvalues is displayed and the projected
image is stored to disk.
The user is queried for the working directory, the (spatial/spectral subset
of the) multispectral image to be sharpened, the corresponding panchromatic
or high-resolution image and the output file. The panchromatic image should
overlap the multispectral image completely, so that the multispectral image
defines the extent of the final pan-sharpened product. Then the MS to pan
spatial resolution ratio (2 or 4), the MS band to be used for co-registration,
and, in the case of DWT fusion, a fine adjustment parameter are queried.
During the calculation the correlations of the wavelet coefficients for the low-
vs. high-resolution bands are printed.
are a good choice; see ?. PolSARpro is first used to create multi-look images
in covariance matrix format, which can then be exported to MapReady for
georeferencing with or without a DEM. Alternative commercial solutions are
the Gamma Software (Gamma Remote Sensing)
http://www.gamma-rs.ch/
enter the filename for the classification (thematic map) image and (option-
ally) a filename for the class membership probability image. The latter is not
available for the maximum likelihood classifier. Finally, if the neural network
classifier was selected, the number of hidden neurons must be entered. (A
cross-entropy plot will be displayed after training has completed.) If the out-
put format chosen is ENVI, then an ENVI header corresponding to the ENVI
filetype “classification” will be written. The test result file can be processed
with the scripts ct.py and mcnemar.py discussed in Section D.2.6.3 below.
parallelization, so that the script uses the ordinary Python map() function to
emulate parallel processing on the cloud service.
The scripts ct.py and mcnemar.py are used to evaluate and compare test
results generated by the supervised classifiers discussed in this appendix. They
request input files with extension tst and generate their outputs (contingency
tables, accuracies, test statistics, etc.) on the standard output.
The user is first queried for a working directory and input image filename.
The image can be in any format recognized by GDAL. Then the output file-
name and desired format must be entered. The anomaly image (the Maha-
lanobis distance of each pixel vector to the mean image background) is written
to disk.
The user is first queried for a working directory and input image filename.
The image can be in any format recognized by GDAL. Then spatial and/or
spectral subsets can be entered along with a training data sample size to es-
timate the kernel and the desired number of clusters. Finally, the output
filename and format and the kernel type (Gaussian or linear) must be cho-
sen. If the output format is ENVI, then an ENVI classification file header is
generated.
When running the iMad.py script, you are first prompted for an input
directory and then for the (spectral and spatial subsets of) the first and second
input images. The input subsets must be co-registered and have the same
spatial/spectral dimensions. They can be in any format recognized by GDAL.
The following prompts are for a regularization or penalization factor (default
0) and for an output filename, which again can be in any GDAL multispectral
format. The script prints the convergence criterion Delta and the canonical
correlations for each iteration. Computation terminates when Delta < 0.001
or after 100 iterations. The output consists of the stacked MAD variates in
order of decreasing variance followed by the chi-square image.
The two polarimetric SAR images should have the same format as in Section
D.2.4.2 above and be geo-referenced. The user is asked to choose a working
directory, and then to enter the input filename and the equivalent number of
looks for each image successively. The images must have the same polarimetry,
which can be any of the following:
• full polarimetry
• dual polarimetry
• single polarization.
Next a significance threshold for the change map image is queried, and fi-
nally, an output filename and desired format. The images are co-registered
automatically and the second image is clipped to have the same spatial ex-
tent as the first. Note: this will only work reliably when the original spatial
dimensions of the two images are similar. After completion, a 2-band image
consisting of the test statistic −2ρ ln Q, and the associated change probability
Pr(−2ρ ln Q) ≤ z) is written to the chosen output filename. The latter can be
482 Python Scripts
This script takes advantage of the linear and affine invariance of the MAD
transformation to perform a relative radiometric normalization of the images
involved in the transformation.
The routine first prompts for an input directory, (spectral/spatial subsets
of) the reference and target images, as well as for the spatial subset of the iMad
image generated according to Section D.2.8.1 above (the chi-square band is
found automatically). The spatial subsets of all three input images must have
the same size and the spectral subsets of the reference and target images must
match. After the prompt for an output filename, which can have any of the
formats recognized by GDAL, the filename of a larger (e.g., full) scene can be
(optionally) entered. This file must have the same spectral dimensions as the
reference and target files and will be normalized with the regression coefficients
determined by them. The result will be stored in the same format and with the
same root name as the specified output filename, but with norm appended.
Finally the user is prompted for a minimum no-change probability threshold
(default 0.95). The script prints, band-wise, the regression coefficients, and
the results of statistical tests for equal means and variances of the reference
and normalized target image bands. The tests are evaluated on the basis of
the hold-out test pixels (train:test = 2:1). The script also plots the orthogonal
regression lines for up to 10 spectral bands.