0
votes

I am looking to read a set of images from a folder and then store it in an array such that if I ask imshow(imageArray(5)) it displays the 5th image in the array. Using some code I found from similar questions, I have this so far:

% Specify the folder where the files live.
myFolder = 'C:\Users\MyName\Documents\MATLAB\FolderName';

% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);

imageArray = zeros(480, 1);

for k = 1 : length(theFiles)
  baseFileName = theFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  % Now do whatever you want with this file name,
  % such as reading it in as an image array with imread()
  imageArray(k) = imread(fullFileName);
end

However when I do this I get the following error:

Unable to perform assignment because the left and right sides have a different number of elements.

Error in ImportTry (line 16) imageArray(k) = imread(fullFileName);

How can I accomplish using a single index to index higher-dimensional arrays?

3

3 Answers

2
votes

As opposed to the other solutions here, I would not go to a cell, because cells are notoriously slower than matrices. I'd just go for a 4D matrix, where the first three are your image as N rows, M columns, 3 RGB channels, and then an index for your image:

imageArray = zeros(N,M,3,480); % Where N and M are your image size
for k = 1 : length(theFiles)
  baseFileName = theFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  % Now do whatever you want with this file name,
  % such as reading it in as an image array with imread()
  imageArray(:,:,:,k) = imread(fullFileName);
end

Note that this only works if all your images are exactly the same shape. If not, cells or structures are the way to go.

2
votes

The problem with your code is that in the last line of the loop you assign an image, which has dimensions like 1080x1920x3 uint8 (for full HD image) to a vector of size 480x1. This does naturally not work. So try the following and use a cell array.

% Specify the folder where the files live.
myFolder = 'C:\Users\MyName\Documents\MATLAB\FolderName';

% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.tif'); % Change to whatever pattern you need.
theFiles = dir(filePattern);

imageArray = cell(size(theFiles)); % initialize cell array for speedup

for k = 1 : length(theFiles)
  baseFileName = theFiles(k).name;
  fullFileName = fullfile(myFolder, baseFileName);
  fprintf(1, 'Now reading %s\n', fullFileName);
  % Now do whatever you want with this file name,
  % such as reading it in as an image array with imread()
  imageArray(k) = {imread(fullFileName)};
end

% iterate cell array and display all images 1-by-1
for k = 1 : length(imageArray)
    imshow(cell2mat(imageArray(k)))
    pause; % press a button to continue
end
1
votes

The error comes from the fact that you initialized your array with zeros, therefore the size of the image does not correspond to the size of the elements of the array.

What you want is to use cells.

So you would initialize your cell :

imageCell = cell(480, 1);

and then assign the images to the elements of the cell array :

imageCell{k} = imread(fullFileName);

Edit : As pointed out by Adriaan in his answer, if the sizes of your images are consistent over your folder then you'd better use a 4D matrix.