0

I am trying to do SNIC segmentation and then classification in Google Earth engine. My problem is that every time I change the zoom scale, it gives me a new result with a recalculation. For this I looked at this link: Is Google Earth Engine SNIC Segmentation algorithm inconsistent?

The reproject method here solves my problem, but as the area grows or the number of bands in the image increases, I get errors. Applying only to the "clusters" band did not solve my problem either. I need to set the scale to 10 m and obtain mean values for each segment.

//Select the band "clusters" from the snic output fixed on its scale of 10 meters 

var clusters_snic = snic.select("clusters")
var clusters_snic = clusters_snic.reproject ({crs: 'EPSG:3857', scale: 10});

Can you give advice for this?

The simplified demo code can be found https://code.earthengine.google.com/99fb39b5053e7c5a7db92a78a74a9f5e

//image import 

var s2 = ee.ImageCollection('COPERNICUS/S2_SR');

var rgbVis = {
  min: 0.0,
  max: 3000,
  bands: ['B4', 'B3', 'B2'], //11-8A-4 //'B4', 'B3', 'B2' // 'B11', 'B8A', 'B4'
};
 
var filtered = s2
  .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 30))
  .filter(ee.Filter.date('2021-08-25', '2021-08-29'))
  .filter(ee.Filter.bounds(geometry))
  .select('B.*');

var composite = filtered.mean().clip(geometry) ;
print('composite image', composite)
// Display the input composite.
Map.addLayer(composite, rgbVis, 'image');

var img= composite;


var bands = ['B4', 'B3', 'B2','B8','B11','B12']; //B8','B8A','B11','B12'
var FullImage = img.select(bands).float()//.divide(255);


print('FullImage 4 bands',FullImage)



// Segmentation -----------------------------------------------------------------------------

var seeds = ee.Algorithms.Image.Segmentation.seedGrid(100);
//size : The superpixel seed location spacing, in pixels.

//Map.addLayer(seeds, {palette: 'FF0000'},'seeds');


var snic = ee.Algorithms.Image.Segmentation.SNIC({
  image: FullImage, 
  compactness: 0, // Compactness factor. Larger values cause clusters to be more compact (square).
  //Setting this to 0 disables spatial distance weighting.
  connectivity: 8, // Connectivity. Either 4 or 8.
  neighborhoodSize: 256, // Tile neighborhood size (to avoid tile boundary artifacts). Defaults to 2 * size.
  //size: 16, // The superpixel seed location spacing, in pixels. If 'seeds' image is provided, no grid is produced.
  seeds: seeds, // If provided, any non-zero valued pixels are used as seed locations.
  //Pixels that touch (as specified by 'connectivity') are considered to belong to the same cluster.
//}).reproject({
  //crs: 'EPSG:3857',
  //scale: 10
})

//Map.addLayer(snic,rgbVis,"snic");
//var clusters_snic = snic.select("clusters")
//clusters_snic = clusters_snic.reproject ({crs: 'EPSG:3857', scale: 10});
//Map.addLayer(clusters_snic.randomVisualizer(), {}, 'clusters')
//print(clusters_snic,'clusters_snic')
//Map.addLayer(clusters_snic.randomVisualizer(), null, "clusters")



// Prepare data ------------------------------------------------------------------------------

var train_points = unburn.merge(burned)
//var train_points = burn2.merge(nonburn)

//split train points

// Add a random column to train_points
var train_points_withRandom = train_points.randomColumn('random');

// Split the data into training (70%) and validation (30%) sets
var split = 0.7; // Percentage of data for training
var training_set = train_points_withRandom.filter(ee.Filter.lt('random', split));
var validation_set = train_points_withRandom.filter(ee.Filter.gte('random', split));

// Display the size of training and validation sets
print('Number of points in training set:', training_set.size());
print('Number of points in validation set:', validation_set.size());

// Now you can use the training_set and validation_set for further processing.


//////////////***************
/////////////////**********



//Select the band "clusters" from the snic output fixed on its scale of 10 meters 
// Calculate the mean for each segment with respect to the pixels in that cluster
var clusters_snic = snic.select("clusters")
var clusters_snic = clusters_snic.reproject ({crs: 'EPSG:3857', scale: 10});
//Map.addLayer(clusters_snic.randomVisualizer(), {}, 'clusters')

var clusters_snic = clusters_snic.addBands(snic)
var clusters_snic_bands=clusters_snic.bandNames().remove('clusters_1')
print(clusters_snic_bands)




//Define the training bands removing just the "clusters" bands
var predictionBands=clusters_snic.bandNames().remove("clusters").remove('clusters_1')
print(predictionBands)
print(clusters_snic)
//Classification using the classifier with the training bands called predictionBands
var training_geobia = clusters_snic.select(predictionBands).sampleRegions({
  collection: training_set,
  properties: ['landcover'],
  scale: 10,
  tileScale:16
});



var RF = ee.Classifier.smileRandomForest(50).train({
  features:training_geobia, 
  classProperty:'landcover', 
  inputProperties: predictionBands,
});


var classified_RF = clusters_snic.select(predictionBands).classify(RF);

var vis_RF = {min: 0, max: 1,
palette: [ 'gray' //1 unburn
,'red' //2 burned
]
}
Map.addLayer(classified_RF,vis_RF,"OBIA_RF");
3
  • When you use reproject, you will ultimately have to run things as an Export, because you'r easking for too much data to fint in a single interactive timeout. Your best option is to export the clusters band and then continue using the exported version. Commented Mar 4 at 11:28
  • Will requesting an additional quota solve my problem?
    – user217304
    Commented Mar 4 at 15:07
  • No, the memory ceiling and interactive timeout are fixed constraints. You get more memory and time as a batch task. Commented Mar 5 at 16:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.