I have just started to learn how to analyse immunohistochemitry images using R. I have a segmented image (segmentation performed by others) that identifies cells positive for one marker, and I need to read this into R for plotting and spatial analyses. I struggle with understanding how to read in this file using terra:rast(), or if other methods are more appropriate?
Link to the file: https://drive.google.com/file/d/1152QLPxF98GqI0IrbVy6VHTEa9-IpcXU/view?usp=sharing
When I read in this file I get a warning:
test <- rast("Test.tif")
Warning: [rast] unknown extent
Description of test:
test
class : SpatRaster
dimensions : 8648, 7170, 1 (nrow, ncol, nlyr)
resolution : 1, 1 (x, y)
extent : 0, 7170, 0, 8648 (xmin, xmax, ymin, ymax)
coord. ref. :
source : Test.tif
color table : 1
name : Test
QUESTIONS:
What should I do with that warning?
Why are the values changed so that white is 0 and black is 1 compared to other methods to read in the file?
When I plot the image (plot(test)), it is both turned and mirrored. Why? And how to fix this?
Original file:
Plotted in r:
plot(test, col = c("brown", "NA"))
If I read in using
test <- readTIFF("Test.tif", convert = TRUE)
and then convert to raster:
test <- rast(test)
Then the code for plotting with changed colors works. The description of test is somewhat different with min
and max
values specified:
test
class : SpatRaster
dimensions : 8648, 7170, 1 (nrow, ncol, nlyr)
resolution : 1, 1 (x, y)
extent : 0, 7170, 0, 8648 (xmin, xmax, ymin, ymax)
coord. ref. :
source(s) : memory
name : lyr.1
min value : 0
max value : 1
How to read in using terra:rast (have a lot of files so would like to omit storing the file information in memory) but be able to change the colors?
Some additional information:
terra::describe("Test.tif")
[1] "Driver: GTiff/GeoTIFF" "Files: "Test.tif"
[3] "Size is 7170, 8648" "Metadata:"
[5] " TIFFTAG_XRESOLUTION=72" " TIFFTAG_YRESOLUTION=72"
[7] " TIFFTAG_RESOLUTIONUNIT=2 (pixels/inch)" "Image Structure Metadata:"
[9] " COMPRESSION=CCITTRLE" " INTERLEAVE=BAND"
[11] " MINISWHITE=YES" "Corner Coordinates:"
[13] "Upper Left ( 0.0, 0.0)" "Lower Left ( 0.0, 8648.0)"
[15] "Upper Right ( 7170.0, 0.0)" "Lower Right ( 7170.0, 8648.0)"
[17] "Center ( 3585.0, 4324.0)" "Band 1 Block=7170x135 Type=Byte, ColorInterp=Palette"
[19] " Image Structure Metadata:" " NBITS=1"
[21] " Color Table (RGB with 2 entries)" " 0: 255,255,255,255"
[23] " 1: 0,0,0,255"
You can ignore the warning - it comes from read_gdal.cpp
, I believe. Terra package is supposed to work with georeferenced data, which have extent and CRS specified and coded within data itself.
r <- terra::rast("~/Downloads/Test.tif")
#> Warning: [rast] unknown extent
r
#> class : SpatRaster
#> dimensions : 8648, 7170, 1 (nrow, ncol, nlyr)
#> resolution : 1, 1 (x, y)
#> extent : 0, 7170, 0, 8648 (xmin, xmax, ymin, ymax)
#> coord. ref. :
#> source : Test.tif
#> color table : 1
#> name : Test
terra::plot(r)
The black and white colors are forced by color table in tiff:
terra::coltab(r)
#> [[1]]
#> value red green blue alpha
#> 1 0 255 255 255 255
#> 2 1 0 0 0 255
To plot it in different colors (without changing the values of raster), we have to define and change color table:
ctb <- data.frame(value = 0:1, col = c("brown", "black"))
terra::coltab(r) <- ctb
terra::plot(r)
If you wish to change the raster values, you might use something like:
s <- r
s[s == 1] <- NA
terra::plot(s, col = data.frame(0, "brown"))
Just for information the version of {terra} used:
packageVersion("terra")
#> [1] '1.8.3'
If your raster is flipped/rotated, please have a look on terra::flip()
and/or rev()
functions.
Created on 2024-12-08 with reprex v2.1.1.9000
PS. The "color map" in TIFF
is given in bits per pixel and color.space attributes of file:
tt <- tiff::readTIFF("~/Downloads/Test.tif", info = TRUE, convert = TRUE)
attributes(tt)$bits.per.sample
#> [1] 1
attributes(tt)$color.space
#> [1] "white is zero"