问题
I have a tried to convert a RasterLayer 16-bit into a RasterLayer 8-bit in R, but I did not have success. Any ideas ?
Thanks!
回答1:
If you want to convert your RasterLayer from 16-bit to 8-bit you need to stretch your values into the 8-bit interval (0-255 unsigned) first. Then you can save it as an 8bit image:
#sample raster
library(raster)
ras16b <- raster(x=matrix(as.integer(rnorm(180*180,1000,50)),180,180))
#convert to 0-255 using the calc. function and basic raster algebra
ras8b <- calc(ras16b, fun=function(x){((x - min(x)) * 255)/(max(x)- min(x)) + 0})
#export 8b raster
writeRaster(ras8b, '/bla/bla/ras8b.tif', datatype='INT1U')
You can find more information on how to normalize values into the 0-255 interval here
来源:https://stackoverflow.com/questions/31953180/rasterlayer-16-bits-into-a-rasterlayer-8-bits