从子文件夹中读取图像并保存到另一个文件夹中

时间:2016-08-24 05:35:25

标签: matlab

我这里有代码。

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

我试图将子文件夹中的所有图像保存到另一个文件夹中。但是无法获取所有图像。只保存了少量图像。我想保存所有图像。任何人都可以帮我这个代码?< / p>

2 个答案:

答案 0 :(得分:2)

我稍微更新了您的代码,请检查这是否适合您。一个问题是你的基本文件名总是'%3.3d.jpg'所以每张图片都是'.jpg',即使它不是。此外,您正在加载和显示图像,但您只需复制它们,因此您可以转到copyfile。 3,你总是设置每个图像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

答案 1 :(得分:1)

问题出在baseFileNames = dir(filePattern),每次循环在新文件夹上时重置列表。这就是为什么最后你只有最后一个文件夹的图像。只需在for循环之前添加baseFileNames = [],然后将baseFileNames = dir(filePattern)替换为baseFileNames = [baseFileNames; dir(filePattern)]