detrendr
Detrend image series.
First let’s load the library:
library(detrendr)
The package contains convenient functions for reading and writing TIFF files. It also contains a sample image series which can be found at system.file("extdata", "cells.tif", package = "detrendr")
. Let’s read it in and display the first and last frames:
path <- system.file("extdata", "bleached.tif", package = "detrendr")
img <- read_tif(path)
dim(img) # img has 500 frames
#> [1] 60 60 500
display(img[, , 1], # first frame
breaks = 0:500, col = grDevices::grey(seq(0, 1, length.out = 500)))
display(img[, , 500], # last frame
breaks = 0:500, col = grDevices::grey(seq(0, 1, length.out = 500)))
We see that the intensity is much lower for the last frame, this is because the image series has been bleached. This can be further seen with a plot of the frame means:
plot(apply(img, 3, mean), ylim = c(60, 150))
We can correct for this (and check how long it takes):
system.time(corrected_exp <- img_detrend_exp(img, "auto",
seed = 0, parallel = 2))["elapsed"]
#> elapsed
#> 7.537
display(corrected_exp[, , 1], # first frame
breaks = 0:500, col = grDevices::grey(seq(0, 1, length.out = 500)))
display(corrected_exp[, , 500], # last frame
breaks = 0:500, col = grDevices::grey(seq(0, 1, length.out = 500)))
So we see that the corrected series does not have this drop-off in intensity.
plot(apply(corrected_exp, 3, mean), ylim = c(60, 150))
Above we used exponential filtering detrending, but we could also use boxcar or polynomial.
system.time(corrected_boxcar <- img_detrend_boxcar(img, "auto",
seed = 0, parallel = 2))["elapsed"]
#> elapsed
#> 6.001
system.time(corrected_polynom <- img_detrend_polynom(img, "auto",
seed = 0, parallel = 2))["elapsed"]
#> Warning in best_degree(img, seed = seed, parallel = parallel): The polynomial degree found for your detrend was 7. Degrees above 3 are not recommended as they usually indicate eccentric fits. It would
#> be wise to use another detrending method (exponential or boxcar).
#> elapsed
#> 6.391
Let’s check the mean brightness of each:
mean(brightness_pillars(corrected_exp))
#> [1] 2.384401
mean(brightness_pillars(corrected_boxcar))
#> [1] 1.806117
mean(brightness_pillars(corrected_polynom))
#> [1] NaN
So we see that different methods give different results.