Life is a game, take it seriously

MATLAB: Read all images in a folder; everything starts here

In Computer Vision, Matlab on April 13, 2011 at 3:26 pm

written by Gooly

In ordinary people’s life, it’s often the case that you need to deal with a large but not extremely huge image database with non sequential naming. Instead of reading each image when you need it, you probably would prefer to read it all into a matrix for one time, or even store it into a mat file so that you can access it much faster next time. The MATLAB function dir would be a good way to do this, see the example below.

function X = ReadImgs(Folder,ImgType)
    Imgs = dir([Folder '/' ImgType]);
    NumImgs = size(Imgs,1);
    image = double(imread([Folder '/' Imgs(1).name]));
    X = zeros([NumImgs size(image)]);
    for i=1:NumImgs,
      image = double(imread([Folder '/' Imgs(i).name]));
      if (size(image,3) == 1)
        X(i,:,:) = image;
        X(i,:,:,:) = image;

To read in all the jpeg files in image_folder simply do the following,

ImageData = ReadImgs('image_folder','*.jpg');

Notice that this function only deals with image of the same size, which is usually the case; to be able to deal with different size images, you can use imresize in the function or other methods depending on what the task you are actually working on.

When dealing with huge image database (2GB up), you would probably prefer to store the images into separate mat files, only open the one that you want to use, and close it afterwards.

Note that as Thiruvikraman Kandhadai mentioned in the comment, this code won’t run if you have both RGB and Gray scale images in the folder. Save them into two separate matrices if you really need to.


  1. I’m having trouble in that i can paste both into a script and attempt to run it, but it will not recognize ‘ReadImgs’ as a function. It could be my blatant lack of skill with matlab, I’m not sure how to fix it. If i define the function earlier in the script, it should call correctly?

  2. @Gooly, I managed to used your function to read all the images onto the memory. How do I display the images so obtained from your function?

  3. I kind of forgot why I store images in double format, but this should probably do the job: image(uint8(imageData(1,:,:))) // for image 1
    By the way I just notice that the code doesn’t work for color image, I’ll change it in a minute

  4. I got the following error message when I called your function for reading many image files from a folder:

    Subscripted assignment dimension mismatch.

    Error in ReadImgs (line 10)
    X(i,:,:) = image;

    Then I figured that the error was because there was a mix of RGB and grayscale images in the folder I had. The error did not occur when all the images were colour images. Looks like the dimension of X gets fixed by the first image which the function encounters and the dimension cannot be changed subsequently.

    Anyway thanks for this function. Was very useful

  5. hi guys
    can you help me
    i want compare single image with folder for matching or not matching

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: