Band combination in R

Combining multiple bands into a single composite image is a common task in remote sensing and image processing. In R, this can be easily accomplished using the raster package, which provides functions for reading, manipulating, and writing raster images.

  1. Load the necessary libraries for the process: rgdal and raster.
    • raster: for reading, manipulating, and writing raster images.
    • rgdal: for reading and writing images in different vector and raster formats.

library(rgdal)
library(raster)

2. Use the setwd() function to set the working directory where the individual band images are located.

setwd("D:/Documents/combinations")

3. Use the raster() function to load each individual band into a separate variable.

band1 <- raster("LC08_L1TP_010063_20220614_20220617_02_T1_B5.tif")
band2 <- raster("LC08_L1TP_010063_20220614_20220617_02_T1_B6.tif")
band3 <- raster("LC08_L1TP_010063_20220614_20220617_02_T1_B2.tif")

4. Create a list with the individual bands loaded in the previous variables (band1, band2, band3).

band_list <- list(band1, band2, band3)

5. Use the stack() function to combine the individual bands into a single composite image.

composite_image <- stack(band_list)

6. Use the writeRaster() function to save the composite image in the previously established working directory.

writeRaster(composite_image, "Vegetation_analysis.tif")

Made with ChatGPT

2 thoughts on “Band combination in R”

  1. My two cents, I try to unify the language everytime I write a post. If the article is written in English, I’ll write the code in English too.

    Reply
    • I would like to thank you for your observation. I have now the code to English. As a reminder, please note that the variable names and the output file name can be assigned by you according to your preferences.

      Reply

Leave a Comment