Commit 12da92c4 authored by Daniel Scheffler's avatar Daniel Scheffler
Browse files

Added auto-normalization prior to SAM classification.S

parent 01b74f2e
Pipeline #3239 passed with stage
in 17 minutes and 46 seconds
......@@ -1637,7 +1637,6 @@ class RSImage_ClusterPredictor(object):
:param CPUs: CPUs to use (default: 1)
:return: 3D array representing the predicted spectral image cube
"""
image = image if isinstance(image, GeoArray) else GeoArray(image, nodata=nodataVal)
image.nodata = nodataVal if nodataVal is not None else image.nodata # might be auto-computed here
......
......@@ -6,10 +6,11 @@ Algorithms for multispectral image classification.
import warnings
import numpy as np
from typing import Union, List # noqa F401 # flake8 issue
from typing import Union, List, Tuple # noqa F401 # flake8 issue
from multiprocessing import Pool
from tqdm import tqdm
from sklearn.neighbors import KNeighborsClassifier, NearestCentroid
from sklearn.preprocessing import MaxAbsScaler
from pysptools.classification import SAM
from geoarray import GeoArray
......@@ -194,6 +195,9 @@ def classify_image(image, train_spectra, train_labels, classif_alg,
CPUs=CPUs)
elif classif_alg == 'SAM':
# normalize input data because SAM asserts only data between -1 and 1
train_spectra, image = normalize_endmembers_image(train_spectra, image)
clf = SAM_Classifier(
train_spectra,
CPUs=CPUs)
......@@ -204,3 +208,20 @@ def classify_image(image, train_spectra, train_labels, classif_alg,
cmap = clf.classify(image, nodataVal=nodataVal, tiledims=tiledims)
return cmap
def normalize_endmembers_image(endmembers, image):
# type: (np.ndarray, np.ndarray) -> Tuple[np.ndarray, np.ndarray]
from .L2B_P import im2spectra, spectra2im # avoid circular import
em = endmembers.astype(np.float)
im = image.astype(np.float)
allVals = np.hstack([em.flat, im.flat]).reshape(-1, 1)
max_abs_scaler = MaxAbsScaler()
max_abs_scaler.fit_transform(allVals)
endmembers_norm = max_abs_scaler.transform(em)
image_norm = spectra2im(max_abs_scaler.transform(im2spectra(im)), tgt_rows=im.shape[0], tgt_cols=im.shape[1])
return endmembers_norm, image_norm
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