0

I tried this where the star * represents all images but did not work

path = system.file ("C:/Users/Bilal/Desktop/d","*.jpg", package = "OpenImageR")


head(path)

Error in readImage(path) : the path to an image is invalid or the image does not exist

im<- readImage(path)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
B84
  • 7
  • 3
  • The first argument of `system.file` should be a subdirectory of the `OpenImageR` package. Your path does not look like that is the case. Are you just wanting to direct to a directory of images that are at `"C:/Users/Bilal/Desktop/d`? – user20650 Aug 07 '22 at 10:12
  • if so; then to get a list of paths you can use `list.files("C:/Users/Bilal/Desktop/d", pattern=".jpg", full.names = TRUE)`. You can then read these in one at a time of with a loop, say using `lapply` – user20650 Aug 07 '22 at 10:15
  • I tried 'list.files' and it did not work. Here's the rest of my code – B84 Aug 07 '22 at 11:00
  • im<- readImage(path) – B84 Aug 07 '22 at 11:02
  • for (i in 1:10) { image(im[,,i], col=grayscale) } dim(im) – B84 Aug 07 '22 at 11:03
  • "*'list.files' and it did not work.*" : we need more info. If you do `pth = list.files("C:/Users/Bilal/Desktop/d", pattern=".jpg", full.names = TRUE)` and then inspect the `pth` variable what does it show? Are there paths to one or multiple jpg's or is it empty or? – user20650 Aug 07 '22 at 11:09
  • yes, it does show a list of images, but if I do 'dim(path)' , it shows 'null' not for example '470 600 12' – B84 Aug 07 '22 at 11:15
  • nframes<-OpenImageR::readImage("10.jpg"), this function works well for one image but I need it to work for all images. – B84 Aug 07 '22 at 11:17
  • So `pth` gives a vector of character strings with the path to multiple jpeg's. As 2nd comment above you can read in one at a time e.g. `img = readImage(pth[1])` or use a loop to read all in e.g. `lst_of_img = lapply(pth, readImage)` – user20650 Aug 07 '22 at 11:19
  • Thank you so much for you comments. I did loops but the problem is 'dim' does not work. I need this work as I need to do image variance – B84 Aug 07 '22 at 11:39
  • for (i in 1:10) { image(im[,,i], col=grayscale) } – B84 Aug 07 '22 at 11:41
  • If you do `dim(path)` this will be NULL as it is a vector. If `lst_of_img = lapply(...)` (from above comment) executed correctly, you can then loop through the list to get the `dim` e.g.`lst_of_dims <- lapply(lst_of_img, dim)` – user20650 Aug 07 '22 at 11:42
  • This is great, but image variance still is not working, I have no idea why: varimg <- apply(lst_of_dims,1:2,var) image(varimg, col=grayscale) – B84 Aug 07 '22 at 12:00
  • Error in image.default(varimg, col = grayscale) : 'z' must be a matrix – B84 Aug 07 '22 at 12:21
  • This has moved away from your original question which was a misunderstanding of `system.file`. I'd suggest asking a new question. For example, in the new question add the relevant data e.g. `dput(lst_of_dims[1:5])` and ask how to get the variance of this, explaining what variance you want e.g. across all dims / one dim at a time or ...? – user20650 Aug 07 '22 at 12:40
  • have a look a this : https://stackoverflow.com/questions/2953254/cgetting-all-image-files-in-folder – bigtheo Aug 09 '22 at 08:54

1 Answers1

0

try this :

var files = Directory.GetFiles("C:\\path", "*.*", SearchOption.AllDirectories);
    
    List<string> imageFiles = new List<string>();
    foreach (string filename in files)
    {
        if (Regex.IsMatch(filename, @"\.jpg$|\.png$|\.gif$"))
            imageFiles.Add(filename);
    }
bigtheo
  • 624
  • 9
  • 16