I am new to Google Earth Engine and have been using it for just over two weeks now. I have a question about image composition.

I have got three images with me that cover the same area over three different time periods. I have done cloud masking and filtering on all three images which come from the sentinel-2 satellite. Because of that, one of the images is mostly empty with many null pixel values because of cloud cover over that period of time. I need to now composite two images on it which are much clearer. All three images are in an image list.

The way image composition needs to work is that now there are three images and the image that is in the middle needs to be worked on. So, I want to do image composition in such a way that for all the null pixel values in the middle image, those null values needs to be covered with the average of the other two images if the pixel is available from both images. If the pixel is not available in the other two images, it should put just the value available.

An example would be if there is Null value and the other two images have value 2 and 3 at the same spot, then the new value would be (2 + 3) / 2 = 2.5. If one of the other two image also has the null value, then the new value in the image would be 2.

1 Answer 1


I think you are looking for ee.ImageCollection.mean() reducer. It calculates the mean value from all pixels. After you create the cloud masked images, apply the mean reducer to the image collection. It will create a composite image with mean values from all available pixels as you have mentioned. If there is only one image that has a certain pixel the value will be replaced with it.

Here is an example of using the mean reducer. After mapping with the cloudMask function, it can be executed.

var meanImage = ee.ImageCollection('COPERNICUS/S2_SR')
  .filterDate('2021-07-01', '2021-08-01')
  .filterBounds(ee.Geometry.Point(-122.373, 37.448))
  • The mean value will just give me mean for the pixels that already has value. I want to do the average for the place where there is no pixels.
    – Raj Bajaj
    Jun 14, 2023 at 18:42
  • What I have understood from your example: if you have one image pixel with null and other corresponding pixel of another image with some value, you want to create an image composite where the null will be replaced by the existing value or the average or mean will be calculated on the basis of non-null values. The mean reducer does the job. You can test the code.
    – Padmanabha
    Jun 14, 2023 at 19:01
  • It does that that works. Thank you. I just have one more question. If the middle image over which image composing needs to be done has a value of 2, but the image before that has value 4 and the image after that has value 3. What would be the final value, will it be (2+3+4)/3 = 3 or will it be original 2 because I need 2 as the value. Thank You.
    – Raj Bajaj
    Jun 14, 2023 at 19:22
  • The final value will be 3. So, you want only the null pixels to be have the mean.
    – Padmanabha
    Jun 14, 2023 at 19:49
  • Yes, I need final pixels to have the mean.
    – Raj Bajaj
    Jun 14, 2023 at 20:39

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.