# Pan-sharpening Using R

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Misanthrope's Thoughts**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

In my previous post I described how to perform pan-sharpening using OrfeoToolbox and QGIS. This time I will show you how to do this in R. At the bottom you will find several functions I wrote on top of the ‘raster’ package that allow a convenient pan-sharpening in R.

### Motivation

You may wonder why I even bothered myself with pan-sharpening in R when I already have a nice model for pan-sharpening in QGIS. See, one can’t control the data-type of the imagery returned by pan-sharpening that involves OTB. This leads to some unpleasant consequences: during pan-sharpening one will get floating point pixel values even if in initial values were integers. So for example a 600 MiB multi-spectral imagery (with integer pixel values) after pan-sharpening will grow to 5.2 GB. But if we will change datatype of the resulting imagery to force it store only integers it size will be reduced from 5.2 to 2.8 GB which is a huge difference. ‘raster’ package in R allows to control output datatype. Plus using R you can play with different filtering options to play with.

### The Theory

In OTB pan-sharpening is performed using following well-known formula:

Where i and j are pixels indices, PAN is the panchromatic image, XS is the multi-spectral image and PAN

We will implement exact the same approach using ‘raster’ package for R.

_{smooth}is the panchromatic image smoothed with a kernel to fit the multi-spectral image scale.We will implement exact the same approach using ‘raster’ package for R.

### Code Usage and Result

As pan-sharpening is the type of procedure that will reoccur over some time I decided to write generic functions for pan-sharpening itself and for saving the results to have easier time in future.

The usage is as simple as:

pan <- raster('pan.tif')

multi <- brick('multi.tif')

pansharp <- processingPansharp(pan, multi)

output_path <- 'path_and_filename_without_extention'

saveResult(pansharp, output_path)

Here you are the example results from the script and from the OTB model for one of the illegal landfills in Russia:

Initial multi-band raster |

Initial panchromatic raster |

Result of pan-sharpening using R script |

Result of pan-sharpening using OTB |

Which output do you like better: from OTB or R? Comparing both output results you can notice that the output from R bears heavier filtering markings than the one from OTB. On the other hand R output has more hues of the green colours which actually helps in distinguishing different types of vegetation. As you will see in the code – one can easily adjust or modify procedure of filtering panchromatic raster (extractLPF() function) in order to get desired output.

### The code

library(raster) # Create needed functions ------------------------------------------------- pansharpFun

To

**leave a comment**for the author, please follow the link and comment on their blog:**Misanthrope's Thoughts**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.