2016-08-24 148 views
0

我在這裏有一個代碼。從子文件夾讀取圖像並保存到另一個文件夾中

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有人幫我這個代碼?

+0

什麼圖像丟失?他們都在同一個目錄或不同的目錄?那裏有多少個文件夾? – Finn

+0

在主文件夾中有4個子文件夾每個子文件夾都有很多圖像我只能保存在第一個子文件夾中的圖像 – Jury

+0

我想將子文件夾中的所有圖像保存到一個文件夾中 – Jury

回答

1

我更新了一下你的代碼,請檢查一下是否適用於你。一個問題是你的基本文件名總是'%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

0

問題出現在baseFileNames = dir(filePattern),每當循環在新文件夾中時,您重置列表。這就是爲什麼最後你只有最後一個文件夾的圖像。只需在for循環前添加baseFileNames = [],然後用baseFileNames = [baseFileNames; dir(filePattern)]替換baseFileNames = dir(filePattern)即可。

+0

dir的輸出是我的matlab版本中的一個結構,這部分工作 – Finn

+0

謝謝。我試過這個。但我仍然無法獲得所有圖像。我有3個子文件夾稱爲folder1,folder2,folder3。它只保存文件夾3張圖像。 – Jury

+0

你說得對@Finn,我錯了。 – erfan

相關問題