我有一个包含多个文件夹的目录。每个文件夹都包含多个图像。
如果我们假设我们有一个包含图像的目录,我知道如何阅读这些图像(参见下面的代码);如果我们假设这些图像放在这个目录的子目录中,那么我不知道如何编辑我的代码。
directory = 'my_direct';
dnames1={directory};
c{1} = dir(dnames1{1}); % struct array with 5 fileds (name, isdir,...)
if length(c{1}>0)
if c{1}(1).name =='.'
c{1} = c{1}(3:end); %ignore the '.' and '..'
end
end
for k = 1: length(c{1})
image= double(imread([dnames{1} '/' h{1}(L).name]))./255;
end
答案 0 :(得分:2)
假设您想进入每个子目录并阅读图像
Root_directory='The directory location';
sub_directories=dir(Root_directory);
sub_directories(1,2)=[]; % to remove . and ..
for sub_dir_index=1:length(sub_directories)
images=dir(fullfile(Root_directory,sub_directories(sub_dir_index).name));
the rest of your code
end
答案 1 :(得分:1)
您可以使用此代码读取多个图像,您应该知道的是该目录的路径。只需编写路径,代码就会根据您存储的图案读取图像。我希望你会发现它很有用。
myFolder = 'path';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(myFolder, '*.jpg');
jpegFiles = dir(filePattern);
for i = 1:length(jpegFiles)
baseFileName = jpegFiles(i).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
Input_image = imread(fullFileName);
end
答案 2 :(得分:1)
如果你有使用计算机视觉系统工具箱的R1614b MATLAB版本,你可以使用imageSet对象:
imgSets = imageSet('my_direct','recursive')
imgSets
将是imageSet
个对象的数组,每个对象都包含my_direct
子目录中所有图像文件的路径。然后,您可以从第i个子目录中读取第j个图像,如下所示:
im = read(imgSets(i), j);