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.

2 Answers 2

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")
Sign up to request clarification or add additional context in comments.

Comments

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))

5 Comments

I was also thinking of detaching the package before running histogram: detach("package:EBImage", unload=TRUE).
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?
Thanks for the answers guys!
@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 arrays which might be used to store image data.
I wasn't aware of the accessor; thanks, updated my answer!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.