In our post on rio color
we introduced a Rasterio plugin for color-balancing geospatial imagery.
By adjusting gamma, saturation, and contrast we can correct colors to produce a more attractive and consistent basemap.
rio hist
takes a different approach to a similar problem. Instead of adjusting the
color balance manually, we can use histogram matching to automatically match the
colors of a source image to look similar to a reference image. Here’s how it works:
We start with a source image to be adjusted. In this case we have a 2005 aerial photo of Disneyland in Anaheim, CA taken at 1 meter resolution. It has good detail but unbalanced colors; too dark and tinted green.
Compare that with our reference image of the same area. The resolution is not great but the color balance is more realistic and attractive:
Ideally we could retain all of the detail of the source with the color balance of the reference. Enter histogram matching, using rio hist
on the command line:rio hist --color-space RGB source.tif reference.tif final.tif
To understand what’s happening under the hood, rio hist provides a --plot
option which shows the source, reference, and final histograms along with the cumulative distribution function, or CDF, that is used to perform the matching.
Because histogram matching with red, green, and blue bands can occasionally introduce hue distortions, we’ve added the ability to work in other color spaces with more perceptually uniform scales. For more details on the histogram matching algorithm, check out the ipython notebook, which walks through the implementation details.
The histogram matching approach provides an automated method for achieving color consistency
across many disparate sources. We use rio hist
in our image processing pipeline so that new image acquisitions stay consistent with our mapbox-satellite
basemap. By using the Rasterio plugin architecture we get a consistent command line interface and Python API with excellent performance and scalability.
Interested in trying histogram matching on your own images? The rio hist
plugin is available on github and can be installed using pip install rio-hist
. Reach out to us on Twitter: @Mapbox if you have any questions.