从子文件夹读取图像并保存到另一个文件夹中
问题描述:
我在这里有一个代码。从子文件夹读取图像并保存到另一个文件夹中
start_path = fullfile(matlabroot, '\toolbox\images\imdemos');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
while true
[singleSubFolder, remain] = strtok(remain, ';');
if isempty(singleSubFolder)
break;
end
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
% Add on JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
pathname = strcat('C:\\xampp\\htdocs\\PACS_Client\\cbir_matlab\\ano\\');
outputBaseFileName = sprintf('%3.3d.jpg',f);
outputFullFileName = fullfile(pathname, outputBaseFileName);
fprintf('Processing image file %s\n', fullFileName);
im=imread(fullFileName);
imshow(im);
data = im;
imwrite(data,[pathname,outputBaseFileName]);
end
else
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
我试图从所有子文件夹中的图像保存到另一个folder.But无法获得所有图像的images.Only几个数字是saved.I要保存所有images.can有人帮我这个代码?
答
我更新了一下你的代码,请检查一下是否适用于你。一个问题是你的基本文件名总是'%3.3d.jpg',所以每张图片即使不是'.jpg'也是。你也加载和显示图像,但你只需要复制它们,所以你可以去copyfile
。第三你总是设置每个图像001.jpg它将覆盖上一个文件夹的最后001.jpg。您必须添加该号码,以便下一个文件夹以更高的数字开始。
start_path = fullfile(matlabroot, '\toolbox\images\imdemos');
% Ask user to confirm or change.
topLevelFolder = uigetdir(start_path);
if topLevelFolder == 0
return;
end
%dir where everything should go. if the destination is not the
%topLevelFolder
%destinationpath = strcat('D:\\pics\\');
destinationpath = topLevelFolder;
% Get list of all subfolders.
allSubFolders = genpath(topLevelFolder);
% Parse into a cell array.
remain = allSubFolders;
listOfFolderNames = {};
%while true
% [singleSubFolder, remain] = strtok(remain, ';');
% if isempty(singleSubFolder)
% break;
% end
% listOfFolderNames = [listOfFolderNames singleSubFolder];
%end
%your while worked fine, but try to avoid 'while true' with break
for i=1:sum(strfind(allSubFolders,';'))
[singleSubFolder, remain] = strtok(remain, ';');
listOfFolderNames = [listOfFolderNames singleSubFolder];
end
numberOfFolders = length(listOfFolderNames)
%set inital count
picturecount=0;
% Process all image files in those folders.
for k = 1 : numberOfFolders
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
% Get PNG files.
filePattern = sprintf('%s/*.png', thisFolder);
baseFileNames = dir(filePattern);
% Add on TIF files.
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
% Add on JPG files.
filePattern = sprintf('%s/*.jpg', thisFolder);
baseFileNames = [baseFileNames; dir(filePattern)];
numberOfImageFiles = length(baseFileNames);
% Now we have a list of all files in this folder.
if numberOfImageFiles >= 1
% Go through all those image files.
for f = 1 : numberOfImageFiles
fullFileName = fullfile(thisFolder, baseFileNames(f).name);
[~,~,ext] = fileparts(baseFileNames(f).name); %get extension
outputBaseFileName = sprintf(['%3.3d' ext],f+picturecount);%create name based on picturecount
outputFullFileName = fullfile(destinationpath, outputBaseFileName);
%fprintf('Processing image file %s\n', fullFileName);
%im=imread(fullFileName);
%imshow(im);
%data = im;
%imwrite(data,[pathname,outputBaseFileName]);
%you dont need it in matlab just copy the file
copyfile(fullFileName,outputFullFileName);
end
picturecount=picturecount+numberOfImageFiles;%set picturecount for next k
else
fprintf(' Folder %s has no image files in it.\n', thisFolder);
end
end
+0
非常感谢。此代码工作完美...感谢您的帮助! – Jury
答
问题出现在baseFileNames = dir(filePattern)
,每当循环在新文件夹中时,您重置列表。这就是为什么最后你只有最后一个文件夹的图像。只需在for循环前添加baseFileNames = []
,然后用baseFileNames = [baseFileNames; dir(filePattern)]
替换baseFileNames = dir(filePattern)
即可。
什么图像丢失?他们都在同一个目录或不同的目录?那里有多少个文件夹? – Finn
在主文件夹中有4个子文件夹每个子文件夹都有很多图像我只能保存在第一个子文件夹中的图像 – Jury
我想将子文件夹中的所有图像保存到一个文件夹中 – Jury