Reputation: 25
I have a code here.
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
i tried to save all the images from sub folders into another folder.But could not get all the images.Only few numbers of images are saved.I want to save all the images.can anyone help me with this code?
Upvotes: 1
Views: 712
Reputation: 1927
The problem is in baseFileNames = dir(filePattern)
, where you reset the list each time the loop is on a new folder. This is why at the end you will only have the images of the last folder. Simply add baseFileNames = []
just before the for loop and then replace baseFileNames = dir(filePattern)
with baseFileNames = [baseFileNames; dir(filePattern)]
.
Upvotes: 1
Reputation: 2343
i updated your code a bit please check if this works for you. One issue being your base file name always is '%3.3d.jpg' so every picture will be a '.jpg' even if its not. Also you are loading and showing images, but you only need to copy them, so you can go for copyfile
. 3rd you always setting every image 001.jpg which will overwrite the the last 001.jpg from the previous folder. you have to add the number so the next folder starts with higher numbers.
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
Upvotes: 2