How to input image in array?

4 ビュー (過去 30 日間)
Tawsif Mostafiz
Tawsif Mostafiz 2021 年 6 月 29 日
編集済み: Stephen23 2021 年 6 月 30 日
Hi, I am using this part of code to input multiple images.
myFolder = 'Folderlocation';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir(); % Ask for a new one.
if myFolder == 0
% User clicked Cancel
return;
end
end
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.bmp'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, 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);
Now, I want to store these images in an array, something like image(k). But any effort to it shows error. How do I do it?
  1 件のコメント
Tawsif Mostafiz
Tawsif Mostafiz 2021 年 6 月 30 日
Thanks @Stephen Cobeldick. It helps!

サインインしてコメントする。

採用された回答

Stephen23
Stephen23 2021 年 6 月 29 日
編集済み: Stephen23 2021 年 6 月 30 日
The simplest solution is to just store the image data in the same structure that DIR returns:
S = dir(fullfile(myFolder,'*.bmp'));
for k = 1:numel(S)
F = fullfile(S(k).folder,S(k).name);
fprintf(1, 'Now reading %s\n', F); % optional
S(k).data = imread(F);
end
For example, the second file:
S(2).name % filename
S(2).data % image data

その他の回答 (1 件)

Chunru
Chunru 2021 年 6 月 29 日
You can use cell array so that each cell stores an image (of different size).
im{k} = imageArray;

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by