简体   繁体   中英

RasterLayer 16-bits into a RasterLayer 8-bits

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM