With this code GGE get the least cloudy image in 2022, but i need to calculate NDVI in a specific image (2022-07-20). What should i change? If I try to specify a date in filter.Date the code doesn't work. .filterDate('2022-07-20')
The error is: NDVI image: Layer error: Image.select, argument 'input': Invalid type. Expected type: Image. Actual type: ImageCollection.
var point = ee.Geometry.Point([8.73727, 45.81165]);
// Import the Sentinel-2 image collection.
var S2 = ee.ImageCollection("COPERNICUS/S2")
// Get the least cloudy image in 2022.
var image = ee.Image(
S2.filterBounds(point)
.filterDate('2022-01-01', '2022-12-31')
.sort('CLOUD_COVER')
.first()
);
// Compute the Normalized Difference Vegetation Index (NDVI).
var nir = image.select('B8');
var red = image.select('B4');
var ndvi = nir.subtract(red).divide(nir.add(red)).rename('NDVI');
// Display the result.
Map.centerObject(image, 9);
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
Map.addLayer(ndvi, ndviParams, 'NDVI image');