The _ImageJ_ Problem

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
library(magrittr)

Introduction

The ImageJ software (https://imagej.nih.gov/ij/) is a widely-used image viewing and processing software, particularly popular in microscopy and life sciences. It supports the TIFF image format (and many others). It reads TIFF files perfectly, however it can sometimes write them in a peculiar way, meaning that when other softwares try to read TIFF files written by ImageJ, mistakes can be made.

One goal of the ijtiff R package is to correctly import TIFF files that were saved from ImageJ.

Frames and Channels in TIFF files

The Peculiarity of ImageJ TIFF files

It is common to use TIFFTAG_SAMPLESPERPIXEL to record the number of channels in a TIFF image, however ImageJ sometimes leaves TIFFTAG_SAMPLESPERPIXEL with a value of 1 and instead encodes the number of channels in TIFFTAG_IMAGEDESCRIPTION which might look something like
"ImageJ=1.51 images=16 channels=2 slices=8".

A conventional TIFF reader would miss this channel information (because it is in an unusual place). ijtiff does not miss it. We'll see an example below.

Note: These peculiar ImageJ-written TIFF files are still bona fide TIFF files according to the TIFF specification. They just break with common conventions of encoding channel information.

Reading ImageJ TIFF files

path_2ch_ij <- system.file("img", "Rlogo-banana-red_green.tif",
  package = "ijtiff"
)

path_2ch_ij is the path to a TIFF file which was made in ImageJ from the R logo dancing banana GIF used in the README of Jeroen Ooms' magick package. The TIFF is a time-stack containing only the red and green channels of the first and third frames of the original GIF. Here's the full gif:

Here are the red and green channels of the first and third frames of the TIFF:

rgbanana_tif <- system.file("img", "Rlogo-banana-red_green.tif",
  package = "ijtiff"
) %>%
  ijtiff::read_tif()
d <- dim(rgbanana_tif)
reds <- purrr::map(seq_len(d[4]), ~ rgbanana_tif[, , 1, .]) %>%
  purrr::reduce(cbind)
greens <- purrr::map(seq_len(d[4]), ~ rgbanana_tif[, , 2, .]) %>%
  purrr::reduce(cbind)
to_display <- array(0, dim = c(2 * nrow(reds), ncol(reds), 3, 1))
to_display[seq_len(nrow(reds)), , 1, ] <- reds
to_display[seq_len(nrow(reds)) + nrow(reds), , 2, ] <- greens
ijtiff::display(to_display)

The original tiff package

When we import it with the original tiff package:

img <- tiff::readTIFF(path_2ch_ij, all = TRUE)
str(img) # 10 images
img[[1]][100:105, 50:55, 1] # print a section of the first image in the series

The ijtiff package

When we import the same image with the ijtiff package:

img <- ijtiff::read_tif(path_2ch_ij)
dim(img) # 2 channels, 2 frames
img[100:105, 50:55, 1, 1] # print a section of the first channel, first frame

Note

The original tiff package reads several types of TIFFs correctly, including many that are saved from ImageJ. This is just an example of a TIFF type that it doesn't perform so well with.

Advice for all ImageJ users

Base ImageJ (similar to the tiff R package) does not properly open some perfectly good TIFF files^[I think native ImageJ only likes 1, 3 and 4-channel images and complains about the rest, but I'm not sure about this.] (including some TIFF files written by the tiff and ijtiff R packages). Instead it often gives you the error message: imagej can only open 8 and 16 bit/channel images. These images in fact can be opened in ImageJ using the wonderful Bio-Formats plugin.



Try the ijtiff package in your browser

Any scripts or data that you put into this service are public.

ijtiff documentation built on Oct. 9, 2023, 1:07 a.m.