Google Earth Engine Masking – How to Replace Masked Pixels by Another Image

google-earth-enginegoogle-earth-engine-javascript-apimasking

How can I replace masked pixels with another image?

I have two sets of images: Image_A and Image_B, each has 10 bands.

I want to change the masked pixels in Image_A by the corresponding (same band, line, and column) values from Image_B.

Image_A_Update = Image_A.updateMask(Image_A.unmask(Image_B))

If Image_B also has a mask for some pixels, the output would be the mask for those pixels.

Best Answer

var Image_A_Update = Image_B.blend(Image_A);

This will be unmasked wherever at least one of the images is unmasked, using pixels from Image_A if available and pixels from Image_B otherwise.

(This is the same operation as ImageCollection.mosaic(), just applied to two images instead of an entire collection.)