Calculate and extract layer of NDVI change in Google Earth Engine

My experience and expertise in Google Earth Engine are super low. My study area is Sundarban, Bangladesh. I have 2 NDVI from 2 years (2016 & 2017). I want to find the vegetation change by subtracting these two layers. But my study area also contains unwanted water bodies. So, I need to calculate vegetational changes by excluding water bodies. After calculating the difference I need a layer of this changed vegetative area.

Any kind soul to help me out solving this problem?

``````/// Masking clouds
// // Create the cloud masking function.
var cloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
var cloudLikelihood = cloudScore.select('cloud');
var cloudPixels = cloudLikelihood.lt(cloud_thresh);
};
////For 2016 image
var landsat8_collection_2016 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(Sundarban)
.filterDate('2016-01-01', '2016-01-31');
// Reduce the collection to the median value per pixel.
var median_L8_2016 = landsat8_collection_2016_NoClouds.median()
.clip(Sundarban);
// Create an NDVI image using bands the NIR and red bands (5 and4) for 2016.
var NDVI = median_L8_2016.normalizedDifference(['B5', 'B4']);

////For 2016 image
var landsat8_collection_2017 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(Sundarban)
.filterDate('2017-01-01', '2017-01-30');

.filterDate('2017-01-01', '2017-01-30');

// // Reduce the collection to the median value per pixel.
var median_L8_2017 = landsat8_collection_2017_NoClouds.median()
.clip(Sundarban);

// Create an NDVI image using bands the NIR and red bands (5 and4) for 2017.
var NDVI17 = median_L8_2017.normalizedDifference(['B5', 'B4']);
``````

I used the water masking approach described here, which uses the Hansen forest dataset to mask out water. That is a global dataset so it may be suitable for your study area. Since you didn't provide a study area, I used a county in Maine as a stand-in (and since you didn't provide a cloud threshold, I used 50).

This approach will use a constant mask for water across the years of your study, rather than adjusting the size of the mask with respect to changing lake conditions. After mapping the cloud mask to your `ImageCollection`s and finding the median NDVI, we can then mask the median images using the water mask. After creating the masked `Image`s, use mathematical operations to find the band difference. Set some visualization parameters, make the subtraction, and check your work with `Map.addLayer()`.

``````var maineCounties = ee.FeatureCollection('TIGER/2016/Counties')
.filter(ee.Filter.eq('NAME', 'Waldo'));
print(maineCounties);
var Sundarban = maineCounties;

// Load or import the Hansen et al. forest change dataset.
var hansenImage = ee.Image('UMD/hansen/global_forest_change_2015');

// // Create the cloud masking function.
var cloudScore = ee.Algorithms.Landsat.simpleCloudScore(image);
var cloudLikelihood = cloudScore.select('cloud');
var cloudPixels = cloudLikelihood.lt(50);
};
////For 2016 image
var landsat8_collection_2016 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(Sundarban)
.filterDate('2016-01-01', '2016-01-31');
var landsat8_collection_2016_NoClouds = landsat8_collection_2016
// Reduce the collection to the median value per pixel.
var median_L8_2016 = landsat8_collection_2016_NoClouds
.median()
.clip(Sundarban)

// Create an NDVI image using bands the NIR and red bands (5 and4) for 2016.
var NDVI16 = median_L8_2016.normalizedDifference(['B5', 'B4']);

////For 2017 image
var landsat8_collection_2017 = ee.ImageCollection('LANDSAT/LC08/C01/T1_TOA')
.filterBounds(Sundarban)
.filterDate('2017-01-01', '2017-01-30');

var landsat8_collection_2017_NoClouds = landsat8_collection_2017
.filterDate('2017-01-01', '2017-01-30');

// // Reduce the collection to the median value per pixel.
var median_L8_2017 = landsat8_collection_2017_NoClouds
.median()
.clip(Sundarban)
// Create an NDVI image using bands the NIR and red bands (5 and4) for 2017.
var NDVI17 = median_L8_2017.normalizedDifference(['B5', 'B4']);

// Create some visualization parameters
var ndviParams = {min: -1, max: 1, palette: ['blue', 'white', 'green']};
var diffParams = {min: -1, max: 1, palette:['red','white','green']};
// Center the map layer
Map.centerObject(Sundarban, 7);

var NDVIdiff = NDVI17.subtract(NDVI16);