Skip to content

Instantly share code, notes, and snippets.

@anttilipp
Created February 12, 2017 18:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anttilipp/91080cbc6f81d938aae9df9db1ecbcb1 to your computer and use it in GitHub Desktop.
Save anttilipp/91080cbc6f81d938aae9df9db1ecbcb1 to your computer and use it in GitHub Desktop.
A quick test script for analyzing changes in Sentinel-2 R, G, and B band satellite images
#!/bin/bash
#
# A quick test script for analyzing changes in Sentinel-2 R, G, and B bands
# Made by Antti Lipponen (Twitter @anttilip)
# 12 February 2017
#
# keywords: satellite, Sentinel-2, remote sensing, data processing, gdal, imagemagick
#
########################
# If data does not exist, download it
# Tile 11/S/LT (near Los Angeles) 9 Feb 2016
# http://sentinel-s2-l1c.s3-website.eu-central-1.amazonaws.com/#tiles/11/S/LT/2016/2/9/0/
if [ ! -d "LosAngeles20160209" ]; then
mkdir LosAngeles20160209
wget -O LosAngeles20160209/B04.jp2 http://sentinel-s2-l1c.s3.amazonaws.com/tiles/11/S/LT/2016/2/9/0/B04.jp2
wget -O LosAngeles20160209/B03.jp2 http://sentinel-s2-l1c.s3.amazonaws.com/tiles/11/S/LT/2016/2/9/0/B03.jp2
wget -O LosAngeles20160209/B02.jp2 http://sentinel-s2-l1c.s3.amazonaws.com/tiles/11/S/LT/2016/2/9/0/B02.jp2
fi
# Tile 11/S/LT (near Los Angeles) 14 Jan 2017
# http://sentinel-s2-l1c.s3-website.eu-central-1.amazonaws.com/#tiles/11/S/LT/2016/2/9/0/
if [ ! -d "LosAngeles20170114" ]; then
mkdir LosAngeles20170114
wget -O LosAngeles20170114/B04.jp2 http://sentinel-s2-l1c.s3.amazonaws.com/tiles/11/S/LT/2017/1/14/0/B04.jp2
wget -O LosAngeles20170114/B03.jp2 http://sentinel-s2-l1c.s3.amazonaws.com/tiles/11/S/LT/2017/1/14/0/B03.jp2
wget -O LosAngeles20170114/B02.jp2 http://sentinel-s2-l1c.s3.amazonaws.com/tiles/11/S/LT/2017/1/14/0/B02.jp2
fi
# Delete all image files if they exist
rm -f *.jpg *.tif
# Crop tile to a smaller region of interest & reproject to utm zone 11
# 2016
gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -of GTiff -te 380316 3726819 405999 3747456 -tr 10 10 -r cubicspline LosAngeles20160209/B04.jp2 R2016.tif
gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -of GTiff -te 380316 3726819 405999 3747456 -tr 10 10 -r cubicspline LosAngeles20160209/B03.jp2 G2016.tif
gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -of GTiff -te 380316 3726819 405999 3747456 -tr 10 10 -r cubicspline LosAngeles20160209/B02.jp2 B2016.tif
# 2017
gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -of GTiff -te 380316 3726819 405999 3747456 -tr 10 10 -r cubicspline LosAngeles20170114/B04.jp2 R2017.tif
gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -of GTiff -te 380316 3726819 405999 3747456 -tr 10 10 -r cubicspline LosAngeles20170114/B03.jp2 G2017.tif
gdalwarp -t_srs '+proj=utm +zone=11 +datum=WGS84' -of GTiff -te 380316 3726819 405999 3747456 -tr 10 10 -r cubicspline LosAngeles20170114/B02.jp2 B2017.tif
# construct RGB images (convert will generate warnings because of geoinformation in tiffs)
convert R2016.tif G2016.tif B2016.tif -combine -colorspace sRGB -level 0,5000 -unsharp 5x2+1+0 -sampling-factor 4:2:2 -quality 98 RGB2016.jpg
convert R2017.tif G2017.tif B2017.tif -combine -colorspace sRGB -level 0,5000 -unsharp 5x2+1+0 -sampling-factor 4:2:2 -quality 98 RGB2017.jpg
# construct histogram equalized & sharpened R, G, B images (and convert to jpg)
convert R2016.tif -unsharp 5x2+1+0 -level 0,5000 -equalize -sampling-factor 4:2:2 -quality 98 R2016eq.jpg
convert G2016.tif -unsharp 5x2+1+0 -level 0,5000 -equalize -sampling-factor 4:2:2 -quality 98 G2016eq.jpg
convert B2016.tif -unsharp 5x2+1+0 -level 0,5000 -equalize -sampling-factor 4:2:2 -quality 98 B2016eq.jpg
convert R2017.tif -unsharp 5x2+1+0 -level 0,5000 -equalize -sampling-factor 4:2:2 -quality 98 R2017eq.jpg
convert G2017.tif -unsharp 5x2+1+0 -level 0,5000 -equalize -sampling-factor 4:2:2 -quality 98 G2017eq.jpg
convert B2017.tif -unsharp 5x2+1+0 -level 0,5000 -equalize -sampling-factor 4:2:2 -quality 98 B2017eq.jpg
# construct average images 0.5 * (2016 + 2017)
convert R2016eq.jpg R2017eq.jpg -average -sampling-factor 4:2:2 -quality 98 Raverage.jpg
convert G2016eq.jpg G2017eq.jpg -average -sampling-factor 4:2:2 -quality 98 Gaverage.jpg
convert B2016eq.jpg B2017eq.jpg -average -sampling-factor 4:2:2 -quality 98 Baverage.jpg
# construct change images
convert R2017eq.jpg Raverage.jpg R2016eq.jpg -combine -colorspace sRGB -sampling-factor 4:2:2 -quality 98 ChangeR.jpg
convert G2017eq.jpg Gaverage.jpg G2016eq.jpg -combine -colorspace sRGB -sampling-factor 4:2:2 -quality 98 ChangeG.jpg
convert B2017eq.jpg Baverage.jpg B2016eq.jpg -combine -colorspace sRGB -sampling-factor 4:2:2 -quality 98 ChangeB.jpg
# combine to a single image
convert ChangeR.jpg ChangeG.jpg ChangeB.jpg +append -sampling-factor 4:2:2 -quality 98 ChangeRGB.jpg
# and we are done!
# red in change images indicates increased intensity and blue decreased intensity
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment