Search
Question: Not able to display actual grayscale image using EBImage
1
2.5 years ago by
Kpurva10
Germany
Kpurva10 wrote:

I am using the EBImage package for image processing in R. I have a 260 by 134 Matrix which I converted to an image using

    > image1 <- as.Image(matrix1)

And, here is the image object summary

    > image1
colorMode    : Grayscale
storage.mode : double
dim          : 260 134
frames.total : 1
frames.render: 1

imageData(object)[1:5,1:6]
[,1] [,2] [,3] [,4] [,5] [,6]
[1,]    0    0    0    0    0    0
[2,]    0    0    0    0    0    0
[3,]    0    0    0    0    0    0
[4,]    0    0    0    0    0    0
[5,]    0    0    0    0    0    0

A value greater than zero for a specific cell in the image object looks like this:

   > imageData(image1)[9,2]
[1] 3686.308

I then use the display function in the EBImage package to view the image which is build from the matrix data.

    > display(image1, method = "raster")

However, I get a binary image i.e just black and white pixels. I have shown it below. My data does not just consist of 0 and 1. The background values are zero but the regions with the actual image pattern have values higher than 1. How do I display the image using grayscale and using the functions in this package? Did someone face a similar issue? I also could not find a parameter to specify a gradient level.

modified 2.5 years ago by Andrzej Oleś720 • written 2.5 years ago by Kpurva10
2
2.5 years ago by
Andrzej Oleś720
Heidelberg, Germany
Andrzej Oleś720 wrote:

The reason why the image renders only as black and white is because the display function expects values in the range [0, 1]. Values higher than 1 are clipped and effectively display as 1 (white).

To properly visualize your data you first need to scale the pixel intensity values to the [0, 1] range. This can be done with the help of the normalize function:

image1 <- normalize(image1)
display(image1, method = "raster")