How to load all files from a directory of two different types in MATLAB

前端 未结 1 1918
轮回少年
轮回少年 2020-12-18 15:21

I know that it is possible to load all files of type .gif by using:

files = dir(\'C:\\myfolder\\*.gif\');

However, my problem

相关标签:
1条回答
  • 2020-12-18 15:57

    You can simply search for both .gif and .jpg files then load and process the images one by one.

    Just invoke dir twice - one for each type of image and store the results in two separate structures. Next, concatenate all of the file names to one structure, then go ahead and do your processing for all of the images.

    Something like this:

    %// Specify the folder where your images are stored
    folder = fullfile('path', 'to', 'your', 'folder');
    
    %// Specify search pattern for JPEG and GIF files
    jpgFileFolder = fullfile(folder, '*.jpg');
    gifFileFolder = fullfile(folder, '*.gif');
    
    %// Invoke dir for both types of images
    d1 = dir(jpgFileFolder);
    d2 = dir(gifFileFolder);
    
    %// Concatenate both dir structures together into a single structure
    d = [d1; d2];
    
    %// For each image we have...
    for idx = 1 : numel(d)
        %// Get full path to file
        f = fullfile(folder, d(idx).name);
    
        %// Read in the image
        im = imread(f);
    
        %// Do something with this image
        %//...
        %//...
    end
    

    fullfile allows you to create a directory string that is OS independent. Simply take each subdirectory that is part of your string and place them as separate string arguments into fullfile and it should work fine.

    0 讨论(0)
提交回复
热议问题