Load all the images from a directory

17,734

Solution 1

You can easily load multiple images with same type as follows:

function Seq = loadImages(imgPath, imgType)
    %imgPath = 'path/to/images/folder/';
    %imgType = '*.png'; % change based on image type
    images  = dir([imgPath imgType]);
    N = length(images);

    % check images
    if( ~exist(imgPath, 'dir') || N<1 )
        display('Directory not found or no matching images found.');
    end

    % preallocate cell
    Seq{N,1} = []

    for idx = 1:N
        Seq{d} = imread([imgPath images(idx).name]);
    end
end

Solution 2

I believe you want the imread function, not load. See the documentation.

Solution 3

The full path (inc. directory) is not held in imgfiles.name, just the file name, so it can't find the file because you haven't told it where to look. If you don't want to change directories, use fullfile again when reading the file.

You're also using the wrong function for reading the images - try imread. Other notes: it's best not to use i for variables, and your loop is overwriting I2 at every step, so you will end up with only one image, not four.

Solution 4

You can use the imageSet object in the Computer Vision System Toolbox. It loads image file names from a given directory, and gives you the ability to read the images sequentially. It also gives you the option to recurse into subdirectories.

Share:
17,734
aushima
Author by

aushima

Updated on June 16, 2022

Comments

  • aushima
    aushima almost 2 years

    I have certain images in a directory and I want to load all those images to do some processing. I tried using the load function.

    imagefiles = dir('F:\SIFT_Yantao\demo-data\*.jpg');      
    nfiles = length(imagefiles);    % Number of files found
     for i=1:nfiles
     currentfilename=imagefiles(i).name;
     I2 = imread(currentfilename);
     [pathstr, name, ext] = fileparts(currentfilename);
     textfilename = [name '.mat'];
    fulltxtfilename = [pathstr textfilename];
    load(fulltxtfilename);
    descr2 = des2;
    frames2 = loc2;
    do_match(I1, descr1, frames1, I2, descr2, frames2) ;
    end
    

    I am getting an error as unable to read xyz.jpg no such file or directory found, where xyz is my first image in that directory.
    I also want to load all formats of images from the directory instead of just jpg...how can i do that?