Multispectral Image Analysis in a Docker Container: Examples (continued)

Start the container, if it is not already running, with

sudo docker start crc

or pull and run it with

sudo docker run -d -p 443:8888 -v yourImageFolder:/crc/imagery --name=crc mort/crcdocker

then point your browser to

http://loclahost:443

Example 4: Supervised Land Cover Classification

This example follows some of the development in Chapters 6 and 7 of my book.

First cd to the main directory in the container:

In [19]:
cd /crc
/crc

In the shared imagery directory there is an ASTER image over Juelich, Germany , acquired on May 01, 2007. Also a train shapefile containing the training regions for a supervised land cover classification. The shapefile was generated from an ENVI ROI training dataset, but could have been prepared from any suitable GIS or image analysis environment.

In [20]:
ls -l imagery/may0107.tif imagery/train*
-rw------- 1 1000 1000 36072465 Jan  8 10:08 imagery/may0107.tif
-rw------- 1 1000 1000     2384 Jul  5  2012 imagery/train.dbf
-rw------- 1 1000 1000      391 Jul  5  2012 imagery/train.prj
-rw------- 1 1000 1000    30740 Jul  5  2012 imagery/train.shp
-rw------- 1 1000 1000      340 Jul  5  2012 imagery/train.shx

The image consists of 9 bands:

In [21]:
!gdalinfo imagery/may0107.tif | grep Band
Warning 1: TIFFReadDirectoryCheckOrder:Invalid TIFF directory; tags are not sorted in ascending order
Band 1 Block=1000x1 Type=Float32, ColorInterp=Red
Band 2 Block=1000x1 Type=Float32, ColorInterp=Green
Band 3 Block=1000x1 Type=Float32, ColorInterp=Blue
Band 4 Block=1000x1 Type=Float32, ColorInterp=Undefined
Band 5 Block=1000x1 Type=Float32, ColorInterp=Undefined
Band 6 Block=1000x1 Type=Float32, ColorInterp=Undefined
Band 7 Block=1000x1 Type=Float32, ColorInterp=Undefined
Band 8 Block=1000x1 Type=Float32, ColorInterp=Undefined
Band 9 Block=1000x1 Type=Float32, ColorInterp=Undefined

namely the three NIR bands at 15m ground resolution and six SWIR bands pan-sharpened to the same resolution. Since the SWIR bands are very strongly correlated, we first do principal components transformation:

In [22]:
run pca imagery/may0107.tif
------------PCA ---------------
Sat Jan 10 13:34:50 2015
Input imagery/may0107.tif
Eigenvalues: [  9.53976940e+02   3.99938924e+02   5.32962329e+00   4.07891043e+00
   1.29570649e-01   4.94150789e-03   3.35630991e-03   1.14869654e-03
   8.14456455e-04]

result written to: imagery/may0107_pca.tif
elapsed time: 3.8827559948

and retain only the first four principal component bands for classification. The classification script is classify.py:

In [23]:
run classify -h

Usage: 
---------------------------------------------------------
python classify.py  [-p bandPositions] [- a algorithm] [-L number of hidden neurons]   
[-P generate class probabilities image] filename trainShapefile

bandPositions is a list, e.g., -p [1,2,4]  

algorithm  1=MaxLike
           2=NNet(backprop)
           3=NNet(congrad)
           4=SVM

If the input file is named 

         path/filenbasename.ext then

The output classification file is named 

         path/filebasename_class.ext

the class probabilities output file is named

         path/filebasename_classprobs.ext
         
and the test results file is named

         path/filebasename_<classifier>.tst
--------------------------------------------------------

Let's run the Bayes Maximum Likelihood classifier first:

In [24]:
run classify -a 1 -p [1,2,3,4] imagery/may0107_pca.tif imagery/train.shp
=========================
supervised classification
=========================
Sat Jan 10 13:34:54 2015
image:    imagery/may0107_pca.tif
training: imagery/train.shp
MaxLike
reading training data...
7173 training pixel vectors were read in
training on 4782 pixel vectors...
elapsed time 0.00695896148682
classifying...
elapsed time 2.39069414139
thematic map written to: imagery/may0107_pca_class.tif
test results written to: imagery/may0107_pca_MaxLike.tst
done

and calculate a contingency table (aka confusion matrix) from the test data:

In [25]:
run ct imagery/may0107_pca_MaxLike.tst
=========================
classification statistics
=========================
MaxLiketest results for imagery/may0107_pca.tif
Sat Jan 10 13:34:58 2015
Classification image: imagery/may0107_pca_class.tif
Class probabilities image: None

