Generating multiple images histogram plot

Hi,

I am quite new with R and intend to generate a single plot of image histogram from multiple images.
Found that this EBImage package will generate quite simple method to generate the histogram.
I used these simple lines:

library(EBImage)
files <- list.files(path=path, pattern="*.tiff")
histogram <- hist(readImage(files)

However, since my images are too big (2k x 2k px).
Then, I got an idea to put it in a loop. So, I made this:

for(i in 1:64) {
#reading image
read <- readImage(files[i])
#making histogram
histogram <- hist(read)
}

But, this loop only gather each of the histogram. I am not sure how to make it simpler and faster.
I was thinking to grab only counts and intensity number from each of the image and pool them in a multidimensional array. I just don't have a clue to do it. Maybe anyone in this forum could help me regarding this issue?

Thanks a lot!

Best
IS

 # BiocManager::install("EBImage")

library("EBImage")
library(purrr)


# vector of filepaths of images to process
f = system.file("images", c("sample.png",
                            "shapes.png"), package="EBImage")


#show individual histograms
purrr::walk(f,
            ~hist(readImage(.)))


# collate to single histogram

list_image_data <- purrr::map(f,
           ~as.numeric(imageData(readImage(.))))


single_data <- reduce(list_image_data,c)
hist(single_data)

Hi @nigrahamuk,

Thanks for the reply. I got the same error if I do compile them with the same first few lines I have here

library(EBImage)
files <- list.files(path=path, pattern="*.tiff")
histogram <- hist(readImage(files)

It says, "Error: cannot allocate vector of size 1.8 Gb"
Have you experienced this as well?

Best
IS

to summarise the intensities and then use that for a histogram, I would use the weights package for wtd.hist


library("EBImage")
library(tidyverse)
library(weights)

# vector of filepaths of images to process
f = system.file("images", c("sample.png",
                            "shapes.png"), package="EBImage")


# collate to single histogram

list_image_data <- purrr::map_dfr(f,
                              ~enframe(name=NULL,
                                       as.numeric(imageData(readImage(.)))
                                       ) %>%
                                group_by(value) %>%
                                summarise(w=n()))


wtd.hist(list_image_data$value,
         weight = list_image_data$w)

Thanks! This one works.
Just need to add:

library(dplyr)
library(tibble)
library(weights)

Otherwise, this run smooth! Thanks a lot, once again!

Cheers
IS

sure, but also library(tidyverse) includes this and other commonly used things, so I find it convenient. You can do as you please, glad it worked for you.

Yes, you are right. I think I still try to figure out the library in R.
Seems we have to put the environment as well when setting up new directory (new to me).

Btw, have you experience adding labels such as a straight line with different colour or colouring some part of the histogram as well? or, print the histogram data as csv?

I tried to add:

write.csv(histogram,'histogram.csv')

but didn't work, seems I have to convert it into a list first.

Best
Sanka

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.