How to filter the images and get a list of names passing the criteria in list.filter() in R

I am trying to filter out the images where image_variance > 0.008 and get the the names of these images in a list. The code I have only gives "NA" values for all the images

library(imager)
library(tidyverse) 
library(rlist)

Folder  <- "" #write the path to folder
images  <- list.files(path = Folder, pattern = "*.JPG", full.names = TRUE)

image_variance <- images %>% map(function(x)
  
{
  im <-  load.image(x)
  im %>%
    resize(round(width(.)/10), round(height(.)/10)) %>%
    imsub(y<187) %>%
    as.data.frame(.) %>%
    mutate(channel=factor(cc,labels=c('R','G','B'))) %>%
    select(x,y, channel, value) %>%
    spread(channel, value) %>%
    mutate(new = R*0.2126+G*0.7152+B*0.0722) %>%
    summarise(var = var(new))
  
  
})

hist(var)

snow_coverimgs <- list.filter(images, image_variance > 0.008)
snow_coverimgs  

I don't think rlist gives you any benefits here.
I simplified your map function to return a simple vector of the variances, along with the simple vector of your image names, thats all you need to make a trivial dataframe/tibble to access in the normal ways.


image_variance <- images %>% map_dbl(function(x)
{
  im <-  load.image(x)
  im %>%
    resize(round(width(.)/10), round(height(.)/10)) %>%
    imsub(y<187) %>%
    as.data.frame(.) %>%
    mutate(channel=factor(cc,labels=c('R','G','B'))) %>%
    select(x,y, channel, value) %>%
    spread(channel, value) %>%
    mutate(new = R*0.2126+G*0.7152+B*0.0722) %>%
    summarise(var = var(new)) %>% pull(var)
})

hist(image_variance)

(images_info <- tibble(images=images,
                      image_variance=image_variance))

(snow_coverimgs <- filter(images_info, image_variance > 0.008))

Thank you so much. :slight_smile:
It works exactly how I wanted.

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.