traumenCapote
traumenCapote

Reputation: 19

How do I read in a 16bit TIFF into R and preserve pixel intensities, so that I can use it as a labelled object mask using the EBImage package?

I have a 16-bit grayscale image object mask where each object is a single grayscale value from 1 to 5750 (the number of objects). When I read it with the EBImage package, pixel values drop to below 1:

objectMask <- readImage("/Volumes/myHD/objectMask.tiff")
range([email protected])
[1] 0.00000000 0.08792248

I intend to treat this image as if it were a labelled image similar to the output of the bwlabel {EBImage} function. objectMask is storage.mode:double while bwlabel(objectMask) is storage.mode:integer. I don't know if this is a problem, but if I manually change it to integer

storage.mode(objectMask) <- "integer"

all of the pixel values become 0, so clearly the problem is that it is being rescaled somehow when I read it in. Can someone help me with this?

Upvotes: 2

Views: 266

Answers (1)

Rafa
Rafa

Reputation: 169

From the documentation of EBImage, "readImage loads all images from the files vector and returns them stacked into a single Image object containing an array of doubles ranging from 0 (black) to 1 (white)."

The reason all of the values are becoming 0 when you change the storage mode to integer is because they are all being rounded up to the closest integer.

Therefore, you first need to undo the rescaling introduced by the readImage function, and then change the storage type to integer:

objectMask <- readImage("/Volumes/myHD/objectMask.tiff")
[email protected] <- [email protected] * 5750
storage.mode(objectMask) <- "integer"

Edit with alternative solution

An alternative solution, covering the problem caused by the fact that the conversion to integers merges some of the objects.

First, we create a factor with each level representing one of the objects:

factorData <- factor([email protected], levels=c(0, unique(as.vector([email protected][[email protected]>0]))))

Note that the levels are defined with 0 as the first level, so that the pixels with value of 0 will be assigned level 1.

We then reassign the values of [email protected]:

[email protected]=matrix(as.integer(factorData)-1, nrow=nrow([email protected]))

Note the following:

  • We subtract 1 from the result of converting the factors to integers, so that background pixels will get again a value of 0
  • The dimensions of the image are preserved by setting nrow=nrow([email protected])

Upvotes: 1

Related Questions