Load all the images from a directory

aushima picture aushima · Mar 27, 2013 · Viewed 17.5k times · Source

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?

Answer

Ehsan picture Ehsan · Mar 27, 2013

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