Problem with raster plots RGB from 3 raster layers

rstudio

#1

Good night, gentlemens.

I am trying to compose a RGB raster in R, but it's showing a error message:

Error in grDevices::rgb(RGB[, 1], RGB[, 2], RGB[, 3], alpha = alpha, max = scale) :
color intensity 923, not in 0:255

This is the routine:

require(raster)
library(rgdal)
band_red <- raster("banda_red.tif")
band_blue <- raster("banda_blue.tif")
band_green <- raster("banda_green.tif")
s <- stack(band_blue,band_green,band_red)
plotRGB(s, r=3, g=2, b=1)

Best regards,


#2

This suggests that your data need to be rescaled. Can you share the range of values?

This is enough to get raster to print our ranges for each band

print(s)

otherwise to be more explicit

cellStats(s, min)
cellStats(s, max)

Since one of your values - 923 - is not something obvious, like 1 or 255 then it's not clear what the scaling should be.

If you know the maximum and 0 is the minimum then this would be enough:

plotRGB(s, scale = [max])

Generally you can update each band like this, but without knowing what the overall range of values is or what they represent this is unlikely to be sensible as we need a common from value, not the range of values in any given band.

## danger!!  we also need from = ??
setValues(band_red, scales::rescale(values(band_red), to = c(0, 255)))  

Down under the hood the rgb function expects values in 0, 1 unless you specify a maxColorValue (that's what raster::plotRGB is doing with max = scale as reported in the error message.


#3

Take look:

print(band_blue)
class : RasterLayer
dimensions : 209, 197, 41173 (nrow, ncol, ncell)
resolution : 9.987289, 9.999058 (x, y)
extent : 752253.2, 754220.7, 8174521, 8176611 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Pessi\Downloads\Nova pasta\seriema rgb\banda_blue.tif
names : banda_blue
values : 729, 1971 (min, max)

print(band_green)
class : RasterLayer
dimensions : 209, 197, 41173 (nrow, ncol, ncell)
resolution : 9.987289, 9.999058 (x, y)
extent : 752253.2, 754220.7, 8174521, 8176611 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Pessi\Downloads\Nova pasta\seriema rgb\banda_green.tif
names : banda_green
values : 504, 2304 (min, max)

print(band_red)
class : RasterLayer
dimensions : 209, 197, 41173 (nrow, ncol, ncell)
resolution : 9.987289, 9.999058 (x, y)
extent : 752253.2, 754220.7, 8174521, 8176611 (xmin, xmax, ymin, ymax)
coord. ref. : +proj=utm +zone=21 +south +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0
data source : C:\Users\Pessi\Downloads\Nova pasta\seriema rgb\banda_red.tif
names : banda_red
values : 328, 3609 (min, max)


#4

I don't really know what to do. I try rescale, but it doesn't work.


#5

I don't see any clear pattern in those values, there's no way you can infer the right scaling from those. I think you'll have to go back deeper into how the data you have were intended to be used - are they landsat? Do you know the steps that went into generating them, or do you have a link to the source?


#6

Good morning.

This imagens are from Sentinel-2. I cut an area of interest as from RGB sentinel-2 on the QGIS. After this, I exported the layers (red, green, and blue) separately.

I used this website: http://neondataskills.org/R/Multi-Band-Rasters-In-R/

Thank you.

Best regards,