Misclassification rate: 0.065245
Standard deviation: 0.005050
Conf. interval (95 percent): [0.056027 , 0.075858]
Kappa coefficient: 0.926184
Standard deviation: 0.005713
Contingency Table
[[  174.        0.        0.        0.        0.        0.        0.        0.        0.        0.      174.        1.   ]
 [    0.      170.        0.        0.        0.        0.        0.        0.        0.        0.      170.        1.   ]
 [    0.        0.      252.        0.        0.        0.        0.        0.        0.        0.      252.        1.   ]
 [    0.        0.        0.      305.       46.        2.        0.        2.        0.        0.      355.        0.859]
 [    0.        0.        0.        7.      214.        1.        0.        0.        0.        0.      222.        0.964]
 [    0.        0.        0.        0.        0.      138.        0.        0.        0.        0.      138.        1.   ]
 [    0.        0.        0.        0.        0.        0.      375.       45.        1.        0.      421.        0.891]
 [    0.        0.        0.        3.        0.        1.       14.      101.        7.        0.      126.        0.802]
 [    0.        0.        0.        5.        0.        5.        1.       16.      362.        0.      389.        0.931]
 [    0.        0.        0.        0.        0.        0.        0.        0.        0.      144.      144.        1.   ]
 [  174.      170.      252.      320.      260.      147.      390.      164.      370.      144.     2391.        0.   ]
 [    1.        1.        1.        0.953     0.823     0.939     0.962     0.616     0.978     1.        0.        0.   ]]

The kappa coefficietn is 0.926 \(\pm\) 0.005. We will compare this with the Support Vector Machine algorithm (-a 4):

In [26]:
run classify -a 4 -p [1,2,3,4] imagery/may0107_pca.tif imagery/train.shp
=========================
supervised classification
=========================
Sat Jan 10 13:34:58 2015
image:    imagery/may0107_pca.tif
training: imagery/train.shp
SVM
reading training data...
7173 training pixel vectors were read in
training on 4782 pixel vectors...
elapsed time 0.542578935623
classifying...
elapsed time 55.3350050449
thematic map written to: imagery/may0107_pca_class.tif
test results written to: imagery/may0107_pca_SVM.tst
done

Note that the run time was considerably longer than for Maximum likelihood. However the test results are better:

In [27]:
run ct imagery/may0107_pca_SVM.tst
=========================
classification statistics
=========================
SVMtest results for imagery/may0107_pca.tif
Sat Jan 10 13:35:56 2015
Classification image: imagery/may0107_pca_class.tif
Class probabilities image: None

Misclassification rate: 0.049352
Standard deviation: 0.004430
Conf. interval (95 percent): [0.041369 , 0.058780]
Kappa coefficient: 0.944216
Standard deviation: 0.005007
Contingency Table
[[  194.        0.        0.        0.        0.        0.        0.        0.        0.        0.      194.        1.   ]
 [    0.      157.        0.        0.        0.        0.        0.        0.        0.        0.      157.        1.   ]
 [    0.        0.      236.        0.        0.        0.        0.        0.        0.        0.      236.        1.   ]
 [    0.        0.        0.      274.       29.        0.        0.        2.        0.        0.      305.        0.898]
 [    0.        0.        0.        6.      222.        0.        1.        1.        0.        0.      230.        0.965]
 [    0.        0.        0.        0.        0.      125.        0.        0.        0.        0.      125.        1.   ]
 [    0.        2.        0.        0.        0.        0.      391.       39.        0.        0.      432.        0.905]
 [    0.        0.        0.        0.        0.        0.        8.      126.       15.        0.      149.        0.846]
 [    0.        0.        0.        1.        0.        4.        0.       10.      375.        0.      390.        0.962]
 [    0.        0.        0.        0.        0.        0.        0.        0.        0.      173.      173.        1.   ]
 [  194.      159.      236.      281.      251.      129.      400.      178.      390.      173.     2391.        0.   ]
 [    1.        0.987     1.        0.975     0.884     0.969     0.978     0.708     0.962     1.        0.        0.   ]]

To decide if the SVM result is significantly better, we run the Mcnemar test on the null hypothesis that both classifiers are equally good:

In [28]:
run mcnemar imagery/may0107_pca_SVM.tst imagery/may0107_pca_MaxLike.tst
=========================
     McNemar test
=========================
first classifier:
SVMtest results for imagery/may0107_pca.tif
Sat Jan 10 13:35:56 2015
Classification image: imagery/may0107_pca_class.tif
Class probabilities image: None

second classifier:
MaxLiketest results for imagery/may0107_pca.tif
Sat Jan 10 13:34:58 2015
Classification image: imagery/may0107_pca_class.tif
Class probabilities image: None

test observbations: 2391
classes: 10
first classifier: 107
second classifier: 145
McNemar statistic: 5.730159
P-value: 0.016676

The P-value is less than 0.05, indicating that the null hypothesis can be rejected. Here is the classified image obtained with the SVM (on the left) together with the NIR bands of the original image:

In [29]:
run dispms -f imagery/may0107_pca_class.tif -c -F imagery/may0107.tif -P [1,2,3] -E 4
In [29]: