Histogram Equalization on Sentinel-2 L1C Satellite Images
For those wrestling with satellite imagery, clouds can be a real pain. They overexpose scenes, making it tough to get a clear picture of both the atmospheric and terrestrial elements. No worries, though! Today we’re tackling this issue with a technique called histogram equalization on Sentinel-2 L1C images. 🌦️
Why Histogram Equalization?
Imagine trying to look at a bright cloud and a shaded forest in the same image. Standard visualization will wash out one or the other. Histogram equalization adjusts the range of brightness values in your image, giving both clouds and land their moment in the sun, or well, your screen.
The Code Explained
Let’s jump into our Python code. We’re using the libraries numpy, rasterio, pathlib and tqdm.
Paths and Bands
We first define the paths to our input and output directories. We also specify the RGB bands we’re interested in (4, 3, 2).
Histogram Functions
We have two core functions: get_histogram
to calculate the histogram and histogram_equalization
to apply equalization.
Looping Through Bands
Then we loop through each band, applying histogram equalization.
Why uint8 and JPEG Compression?
We convert the equalized array to uint8
to save space. It still maintains good enough quality for visualization. We then use JPEG compression in our GeoTIFF to save even more space.
Finale: Writing the Image
Finally, we write the image into a GeoTIFF file.
Before and After
Before Equalization After Equalization
Notice how you can see both clouds and land features clearly in the equalized image. So the next time clouds crash your image party, you know how to make them behave!
That’s all folks, happy equalizing! 🌈🛰️
Acknowledgements
Some of the ideas and techniques discussed in this blog post were inspired by Tory Walker’s GitHub repository on histogram equalization, which you can find here.