I'm having a little trouble masking non-vegetation areas in my images.

I'm using Sentinel-2 images, and I have land use and cover across my country (Brazil). To this end, I reclassified this land use and cover image into (1 - forest and natural vegetation areas) and (0 - for other uses). With this I'm trying to mask everything that is not vegetation, that is, I don't want anything to be visualized (0). However, I can't get a result with the script I've been adapting.

function maskS2clouds(image) {
  var qa = image.select('QA60');

  // Bits 10 and 11 are clouds and cirrus, respectively.
 var cloudBitMask = 1 << 10;
  var cirrusBitMask = 1 << 11;

  // Both flags should be set to zero, indicating clear conditions.
  var mask = qa.bitwiseAnd(cloudBitMask).eq(0)
      return image.updateMask(mask).divide(10000);


// Map the function over one year of data and take the median.
// Load Sentinel-2 TOA reflectance data.
var dataset = ee.ImageCollection('COPERNICUS/S2')
                 .filterDate('2019-06-01', '2019-08-31')
                 .select(['B8','B4', 'B3', 'B2','QA60'])
                                    // Pre-filter to get less cloudy granules.
                 .filter(ee.Filter.lt('CLOUDY_PIXEL_PERCENTAGE', 20))

//there's i make function which convert imagecollection to one image with bands
var mergeBands = function(image, previous) {
  return ee.Image(previous).addBands(image, ['B8', 'B4', 'B3','B2']);

var merged = ee.Image(dataset.iterate(mergeBands, ee.Image([])));
print('merged', merged);

var MAPBIOMAS20 = image
//var MAPBIOMAS20_filtrada = image.remap([3,4,12],[3,4,12])
// não pode usar o .map para apenas uma imagem
//var reclassified = function(image) {
//  return image.remap([3,4,12],[3,4,12], 'vegetation');
//var new_ds = MAPBIOMAS20.map(reclassified)

// Remap values.
var MAPBIOMAS20reclass = ee.Image(1)
         .where(MAPBIOMAS20.gt(0).and(MAPBIOMAS20.lte(5)), 1)
         .where(MAPBIOMAS20.gt(10).and(MAPBIOMAS20.lte(12)), 1)
         .where(MAPBIOMAS20.gt(6).and(MAPBIOMAS20.lte(9)), 0)
         .where(MAPBIOMAS20.gt(12).and(MAPBIOMAS20.lte(50)), 0)

//Map.addLayer(MAPBIOMAS20reclass, {min:0, max:1}, 'MAP20RCLASS', true)
//Map.addLayer(MAPBIOMAS20reclass, {min:0, max:12}, 'MAP20RCLASS', true)
var CERRADO_VEG = MAPBIOMAS20reclass.clip(table);
Map.addLayer(CERRADO_VEG, {min:0, max:1}, 'MAP20RCLASS_cerrado', true)

//var CERRADO_VEG = MAPBIOMAS20reclass.clip(table);

// Create a Boolean land mask from the SWIR1 band; water is value 0, land is 1.
var landMask = CERRADO_VEG.select('constant').gt(0);
print('Land mask', landMask);
Map.addLayer(landMask, {palette: ['yellow', 'green']}, 'Land mask');

var trueColorViz = {
  bands: ['B4', 'B3', 'B2'],
  min: 0,
  max: 2700,
  gamma: 1.3

// Apply the single-band land mask to all image bands; pixel values equal to 0
// in the mask become invalid in the image.
var imgMasked = merged.updateMask(landMask);
print('Image, land only', imgMasked);
Map.addLayer(imgMasked, trueColorViz, 'Image, land only');

After that I still intend to obtain vegetation indices for my area, but I already want to exclude what is not vegetation.

Can someone help me?


Your Answer

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