I'm doing a supervised classification using training data from a 2023 Image to a 2022 Image. However, when doing the migration I got the error message "No valid training data were found" for the 2022 Image even though the 2023 Image worked as usual. The Image for 2023 has a previous pre processing to add some bands like elevation and index, and the training data was taken using this same 2023 Image.
The code is right here: https://code.earthengine.google.com/7e3ca5825d18782c457c32b38b793007
var distance = Image_2022.spectralDistance(Image_2023);
Map.addLayer(distance,{}, 'Distancia Espectral')
var samples= Forest.merge(Nonforest)
Map.addLayer(samples, {}, 'Puntos 2023')
var samples_distance= distance.sampleRegions({
collection: samples,
properties: ['name','id'],
scale:10
})
Map.addLayer(samples_distance, {}, 'Puntos Migrados')
var threshold = 0.2
var newGcp = samples_distance.filter(ee.Filter.lt('distance', threshold));
print('Total GCPs', samples.size());
print('Migrated GCPs', newGcp.size());
performClassification(Image_2023, samples, 'Classificacion 2023');
performClassification(Image_2022, newGcp, 'Classificacion 2022');
function performClassification(Image_2022, samples, year) {
var samples = samples.randomColumn();
var trainingGcp = samples.filter(ee.Filter.lt('random', 0.6));
var validationGcp = samples.filter(ee.Filter.gte('random', 0.6));
// Overlay the point on the image to get training data.
var training_2 = Image_2022.sampleRegions({
collection: trainingGcp,
properties: ['name','id'],
scale: 10,
tileScale: 16
});
// Train a classifier.
var classifier_2 = ee.Classifier.smileRandomForest(50)
.train({
features: training_2,
classProperty: 'id',
inputProperties: Image_2022.bandNames()
});
// Classify the image.
var classified_2022 = Image_2022.classify(classifier_2);
Map.addLayer(classified_2022, classVis, year);
// Use classification map to assess accuracy using the validation fraction
// of the overall training set created above.
var test = classified_2022.sampleRegions({
collection: validationGcp,
properties: ['id'],
tileScale: 16,
scale: 10,
});
var testConfusionMatrix = test.errorMatrix('id', 'clasificacion')
// Printing of confusion matrix may time out. Alternatively, you can export it as CSV
print('Confusion Matrix ' + year, testConfusionMatrix);
print('Test Accuracy ' + year, testConfusionMatrix.accuracy());
}
var styling = {color: 'black', fillColor: '00000000'};
Map.addLayer(Poligonos_Leaf.style(styling), {}, 'Poligonos Sosty')
I was following this methodology: https://courses.spatialthoughts.com/end-to-end-gee.html#classification-with-migrated-training-samples