# Pixel wise NDVI calculation in GEE

I have a collection of Landsat images filtered over forest regions of India over a time interval of 6 years(2013 - 2018). I have again filtered this collection such that I have one image per 4 months, that is I have filtered on a quarterly basis so that I will have 3 images per year.

Now, for a particular point, there will be 18 pixels representing it over 6 years. I have to find, out of these 18 pixels, how many satisfy the condition - NDVI < 0.25. Suppose, 10 pixels satisfy this condition, then in my output image, the pixel representing that particular point should have its value = 10. Like this I have to do for all the points in my selected region.

I have no idea as to how to do this pixel wise computation. Can somebody help me on this?

In my code, I have done till filtering quarterly wise.

``````// FILTERING OUT FOREST COVER IN INDIA

var treeCanopyCover = hansen.clip(india).select('treecover2000');
var greater25 = treeCanopyCover.gte(25);

// NDVI FUNCTION

function addNDVI(image) {
var ndvi = image.normalizedDifference(['B5', 'B4']).rename('NDVI');
return image.addBands(ndvi);
}

// FILTERING THE COLLECTION

var startDate = ee.Date('2013-01-01');
var endDate = ee.Date('2019-06-30');
var filtered = l8.filterBounds(india)
.filterDate(startDate, endDate)
.filterMetadata('CLOUD_COVER','less_than',25)
.map(addNDVI);

var maskedCollection = filtered.map(function(image){
var x = image.updateMask(greater25);      // filtering over forest regions
return x;
});

// FILTERING QUARTERLY WISE

var map_m = function(y)
{
var months = [1, 5, 9];
var filtered_col = months.map(function(m)
{
var start = ee.Date.fromYMD(y, m, 1)
var end = start.advance(3, 'month');
var filt = maskedCollection.filterDate(start,end).mean();

return filt.set('year', y)
.set('month', m)
.set('system:time_start',ee.Date.fromYMD(y,m,1)) ;
});
return filtered_col;
};

var years = ee.List.sequence(2013, 2019);
var quarterlyimages = years.map(map_m).flatten();
var z = ee.ImageCollection(quarterlyimages);
``````

## 1 Answer

I think `count` method of `ImageCollection` could help with what you're doing.

Assume `col` is the `ImageCollection` that you want to deal with. Every image in this `col` has `NDVI` band that holds NDVI value. The code below will result in an image called `NDVI_count` in which value of every pixel represents the number of pixels at the corresponding location in `col` that have NDVI < 0.25.

``````var NDVI_count = col.select('NDVI')
.map(function(img) { return img.updateMask(img.select('NDVI').lt(0.25)) })
.count()
``````

The `map` function in the above code just does one thing: mask out any pixels that have NDVI >= 0.25. The `count` method will count the number of valid pixels (i.e. not-masked pixel) throughout the collection at each specific location.

Hope this helps.

• This gives me the number of all pixels with NDVI less than 0.25. I need the number of pixels specific to a particular point, say for a particular longitude and latitude. – Reema Mathew Jul 9 '19 at 4:14
• Sorry if I was not clear enough. It does give you the number of pixels with NDVI less than 0.25 at each particular location. – Kevin Jul 9 '19 at 5:40
• Yeah, it worked. Sorry for the misunderstanding. I didn't expect that such a small single inbuilt function could solve this. Thank you very very much – Reema Mathew Jul 9 '19 at 9:41
• Just one more question: If I have to plot a bar chart or histogram showing the frequency of each count, that is how many pixels in the output image have the value (say, 3), how can I do that? @Kevin – Reema Mathew Jul 9 '19 at 9:52
• @ReemaMathew, you can use `ui.Chart.image.histogram` to create such chart. To specify which chart type, `setChartType` can be used after creating the chart. – Kevin Jul 9 '19 at 11:25