2

How to get the values of histogram with image as parameter. According to this, the values can be retrieved because hist() returns a value.

The rest of the code works fine. However, null is returned to histogram (full code):

library(EBImage)
image = readImage("cat.png")
grayImage <- channel(image,"gray")
grayScaled = floor(grayImage * 255)
histogram <- hist(grayScaled)

> histogram
NULL

Package EBImage is used here. So I want to get the intensities or counts like histogram$counts but the variable histogram is null.

Community
  • 1
  • 1
Obito
  • 77
  • 11

2 Answers2

1

Starting from EBImage version 4.13.5, the method hist() for Image objects returns a (list of) histogram-class object(s). In case of images of colormode Grayscale the result is a single object of class histogram, and for Color images the result is a named list with elements corresponding to the red, green, and blue channels, as illustrated by the following example.

library(EBImage)

file  = system.file("images", "sample-color.png", package="EBImage")
image = readImage(file)

h = hist(image)

str(h)

This feature is currently available in the devel branch of the package. It can be obtained from GitHub:

devtools::install_github("aoles/EBImage")
aoles
  • 1,427
  • 9
  • 16
0

EBImage has registered a method for the generic hist() as can be seen here:

library("EBImage")
findMethodSignatures(hist)
     x          
[1,] "AffyBatch"
[2,] "ANY"      
[3,] "Image"  

that is the reason why you can't return an histogram-class object when you call hist(grayScaled). It depends a bit on what you want, but you can access the .Data slot of your image object (which contains a matrix) and plot that, returning the desired object:

histogram <- hist(imageData(grayScaled))
David Heckmann
  • 2,679
  • 2
  • 17
  • 27
  • 2
    I was also thinking of detaching the package before running histogram: `detach("package:EBImage", unload=TRUE)`. – Konrad Mar 06 '16 at 15:32
  • Nice idea, in my example it yields the same result. Do you know how hist identifies the data slot without having the class-definiton of `image` registered? – David Heckmann Mar 06 '16 at 15:36
  • Thanks for the answers guys! – Obito Mar 06 '16 at 15:58
  • @DavidH Rather than reading the `.Data` slot directly, the `imageData` accessor should be used to get/set the pixel array of an `Image` object. The advantage of such an approach is that the `imageData` function works on both formal S4 `Image` objects, as well as on plain `array`s which might be used to store image data. – aoles Mar 07 '16 at 08:13
  • I wasn't aware of the accessor; thanks, updated my answer! – David Heckmann Mar 07 '16 at 08:50