Image Statistics with Google Earth Engine: A Step-by-Step Guide

Image Statistics with Google Earth Engine: A Step-by-Step Guide

(Mean, Median, Mode, SD, CV, P25, P75 etc.)

As remote sensing and geospatial analysis become increasingly important in various fields, tools like Google Earth Engine (GEE) are proving invaluable. One common task in image analysis is calculating statistics for satellite imagery. In this post, we’ll walk through how to use GEE to calculate various image statistics and export them as a CSV file.

I’ll cover calculating the mean, median, mode, standard deviation, coefficient of variation, and percentiles for Landsat 8 imagery. By the end of this tutorial, you’ll have a script that you can easily adapt for your own projects.

Setting Up the Script

First, let’s set up our script by importing the Landsat 8 collection and defining our region of interest:

// Import the Landsat 8 image collection
var l8 = ee.ImageCollection("LANDSAT/LC08/C02/T1_L2");        
// Define the region of interest (ROI)
var roi = ee.Geometry.Rectangle([-122.5, 37.5, -122.0, 38.0]);        

This code imports the Landsat 8 Surface Reflectance Tier 1 collection and defines a rectangular region of interest. You can adjust the coordinates to focus on your area of study.

Filtering and Selecting the Image

Next, we’ll filter the collection to get a single image for our analysis:

// Filter the collection by date and region
var image = l8
  .filterDate('2023-01-01', '2023-12-31')
  .filterBounds(roi)
  .first();        
// Select the bands you want to analyze (e.g., B2, B3, B4 for RGB)
var bands = ['SR_B2', 'SR_B3', 'SR_B4'];
var selectedBands = image.select(bands);        

This code filters the collection to include only images from 2023 within our region of interest, then selects the first image. We then specify which bands we want to analyze — in this case, the RGB bands.

Calculating Statistics

Now for the heart of our script — calculating the statistics:

// Calculate statistics for each band
var stats = bands.map(function(band) {
  var bandStats = selectedBands.select(band).reduceRegion({
    reducer: ee.Reducer.mean()
      .combine(ee.Reducer.median(), null, true)
      .combine(ee.Reducer.mode(), null, true)
      .combine(ee.Reducer.stdDev(), null, true)
      .combine(ee.Reducer.percentile([25, 75]), null, true),
    geometry: roi,
    scale: 30,
    maxPixels: 1e9
  });
  
  // Calculate Coefficient of Variation (CV)
  var mean = bandStats.get(band + '_mean');
  var stdDev = bandStats.get(band + '_stdDev');
  var cv = ee.Number(stdDev).divide(mean).multiply(100);
  
  return ee.Feature(null, {
    'band': band,
    'mean': bandStats.get(band + '_mean'),
    'median': bandStats.get(band + '_median'),
    'mode': bandStats.get(band + '_mode'),
    'stdDev': stdDev,
    'cv': cv,
    'p25': bandStats.get(band + '_p25'),
    'p75': bandStats.get(band + '_p75')
  });
});        

This code calculates several statistics for each band:

  • Mean, median, and mode
  • Standard deviation
  • 25th and 75th percentiles
  • Coefficient of variation (CV)

The CV is calculated manually by dividing the standard deviation by the mean and multiplying by 100.

Preparing for Export

Finally, we’ll prepare our statistics for export:

// Convert the results to a feature collection
var statsCollection = ee.FeatureCollection(stats);        
// Export the statistics as a CSV file
Export.table.toDrive({
  collection: statsCollection,
  description: 'landsat8_image_statistics',
  fileFormat: 'CSV'
});// Print the statistics to the console (for quick viewing)
print(statsCollection);        

This code converts our statistics into a feature collection, sets up the export to Google Drive as a CSV file, and prints the results to the console for quick viewing.

Running the Script

To use this script:

  1. Copy the entire script into the Google Earth Engine Code Editor.
  2. Adjust the ROI coordinates and date range as needed for your specific area and time period of interest.
  3. Click the “Run” button to execute the script.
  4. Check the “Tasks” tab to see the export task. Click “Run” next to the task to start the export to your Google Drive.

Conclusion

This script provides a solid foundation for calculating image statistics in Google Earth Engine. You can easily modify it to include additional bands, change the region of interest, or add more statistics as needed for your specific analysis.

Remember, the power of GEE lies in its ability to process vast amounts of geospatial data quickly. This script can be adapted to work with other satellite imagery collections, larger areas, or longer time series, opening up a world of possibilities for your remote sensing projects.

Happy coding, and may your analyses be ever insightful!

This medium post format provides a more narrative structure to the code explanation, making it more accessible to readers who might be new to Google Earth Engine or image statistics. It includes an introduction to set the context, a step-by-step walkthrough of the code with explanations, and a conclusion to wrap up and suggest further applications.


?? More insights & projects: pulakeshpradhan.github.io


要查看或添加评论,请登录

Pulakesh Pradhan的更多文章

社区洞察

其他会员也浏览了