Number and Brightness Image Analysis

Rory Nolan

2018-04-27

Loading the libraries

Reading and Displaying Images

Now let’s read in an image:

img <- ijtiff::read_tif(system.file("extdata", "50_big.tif", package = "nandb"))
#> Reading 50_big.tif: a 100x100 pixel image of unsigned integer type with 1 channel and 50 frames . . .
#>  Done.
dim(img)  # get img's dimensions
#> [1] 100 100   1  50

So we can see that img is a 3-dimensional array of \(1\) slices each of which is a \(100 \times 100\) image. We can view the mean projection of the stack:

ijtiff::display(detrendr::mean_pillars(img)[, , 1, 1])  # first channel, first frame

Preprocessing, Brightness Calculation and Post-Filtering

Now we can calculate the brightness image based on this image series, with an exponential filtering detrend with a time constant of (say) 10 frames and using the Huang thresholding method via

img_brightness <- brightness(img, "e", tau = "auto", thresh = "Huang")
ijtiff::display(img_brightness[, , 1, 1])

and we can see what happens when we median filter.

img_brightness_med <- brightness(img, "e", tau = "auto", 
                                 thresh = "Huang", filt = "median")
ijtiff::display(img_brightness_med[, , 1, 1])

Saving Images

We can save the brightness image (let’s do it without median filtering this time).

ijtiff::write_tif(img_brightness, "BrightnessImage")
#> Writing BrightnessImage.tif: a 100x100 pixel image of floating point type with 1 channel and 1 frame . . .
#>  Done.
list.files(pattern = "\\.txt$")  # check that the .txt file was written to the folder
#> character(0)

Studying the Distribution of Brightnesses

We can take a look at the distribution of brightnesses in that image:

db <- density(img_brightness, na.rm = TRUE) %>% 
  {data.frame(x = .$x, y = .$y)}
ggplot(db, aes(x, y)) + geom_line() + 
  labs(x = "brightness", y = "frequency") +
  xlim(-2, 2)
#> Warning: Removed 206 rows containing missing values (geom_path).

and we can compare it to the distribution of brightnesses from immobile particles:

immobile_brightnesses <- matrix(rpois(50 * 10^6, 50), nrow = 10^6) %>% 
  {apply(., 1, var) / rowMeans(.) - 1}
di <- density(immobile_brightnesses) %>% {data.frame(x = .$x, y = .$y)}
rbind(mutate(db, mobility = "mobile"), mutate(di, mobility = "immobile")) %>%      mutate(mobility = factor(mobility)) %>% 
  ggplot(aes(x, y)) + geom_line(aes(colour = mobility)) + 
  labs(x = "brightness", y = "frequency") +
  xlim(-2, 2)
#> Warning: Removed 206 rows containing missing values (geom_path).