Commit c6df09a1 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Moved L2B_P.RSImage_ClusterPredictor.classify_image to classification module.

parent 161c4f97
Pipeline #3039 passed with stage
in 18 minutes and 22 seconds
......@@ -43,7 +43,7 @@ from ..misc.logging import close_logger
from ..model.metadata import get_LayerBandsAssignment
from .L2A_P import L2A_object
from ..model.gms_object import GMS_identifier
from .classification import MinimumDistance_Classifier, kNN_Classifier, SAM_Classifier
from .classification import classify_image
__author__ = 'Daniel Scheffler'
......@@ -873,7 +873,7 @@ class ReferenceCube_Generator(object):
# resample the set of random spectra to match the spectral characteristics of all target sensors
for tgt_sat, tgt_sen in self.tgt_sat_sen_list:
# perform spectal resampling
# perform spectral resampling
self.logger.info('Performing spectral resampling to match %s %s specifications...' % (tgt_sat, tgt_sen))
unif_random_spectra_rsp = \
self.resample_spectra(unif_random_spectra,
......@@ -1539,40 +1539,6 @@ class RSImage_ClusterPredictor(object):
return Cluster_Learner(dict_clust_MLinstances)
def classify_image(self, image, cluster_classifier, nodataVal=None, tiledims=(1000, 1000)):
# type: (Union[np.ndarray, GeoArray], Cluster_Learner, Union[int, float], tuple) -> GeoArray
"""Classify src_spectra to find the cluster each spectrum belongs to.
:param image: image to be classified
:param cluster_classifier: image to be classified
:param nodataVal:
:param tiledims:
"""
if self.classif_alg == 'kNN':
clf = kNN_Classifier(
cluster_classifier.cluster_centers,
cluster_classifier.cluster_pixVals,
CPUs=self.CPUs,
n_neighbors=self.kNN_n_neighbors)
elif self.classif_alg == 'MinDist':
clf = MinimumDistance_Classifier(
cluster_classifier.cluster_centers,
cluster_classifier.cluster_pixVals,
CPUs=self.CPUs)
elif self.classif_alg == 'SAM':
clf = SAM_Classifier(
cluster_classifier.cluster_centers,
CPUs=self.CPUs)
else:
raise NotImplementedError("Currently only the methods 'kNN', 'MinDist' and 'SAM' are implemented.")
cmap = clf.classify(image, nodataVal=nodataVal, tiledims=tiledims) # use default tiledims (100 x 100)
return cmap
def predict(self, image, classifier, nodataVal=None, CPUs=1):
# type: (Union[np.ndarray, GeoArray], Cluster_Learner, float, int) -> GeoArray
"""Apply the prediction function of the given specifier to the given remote sensing image.
......@@ -1590,7 +1556,13 @@ class RSImage_ClusterPredictor(object):
if not self.classif_map:
if self.n_clusters > 1:
t0 = time.time()
self.classif_map = self.classify_image(image, classifier, nodataVal=nodataVal)
self.classif_map = classify_image(image,
classifier.cluster_centers,
classifier.cluster_pixVals,
classif_alg=self.classif_alg,
kNN_n_neighbors=self.kNN_n_neighbors,
nodataVal=nodataVal,
CPUs=self.CPUs)
print('Total classification time: %s' % time.strftime("%H:%M:%S", time.gmtime(time.time() - t0)))
else:
self.classif_map = np.full((image.rows, image.cols), classifier.cluster_pixVals[0], np.int8)
......
......@@ -112,3 +112,48 @@ class SAM_Classifier(_ImageClassifier):
cmap[mask] = -9999
return cmap
def classify_image(image, train_spectra, train_labels, classif_alg,
kNN_n_neighbors=10, nodataVal=None, tiledims=(1000, 1000), CPUs=None):
# type: (Union[np.ndarray, GeoArray], np.ndarray, Union[np.ndarray, List[int]], str, int, ...) -> GeoArray
"""Classify image to find the cluster each spectrum belongs to.
:param image: image to be classified
:param train_spectra:
:param train_labels:
:param classif_alg: algorithm to be used for image classification
(to define which cluster each pixel belongs to)
'MinDist': Minimum Distance (Nearest Centroid)
'kNN': k-nearest-neighbour
'SAM': spectral angle mapping
:param kNN_n_neighbors: The number of neighbors to be considered in case 'classif_alg' is set to
'kNN'. Otherwise, this parameter is ignored.
:param nodataVal:
:param tiledims:
:param CPUs: number of CPUs to be used for classification
"""
if classif_alg == 'kNN':
clf = kNN_Classifier(
train_spectra,
train_labels,
CPUs=CPUs,
n_neighbors=kNN_n_neighbors)
elif classif_alg == 'MinDist':
clf = MinimumDistance_Classifier(
train_spectra,
train_labels,
CPUs=CPUs)
elif classif_alg == 'SAM':
clf = SAM_Classifier(
train_spectra,
CPUs=CPUs)
else:
raise NotImplementedError("Currently only the methods 'kNN', 'MinDist' and 'SAM' are implemented.")
cmap = clf.classify(image, nodataVal=nodataVal, tiledims=tiledims)
return cmap
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment