[R] Creating mean TIF

Mike Smith mike at hsm.org.uk
Sat Jun 11 21:29:24 CEST 2016


Hi

I import 16-bit TIFs (RGB) from a directory using the following code, then loop through adding each TIF before dividing by the total number of files to give an image of mean pixel values. Some quick questions:

-having checked pixel values, this does what I expect it to do in that it creates the mean for each RGB layer in each image. It is pretty quick, but is there a function that does this already (and would also do, for example, the median)?

-do all the calculations take place in 32 bit space?

-when I export the image either as 16 bits per pixel (or indeed 8) and then reimport the pixel values look like they might have been scaled. For example, below. Is writeTIFF doing something Im not aware of??
> m_image_tiff1[1,1,1]
[1] 0.9745098
> test_tiff[1,1,1]
[1] 0.06532387

Thanks very much!

mike



#Scan directory and store filenames in string, then count total files
files <- as.character(list.files(path="./mean/input/"))
n <- length(files)

#Use first TIF as loop file, then add all together
m_image_tiff <- readTIFF(paste("./mean/input/",files[1],sep=""))
for (i in 2:n){
  test<-paste("./mean/input/",files[i],sep="")
  tiff <- readTIFF(paste("./mean/input/",files[i],sep=""))
  m_image_tiff <- (tiff+m_image_tiff)
}

#Calculate mean and write TIF
m_image_tiff <- (m_image_tiff/n)
writeTIFF(m_image_tiff,"./mean/mean_tiff.tif",bits.per.sample=16L)
---
Mike Smith



More information about the R-help mailing list