'Apply linear stretch to Landsat 8 images in the tmap R package

I have a Landsat 8 dataset downloaded from the USGS. I import into R using the following code:

library(raster)
library(tmap)

B1 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B1.TIF")
B2 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B2.TIF")
B3 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B3.TIF")
B4 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B4.TIF")
B5 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B5.TIF")
B6 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B6.TIF")
B7 <- raster("LC08_L2SP_166072_20210819_20210827_02_T1_SR_B7.TIF")

fcc_nir <- stack(B5, B4, B3)

The images are 16 bit. I can plot the false-colour composite image quite easily using this command:

plotRGB(fcc_nir, stretch = "lin")

Landsat FCC image generated using PlotRGB()

However, I need to overlay polygons and add a map grid etc. The tmap package makes great maps but I still have to figure out how to apply the same linear stretch in tmap. There is no obvious stretch option. When I plot the false-colour composite image, it appears dark:

tm_shape(fcc_nir) +
  tm_rgb(max.value = 65536)

Landsat FCC image generated using tm_rgb()

Do I need to rescale the image manually beforehand?

It would be nice if tm_rgb() had a stretch parameter like plotRGB(). Thanks in advance for any help/guidance!



Solution 1:[1]

tm_rgb() has limited options. The plotRGB() function appears to use a sample of raster cell values to perform a stretch. That is why images appear brighter and more contrasty. This is the best I could achieve with tm_rgb():

fcc_nir <- setMinMax(fcc_nir)

tm_shape(fcc_nir) +
  tm_rgb(max.value = max(maxValue(fcc_nir)))

best stretch

tm_rgb() has no min.value = option. One can enter a smaller max.value but this is a bit hit and miss. I would prefer to use a more rigorous approach based on image statistics.

tm_shape(fcc_nir) +
  tm_rgb(max.value = 31000)

arbitrary user-defined max.value

One has to manually rescale images beforehand using the raster::stretch() or some user-defined stretch function to get a brighter output.

For example:

fcc_nir_s <- stretch(fcc_nir, minv = 0, maxv = 255, minq = 0.1, maxq = 0.99)

tm_shape(fcc_nir_s) +
  tm_rgb()

produces this image:

stretched image

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1