inalei
inalei

Reputation: 81

How to extract color features of an image in R

I'd like to do some image feature extraction in R. At the moment I am working with the EBImage package. I can get a RGB-histogramm with the hist-function, but can I also get a Lab-Histogramm? Further I also would like to store the data of the histogramms (the distribution of the pixel intensity). How is this possible? And is it possible to extract a color-correlogram of an image in R? If yes, how can I do this? Thanks a lot! Here is the code I have done so far (with comments):

source("http://bioconductor.org/biocLite.R") 
biocLite() 
biocLite("EBImage") 
library(EBImage) 
image1 <- readImage('C:/Users/Inalei/Pictures/Image1.JPG')
image2 <- readImage('C:/Users/Inalei/Pictures/Image2.JPG') 
image3 <- readImage('C:/Users/Inalei/Pictures/Image3.JPG')

hist1 <- hist(image1) #hist1 is empty
hist2 <- hist(image2) #hist2 is empty
hist3 <- hist(image3) #hist3 is empty

library(corrgram)
corrgram(image1) #Error in slot(x, ".Data")[, sapply(x, is.numeric), drop = FALSE] : incorrect number of dimensions

Upvotes: 3

Views: 1885

Answers (1)

aoles
aoles

Reputation: 1545

I've just updated EBImage development version to facilitate the extraction of the image histogram data. Now the hist method invisibly returns a named list of objects of class "histogram" corresponding to the red, green, and blue channels. For example, to access the distribution of green pixel intensities you can use

hist1 <- hist(image1)
hist1$green

To use this feature, either install EBImage from sources

library(devtools)
install_github("Bioconductor-mirror/EBImage")

or wait until the binaries of EBImage version 4.13.5 are available from the package landing page at https://www.bioconductor.org/packages/3.3/bioc/html/EBImage.html.

Re your question about the Lab color space: unfortunately, currently we don't support it.

To make a correlogram across the color channels, first construct a data frame containing columns of pixel values of each channel

channels = sapply(c("red", "green", "blue"),
  function(ch) as.vector(channel(x, ch)),
  simplify = FALSE)
channels = as.data.frame(channels)

and then pass is to the corrgram function

corrgram(channels)

Upvotes: 2

Related Questions