Yes, it will work with numpy arrays. spy is intended for use with multi-band images so the clustering algorithm expects the shape of its input to be (M, N, K), where M, N, and K are the numbers of rows, columns and bands, respectively. My guess is that your array is only two dimensional (shape is (M, N)). Try adding the third dimension to the shape:
npLandsat.shape = npLandsat.shape + (1,)
-thomas
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hello,
I tried Spectral Python 0.6 to classify a tif file with the following code:
from osgeo import gdal
import numpy as np
import spectral as spy
landsat = gdal.Open('/tmp/L72192027_02720020914_B80_900913_cut.tif')
npLandsat = tif.ReadAsArray()
view(npLandsat)
(m, c) = spy.cluster(npLandsat, 5)
After the cluster i've got the following error:
IndexError Traceback (most recent call last)
/home/kaotika/<ipython-input-7-2e98755e86c6> in <module>()
---> 1 (m, c) = spy.cluster(npLandsat, 5)
/usr/local/lib/python2.6/dist-packages/spectral/algorithms/clustering.pyc in cluster(data, nClusters)
481 '''
482 opc = OnePassClusterer(nClusters)
-> 483 return opc.classifyImage(data)
484
485
/usr/local/lib/python2.6/dist-packages/spectral/algorithms/clustering.pyc in classifyImage(self, image)
419 else:
420 typecode = 'f'
-> 421 self.clusters = numpy.zeros((self.maxClusters, self.image.shape), typecode)
422 self.nClusters = 0
423 clusters = self.clusters
IndexError: tuple index out of range
Is it possible to classify (supervised and unsupervised) images imported as numpy array? Is it possible to convert numpy arrays to spy arrays?
Thanks for help
Yes, it will work with numpy arrays. spy is intended for use with multi-band images so the clustering algorithm expects the shape of its input to be (M, N, K), where M, N, and K are the numbers of rows, columns and bands, respectively. My guess is that your array is only two dimensional (shape is (M, N)). Try adding the third dimension to the shape:
-thomas
This was it. Now it works perfektly! Many Thanks!