matlab trick

5 次查看(过去 30 天)
LIU WEIHUA
LIU WEIHUA 2012-4-27
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!

回答(2 个)

Image Analyst
Image Analyst 2012-4-27
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 2012-4-27
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{:});

类别

Help CenterFile Exchange 中查找有关 Convert Image Type 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by