I have the following code (utilizing a script by Rodrigo Principe that implements Hollstein et al's 2016 Sentinel 2 cloud mask), which overall works quite well.
var mask_all = function(image) {
var hollstein_mask = cld.hollstein_S2(['shadow', 'cloud', 'cirrus'])(image)
return hollstein_mask
};
However, it leaves some white borders around clouds and some black borders around shadows. I'd like to get rid of these with a buffer. But no matter how long I bang away at it, I can't manage to convert the existing mask to a binary image, buffer that, and then use that to update the mask. Here is the best I can do, which doesn't crash but also does not appear to do anything at all:
var buffer = function(image){
var buffer_bad = image.select('cloud').neq(1)
.and(image.select('shadow').neq(1))
.and(image.select('cirrus').neq(1))
.focal_max({radius: 100, units: 'meters'})
return image.updateMask(buffer_bad)
}
What I'm trying to make happen there is to take the existing mask, nibble way at the edges by 100 meters, and then update the mask.
Here's the full snippet of relevant code for context:
var cld = require('users/fitoprincipe/geetools:cloud_masks')
var mask_all = function(image) {
var hollstein_mask = cld.hollstein_S2(['shadow', 'cloud', 'cirrus'])(image)
return hollstein_mask
};
var buffer = function(image){
var buffer_bad = image.select('cloud').neq(1)
.and(image.select('shadow').neq(1))
.and(image.select('cirrus').neq(1))
.focal_max({radius: 100, units: 'meters'})
return image.updateMask(buffer_bad)
}
var allclear = ee.ImageCollection('COPERNICUS/S2')
// Pre-filter to get less cloudy granules over the ROI.
.filter(ee.Filter.calendarRange(1, 2, 'month'))
.filterBounds(ROI)
.map(mask_all)
.map(buffer)
.median();