matlab trick

7 ビュー (過去 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 Center および File ExchangeConvert Image Type についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by