Image Compositing, Masking, Mosaicking, Projection, Visualization and Earth Engine Image Exporting

geedevsnairobi

geedevsnairobi

Posted on October 29, 2023

Image Compositing, Masking, Mosaicking, Projection, Visualization and Earth Engine Image Exporting

This week's focus will be a deep dive into exploring imageCollections.

The learning objectives of this article are:

  1. Image Compositing
  2. Image Masking and mosaicking
  3. Image Projection and Reprojection
  4. ImageCollection visualization
  5. ImageCollection Exporting to Google Drive

Like any other data science project, our first step is to define our region of interest. On defining the area of interest, we will set the map output at the center. For this project, our objective is to create a composite for Nairobi region in Kenya. We will then use Landsat 8 USGS Landsat 8 Collection 2 Tier 1 TOA Reflectance. Let us load Landsat 8 from the Earth Engine Data Catalogue. We will search Landsat 8, USGS Landsat 8 Collection 2 Tier 1 TOA Reflectance through the search bar then click import. The below image details will appear. The image contains multiple properties which can be explored for easy image preprocessing. At this page, we can import again at the lower button to load the data into Earth Engine code editor.

Image description

When we import the image, it will appear at the top of Earth Engine code editor under the imports as a variable with a default name imageCollection for the case of Image Collections. If
To produce filter imageCollection we will narrow to the year of 2016 by filtering by dates with use of the ee.ImageCollection.filterDate function then filter by region of interest using ee.ImageCollection.filterBounds functions.

Let us try to access Landsat-8 into Earth Engine editor

var Landsat8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
                .filterDate('2016-01-01', '2016-12-31')
                .filterBounds(roi);
var dataset = Landsat8.select(['B4', 'B3', 'B2']);
Enter fullscreen mode Exit fullscreen mode

When the image is loaded it is important to position the image at the center of the Earth Engine interface by issuing coordinates for our region of interest and a zoom level. To center our image, we use Map.setCenter function. We then add Map.addLayer function to visualize the image at the center of the Earth Engine Interface.

ImageCollection Compositing

Image compositing is the technique in Google Earth Engine used to combine spatially overlapping images into a single image based on an aggregation function like Mean, Median etc.
For example, we can create an image composite by applying a selection criterion to each pixel based on all pixels in the stack. The images could be taken at different times.
Here we use the .median() function to create a composite where each pixel value is the median of all pixels from the stack.

Image description

If we want to create an image composite, ee.ImageCollection class is an handy function. It allows image collection manipulation and processing of large image collections. Let us try to create an image composite with ee.ImageCollection.median().
Other functions which can used to create an image composite could be ee.ImageCollection.qualityMosaic(), and ee.ImageCollection.mosaic().

Let us try compositing our imagecollection by mean function.

// Image compositing
var compositing = dataset.median();
Map.addLayer(compositing, visParams, 'This is an Image Composite'); 
Enter fullscreen mode Exit fullscreen mode

Image Mosaicking

Besides image compositing we can also perform image mosaicking which is the process of assembling spatial image datasets to produce a single spatially continuous image. During mosaicking process, each pixel in the output image is taken from a different input image. Mosaicking process is often used to fill in a gap of data – used when there is missing data in some of the input images and there is a need for a complete image.

var mosaicking = dataset.mosaic();
// Display the median imageColle after mosaicing.
Map.addLayer(mosaicking, visParams, 'This is mosaiced Image');
Enter fullscreen mode Exit fullscreen mode

Earth Engine Reducers

Earth Engine Reducers are objects used aggregate data over time, bands, space, and arrays.
When computing an imageCollection ee.Reducer class specifies the technique of data aggregation. Let us say you want to perform simple statistics, you can use the aggregation functions such as (min, mean, max, median, and standard deviation). It is important to note that, reducers take input dataset to produce a single output. Reducers result to a single image upon successful completion of pixel computations.

var Landsat8 = ee.ImageCollection('LANDSAT/LC08/C02/T1_TOA')
                .filterDate('2016-01-01', '2016-12-31')
                .filterBounds(roi);             
var dataset = Landsat8.select(['B4', 'B3', 'B2']);              
// Define visualization parameters
var visParams = {
bands: ['B4', 'B3', 'B2'], 
min: 0.0,
max: 0.4};
Map.setCenter(36.90966120185066,-1.2781566341946728, 6);
Map.addLayer(dataset, visParams, 'Image before reduction is done');
Enter fullscreen mode Exit fullscreen mode

There are different ways to reduce an image. I will share some code here for more exploration

// Get the median,mean,max,min,sum,product over time, in each band, in each pixel.
var median = dataset.median();
Map.addLayer(median, visParams, ' Median Reduction');

var mean = dataset.mean();
Map.addLayer(mean, visParams, ' mean Reduction');

var max = dataset.max();
Map.addLayer(max, visParams, ' Max Reduction');

var min = dataset.min();
Map.addLayer(min, visParams, ' min Reduction');

var sum = dataset.sum();
Map.addLayer(sum, visParams, ' sun Reduction');

var product = dataset.product();
Map.addLayer(product, visParams, ' product Reduction');
Enter fullscreen mode Exit fullscreen mode

ImageCollection Projection & Re-projection

Unless you need to perform a specific computation, there is no need to specify a new projection.

There is no need to worry about map projection when working in Google Earth Engine because the computation scale is a pull function. Inputs are asked during projection process and the outputs can be determined from functions like (crs). A projection propagates back via sequence of operations.

ImageCollection Visualization

ImageCollection Visualization is the activity of displaying the prepared image on the map to extract insightful information or exportinting it to drive, assets, or Google cloud for extended analysis. When an image is processed it cannot appear automatically on the map, as a result we need to write some code to display our processed image. Image Compositing can be visualized as either an animation or a series of thumbnails. We need to add ImageCollection layer to the map panel then read it using Map.addLayer method. In it we have to define the visualization parameters, bands, minimum and maximum values, color palette and opacity for better image visualization. In case you add a new map layer without any additional parameters, the code editor assigns the first three bands to red, green and blue, respectively. To achieve a good visualization effect it is advisable you provide image visualization parameters to Map.addLayer() method.

Earth Engine ImageCollection Exporting

Image you have prepared your image and it is now time to export it for further analysis with external softwares. Exporting Images can take different forms in Earth Engine, firstly could export in form of a GeoTIFF or TFRecord format. Other exportable items from Earth Engine include map tiles, tables and video.

Places to export these earth engine items;

  • Export to Google Drive
  • Export to Google Cloud Storage
  • Export to a new Earth Engine asset.

The first step in image exporting process is defining the data to be exported. Next is to define the projection parameters. In this case, we use the crs parameter to specify the coordinate system and the crsTransform parameter to precisely specify the pixel grid. When done it is time to export an image to Drive account, using the Export.image.toDrive()function.

// Export the image, specifying the CRS, transform, and region.
Export.image.toDrive({
  image: landsat,
  description: 'imageToDriveExample_transform',
  crs: projection.crs,
  crsTransform: projection.transform,
  region: geometry
});
Enter fullscreen mode Exit fullscreen mode

On running the above code snippet an export task is created in the Code Editor Task tab. Go to this tab and click Run button to start it. When completed, the image will be created in your Google Drive account within the specified folder and fileFormat.

💖 💪 🙅 🚩
geedevsnairobi
geedevsnairobi

Posted on October 29, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related

What was your win this week?
weeklyretro What was your win this week?

November 29, 2024

Where GitOps Meets ClickOps
devops Where GitOps Meets ClickOps

November 29, 2024