0

I am trying to obtain the mode and the mode frequency per pixel of different iterations of a Random Forest Classifier (With a fixed number of trees but with different random seeds) with 5 classes (0 to 4). So far, I can easily extract the mode from the collection with var GUE_Mode = ee.ImageCollection(MyCollection).mode() function, but I haven't found a way to add another band to depict the count per pixel of the mode class frequency in the collection, to add a measure of pixel confidence/stability/agreement. Say, if I have 20 different iterations, I would like each pixel in the mode image to have a band depicting how many images depict this pixel classified as the mode class, to add higher confidence to the pixel depicted as that class in 19/20 or 20/20, than to those below this value. This is the code I have been developing for this purpose, so far without the desired output:

// Function to count the frequency of each mode pixel on the collection var addModeFrequency = function(mode, collection) { // Extract the mode class var ModeClass = mode.select('LULC'); // Define a function to count occurrences of mode class in each image var countOccurrence = function(image) { // Extract the class band from the image var ImageClass = image.select('LULC'); // Count the pixels where mode class matches image class var count = ModeClass.eq(ImageClass).reduceRegion({ reducer: ee.Reducer.sum(), geometry: mode.geometry(), scale: 60, maxPixels: 1e9 }).getNumber('LULC'); // Return the count as an image with same geometry as mode return ee.Image.constant(count).rename('counts'); }; // Map over the collection to apply countOccurrence function var frequency = collection.map(countOccurrence); // Merge the frequency images into a single image var stabilityMap = ee.ImageCollection(frequency).sum().rename('stability'); return mode.addBands(stabilityMap); }; var stabilityMap = addModeFrequency(ee.Image(GUE_mode),ee.ImageCollection(GUE_FC)); print('stabilityMap:', stabilityMap) // Visualize the stability map Map.addLayer(stabilityMap.select('stability'), {min: 0, max: 20, palette: ['black', 'white']}, 'Stability Map'); 

1 Answer 1

0

I cracked it, here is the code:

// Function to count the frequency of each mode pixel on the collection var addModeFrequency = function(mode, collection) { // Define a function to count occurrences of mode class in each image var countOccurrence = function(image) { // Get the class band from the image var classBand = image.select('LULC'); // Count the pixels where mode class matches image class var count = mode.eq(classBand).rename('counts'); return count; }; // Map over the collection to apply countOccurrence function var frequency = collection.map(countOccurrence); // Merge the frequency images into a single image var sumImage = ee.ImageCollection(frequency).sum().rename('frequency'); return mode.addBands(sumImage); }; 

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.