
328 Unsupervised Classification
Listing 8 .1: K-means clustering of a multispectral image with Python.
1 #!/ usr / bin / env python
2 # Name : ex8_1 . py
3 IMPORT auxi l . aux il as auxi l
4 FROM num py IMPO RT *
5 FROM osg eo IMPO RT gdal
6 FROM osg eo . gdalcons t I MPORT GA_ReadOnly , GDT_ Byte
7 FROM sci py . cluster . vq IMP ORT kmeans , vq
8
9 DEF main ():
10 gdal . AllR egiste r ()
11 infi le = aux il . s elect_infile ()
12 IF inf ile :
13 inDa taset = gdal . Open ( infile , GA_Re adOnly )
14 cols = inDa taset . RasterXSize
15 rows = inDa taset . RasterYSize
16 band s = inDa taset . Rast erCount
17 ELSE :
18 RETU RN
19 pos = auxi l. sel ect_po s ( ba nd