Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 204742

Filter images out of large set, and move only those fulfilling criteria to new folder

$
0
0

I have a dataset of 4000+ images. For the purpose of figuring out the code, I moved a small batch of them to another folder.

The files look like this:

    folder

    r01c01f01p01-ch3.tiff
    r01c01f01p01-ch4.tiff
    r01c01f02p01-ch1.tiff
    r01c01f03p01-ch2.tiff
    r01c01f03p01-ch3.tiff
    r01c01f04p01-ch2.tiff
    r01c01f04p01-ch4.tiff
    r01c01f05p01-ch1.tiff 
    r01c01f05p01-ch2.tiff
    r01c01f06p01-ch2.tiff
    r01c01f06p01-ch4.tiff
    r01c01f09p01-ch3.tiff
    r01c01f09p01-ch4.tiff
    r01c01f10p01-ch1.tiff
    r01c01f10p01-ch4.tiff
    r01c01f11p01-ch1.tiff
    r01c01f11p01-ch2.tiff
    r01c01f11p01-ch3.tiff
    r01c01f11p01-ch4.tiff
    r01c02f10p01-ch1.tiff
    r01c02f10p01-ch2.tiff
    r01c02f10p01-ch3.tiff
    r01c02f10p01-ch4.tiff

I cannot remove the name prior to the -ch# as that information is important. What I want to do is to filter the folder of images, and move/keep only sets (ie: r01c02f10p01) which have all four ch values (ch1-4). It would be helpful to move these complete sets to a new folder, so I am then able to use a different script to merge the sets of four into a singular composite for each.

This script to merge the images into a composite relies on the fact that we have a complete ch1, ch2, ch3, and ch4 suffix on each image, and that they are in batches of four. (This code is used in an image processing software called Fiji, rather than R.) Therefore, it is important that I am able to locate and move only the image sets which are complete out of the massive set of 4000 images.

Previously, the data set included a fifth channel (ch5). I was able to remove the all images with ch5 from the original folder like this.

    ##Create folder variable which has all image files 
     setwd("/Desktop/Nov 5/")
          folder = list.files("/Desktop/Nov5/")
    folder <- list.files(getwd())

    ##Create final2 variable which has all image files ending in ch5
    final2 <- dir(path="/Desktop/Nov5/", pattern="ch5") 

    ##Remove final2 from folder
    file.remove(folder,final2) 

In trying to address the issue of filtering only complete sets, I'd thought that doing something like this:

    ch1 <- dir(path="/Desktop/cp/complete//", pattern="ch1")

    ch2 <- dir(path="/Desktop/cp/complete//", pattern="ch2")

    ch3 <- dir(path="/Desktop/cp/complete//", pattern="ch3")

    ch4 <- dir(path="/Desktop/cp/complete//", pattern="ch4")

And applying the file.remove function, similar to below, might work.

    final2 <- dir(path="/Desktop/cp1/Images//", pattern="ch5") 
    file.remove(folder,final2) 

However, creating new variables for each ch value fragments out each file. I am unsure how to use these to actually distinguish whether an individual image has all four ch values to meaningfully filter my images in the original (or a new folder). The other sources I've seen have issues that don't quite match this problem.

Another thread had a suggestion of converting files into a data.frame, but that doesn't actually allow me to manipulate the image files in the folder. I need to move/filter the files, not just display a list of the correct ones unfortunately. That was as follows:

    library(dplyr)

    ch_set <- 1:4

    files_to_keep <- data.frame(filename = files, stringsAsFactors = FALSE) %>%
      tidyr::extract(filename, into = c("group", "ch"), regex = "                (^[\\w\\d]+)\\-ch(\\d)", remove = FALSE) %>%
      mutate(ch = as.numeric(ch)) %>%
      group_by(group) %>% 
      filter(all(ch_set %in% ch))

    files_to_keep

To summarize: I expect to filter files from a random assortment without complete ch values (ie: maybe only ch1 and ch2, or ch3 and ch4), to an assortment of files which only contains those with complete ch1, ch2, ch3, and ch4 images.


Viewing all articles
Browse latest Browse all 204742

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>