detrendr
Detrend image series.
First let’s load the library:
library(detrendr)
The package 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 <- ijtiff::read_tif(path)
#> Reading a 60x60 pixel image of unsigned integer type with 1 channel and 500 frames.
dim(img) # img has 500 frames
#> [1] 60 60 1 500
ijtiff::display(img[, , 1, 1], # first channel, first frame
breaks = 0:500, col = grDevices::grey(seq(0, 1, length.out = 500)))
ijtiff::display(img[, , 1, 500], # first channel, 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.743
ijtiff::display(corrected_exp[, , 1, 1], # first channel, first frame
breaks = 0:500, col = grDevices::grey(seq(0, 1, length.out = 500)))
ijtiff::display(corrected_exp[, , 1, 500], # first channel, 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
#> 2.736
system.time(corrected_polynom <- img_detrend_polynom(img, "auto",
seed = 0, parallel = 2))["elapsed"]
#> Warning in best_degree(img[, , i, ], 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
#> 5.499
Let’s check the mean brightness of each:
mean(brightness_pillars(corrected_exp))
#> [1] 2.091796
mean(brightness_pillars(corrected_boxcar))
#> [1] 1.854516
mean(brightness_pillars(corrected_polynom))
#> [1] 2.119133
So we see that different methods give different results.