Mask pixels in Image Collection based on band

google-earth-enginegoogle-earth-engine-javascript-api

I have an image collection with multiple bands. I try to mask each images, based on values (in my case 1 and 11) of the 'SCL' band.

This is my code, but the pixels in the other bands don't seem to be masked from what I can tell.

var s2Sr = ee.ImageCollection('COPERNICUS/S2_SR');
s2Sr = s2Sr.select(['B2', 'B3', 'B4', 'B5', 'B6', 'B7', 'B8', 'B8A', 'B11', 'B12', 'SCL']);
var masked_scl = s2Sr.map(function (img) {
    return img.updateMask((img.select('SCL').eq(1).or(img.select('SCL').eq(11))).not());
})
Map.addLayer(masked_scl.select(['B12']), {palette: ['white', 'green']}, 'masked');

Best Answer

You're adding the complete collection to the map. If there is any unmasked image, that pixel will be used. Since your collection contains every single Sentinel 2 image, there's a pretty good chance there some pixel where SCL isn't 1 or 11.

To verify if you're properly masking the images, just look at a single image in the collection, the first one for instance:

Map.addLayer(masked_scl.select(['B12']).first(), {palette: ['white', 'green']}, 'masked');

As far as I can see, your mask is applied to all bands.