예제 #1
0
 def test_gmlc_classify_transformedimage_runs(self):
     '''Tests that GaussianClassifier classifies a TransformedImage object.'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data),
                                      self.gt,
                                      calc_stats=True)
     gmlc = spy.GaussianClassifier(ts)
     ret = gmlc.classify_image(ximg)
예제 #2
0
 def test_gmlc_classify_ndarray_transformedimage_equal(self):
     '''Gaussian classification of an ndarray and TransformedImage are equal'''
     pc = spy.principal_components(self.data).reduce(num=3)
     ximg = pc.transform(self.image)
     ts = spy.create_training_classes(pc.transform(self.data),
                                      self.gt,
                                      calc_stats=True)
     gmlc = spy.GaussianClassifier(ts)
     cl_ximg = gmlc.classify_image(ximg)
     cl_ndarray = gmlc.classify_image(pc.transform(self.data))
     assert (np.all(cl_ximg == cl_ndarray))
예제 #3
0
 def test_gmlc_spectrum_image_equal(self):
     '''Tests that classification of spectrum is same as from image.'''
     gmlc = spy.GaussianClassifier(self.ts, min_samples=600)
     data = self.data[20:30, 30:40, :]
     assert(gmlc.classify_spectrum(data[2, 2]) == \
            gmlc.classify_image(data)[2, 2])
예제 #4
0
cm1 = 'maskar = np.concatenate((' + mskstr + '))'
cm2 = 'classar = np.concatenate((' + allstr + '))'
exec cm1
exec cm2

#Adjust (either zero-pad or cut) classification array to work with source raster
(cx, cy) = maskar.shape
if src_raster_num_bands > classification_images_num_bands:
    zerar = np.zeros((cx, cy))
    diff = src_raster_num_bands - classification_images_num_bands
    for i in range(diff):
        classar = np.dstack((classar, zerar))

#Create training class and model
trcls = sp.create_training_classes(classar, maskar)
gmlc = sp.GaussianClassifier(trcls)
mldc = sp.MahalanobisDistanceClassifier(trcls)

#Classify image and display results
(kclmap, c) = sp.kmeans(barall, 12, 30)  #unsupervised
gclmap = gmlc.classify_image(barall)  #supervised
mclmap = mldc.classify_image(barall)  #supervised


#Define classification colors for array conversion
def num2colR(arg):
    switcher = {
        1: 255,
        2: 0,
        3: 255,
        4: 255,
예제 #5
0
 def test_gmlc_classify_spyfile_runs(self):
     '''Tests that GaussianClassifier classifies a SpyFile object.'''
     gmlc = spy.GaussianClassifier(self.ts, min_samples=600)
     ret = gmlc.classify_image(self.image)
예제 #6
0
    xpos = xpos + xs

# Close training images
for i in range(len(trefs)):
    trefs[i] = None

# Print source raster metadata
print 'Driver: ', data_f.GetDriver().LongName
print 'Raster Sixe: ', data_f.RasterXSize, 'x', data_f.RasterYSize
print 'Pixel Size: ', data_f.GetGeoTransform()[1], ',', data_f.GetGeoTransform(
)[5]

#Create training class and model
tclass = sp.create_training_classes(tdata, tmask)
gmlc = sp.GaussianClassifier(tclass)
mldc = sp.MahalanobisDistanceClassifier(tclass)

# Read in data raster
data = np.zeros((data_f.RasterYSize, data_f.RasterXSize, data_nband))
for i in range(len(trefs)):
    for j in range(data_nband):
        data[:, :, j] = data_f.GetRasterBand(j + 1).ReadAsArray()

#Classify image and display results
print("K-Means")
(kclmap, c) = sp.kmeans(data, kmeans_clusters,
                        kmeans_iterations)  #unsupervised
print("Gaussian")
gclmap = gmlc.classify_image(data)  #supervised
print("Mahalanobis")