matlab trick

2 views (last 30 days)
LIU WEIHUA
LIU WEIHUA on 27 Apr 2012
I have a folder which contain thousands of images, how can I load these image into a matrix with the size of (m,n,k), where m and n is the size of the image, k is the kth image. and I do not care the order of the image.
Thanks!

Answers (2)

Image Analyst
Image Analyst on 27 Apr 2012
Preallocate a 3D image with the known number of files that you will be reading in. Then, inside the loop, insert the 2D image into the volumetric image as a slice (plane):
image3D(:,:,sliceNumber) = image2D;

Kye Taylor
Kye Taylor on 27 Apr 2012
I'm assuming the images all have the same extension and that the file format is supported by imread. Also, I'm assuming that the current folder where your images are located is C:\myDir. You can change this in the code below.
folderOfInterest = 'C:\myDir'; % this is the path to your folder
cd(folderOfInterest); % make current folder your folder
ext = '.png'; % here's the extension of the images
files = dir; % get a listing of the current directory
% counts how many images are in current folder
isImageOfInterest = ~cellfun(@isempty,...
regexp(cellstr(char(files.name)),'.png','ignorecase'));
N = nnz(isImageOfInterest) ;
Images = cell(N,1);
cnt = 0;
for i = 1:length(files)
if isImageOfInterest(i)
cnt = cnt+1;
Images{cnt} = imread(files(i).name);
end
end
If all the images are the same size, you can create your three-dimensional matrix with the command
bigArray = cat(3,Images{:});

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!