我需要加载1000张图像作为面部识别程序的训练数据。
有100个人,每个人有10张独特的照片。
保存在如下文件夹中:
myTraining //main folder
- John // sub folder
- John_Smith_001, John_Smith_002, ... , 00n, //images
- Mary // sub folder
- Mary_Someone_001... you get the idea :)
我熟悉很多matlab,但不熟悉迭代外部文件的方法。
一个接一个地浏览每个文件夹并加载图像的简单实现是什么,理想情况下使用检索文件名并将它们用作变量/图像名称。
提前致谢。
答案 0 :(得分:2)
使用以下命令将递归列出特定目录及其子目录中的所有文件。我已经为Windows和Mac / Linux列出了它。遗憾的是我无法测试Mac / Linux版本,因为我不在任何这些机器附近,但它将与下面的内容非常相似。
<强>窗强>
[~,result] = system('dir C:\Users\username\Desktop /a-d /s /b');
files = regexp(result,'\n','Split')
<强>的Mac / Linux的强>
[~,result] = system('find /some/Directory -type file);
files = regexp(result,'\n','Split')
然后,您可以遍历创建的单元格数组files
,并使用imread
执行您需要的任何加载,或类似的内容
答案 1 :(得分:1)
对于jpg图像,它将是
files = dir('*.jpg');
for file = files'
img = imread(file.name);
% Do some stuff
end
如果您有多个扩展名,请使用
files = [dir('*.jpg'); dir('*.gif')]
我希望这会有所帮助
答案 2 :(得分:1)
你可以这样做:
basePath = pwd; %your base path which is in your case myTraining
allPaths = dir(basePath); %get all directory content
subFolders = [allPaths(:).isdir]; %get only indices of folders
foldersNames = {allPaths(subFolders).name}'; % filter folders names
foldersNames(ismember(foldersNames,{'.','..'})) = []; %delete default paths for parents return '.','..'
for i=1:length(foldersNames), %loop through all folders
tmp = foldersNames{i}; %get folder by index
p = strcat([basePath '\']);
currentPath =strcat([p tmp]); % add base to current folder
cd(currentPath); % change directory to new path
files = dir('*.jpg'); % list all images in your path which in your case could be John or Mary
for j=1:length(files), % loop through your images
img = imread(files(j).name); % read each image and do what you want
end
end