How to use cat() function inside loop for stacking of 2D images ?

Dear experts,
I have 48 no. of '.png' images in a folder.Name of each image is 0001.png....0048.png. I used the following code to import all the images.
% Specify the folder where the files live. myFolder = 'C:\Users\yourUserName\Documents\My Pictures'; % Check to make sure that folder actually exists. Warn user if it doesn't. if ~isdir(myFolder) errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder); uiwait(warndlg(errorMessage)); return; end % Get a list of all files in the folder with the desired file name pattern. filePattern = fullfile(myFolder, '*.png'); % Change to whatever pattern you need. theFiles = dir(filePattern); 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 = imread(fullFileName); imshow(imageArray); % Display image. drawnow; % Force display to update immediately. end
Now please guide me, how to use cat() to stack all the 48 images inside the loop. What will be exact command I should use. All images are grayscale image.
Thanking you in advance and Happy new year 2017.

Answers (1)

Assuming the files are grayscale, not color, make a 3D array inside the loop just after you read in the image with imread():
if k == 1
[rows, columns, numberOfColorChannels] = size(imageArray);
allImages = zeros(rows, columns, length(theFiles)); % Preallocate space
end
% Insert image into the proper plane
allImages(:, :, k) = imageArray;

Asked:

on 2 Jan 2017

Edited:

on 2 Jan 2017

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!