0

What I would like to do is iterate through an Imagecollection dataset and process each image.What i would like to do is extract each image compute the NDVI using this:

var image2 = Filtered_Free_image.clip(geometry);
Map.addLayer(image2, {bands:['B4','B3','B2'], min:0, max:1});
var NDVI = image2.expression(
        "(NIR - RED) / (NIR + RED)",
        {
          RED: image2.select("B4"),    //  RED
          NIR: image2.select("B8"),    // NIR
          BLUE: image2.select("B2")    // BLUE
        });

and then print this using:

Map.addLayer(NDVI, {min: 0.1, max: 0.8}, "NDVI");

Using this code:

var Filtered_Region = imageCollection.filterBounds(geometry); //Load the dataset
var Filtered_Free_image = Filtered_Region.first();//Take the first image

I am able to take the 1st image from the dataset but I don't know how to proceed. How can I take the 2nd,3rd..etc image using something like this:Filtered_Free_image[2],Filtered_Free_image[3]?

Should I first convert this to a list? If yes, how?

1 Answers1

1

You can convert to a list, yes, but the best way to proceed to get an efficient Earth Engine script is usually to avoid using numeric indexes at all. Whenever possible, working on images in an image collection should be done with .map(...). You take each operation you want to do on an image, wrap it in a function, and pass that function to .map(...):

var Filtered_Region = imageCollection.filterBounds(geometry);

// Clip each image in the collection.
var Clipped_Collection = Filtered_Region.map(function (image) {
  return image.clip(geometry);
});

// Display the composite (mosaic) of all images in the collection.
Map.addLayer(Clipped_Collection, {bands:['B4','B3','B2'], min:0, max:1});

// Compute NDVI of each image in the collection.
var NDVI_Collection = Clipped_Collection.map(function(image) {
  return image.expression(
        "(NIR - RED) / (NIR + RED)",
        {
          RED: image2.select("B4"),    //  RED
          NIR: image2.select("B8"),    // NIR
          BLUE: image2.select("B2")    // BLUE
        });
});

// Display the composite (mosaic) of all NDVIs.
Map.addLayer(NDVI_Collection, {min: 0.1, max: 0.8}, "NDVI");

Note that when you give addLayer a collection it displays the most recent pixel at each point. If instead you want some type of average, you can specify that:

Map.addLayer(NDVI_Collection.median(), {min: 0.1, max: 0.8}, "NDVI");
Kevin Reid
  • 37,492
  • 13
  • 80
  • 108
  • Thank you for the answer!.I modified the question giving more info about the situation.I indeed would like to extract each image from the sequence and process it so if you could inform me about this case you would really help! – Giannis Psixogios Jul 14 '20 at 13:32
  • @GiannisPsixogios I have updated my answer to show how to modify the code you provided. Thanks for clarifying! – Kevin Reid Jul 14 '20 at 17:14