how to rename images using a loop?

2 ビュー (過去 30 日間)
souha abdalah
souha abdalah 2015 年 12 月 17 日
コメント済み: souha abdalah 2016 年 1 月 13 日
Hello. I have 150 images (images_0, images_1, images_2, ......, images_140) which are saved in 4 folders called : dataset_1, dataset_2, dataset_3 and dataset_4.
I use this code to rename images
mainDirectory = 'C:\Users\Desktop\data';
subDirectory = dir([mainDirectory '/dataset_*']);
for m = 1 : length(subDirectory)
subFolder = dir(fullfile(mainDirectory, subDirectory(m).name,'*.png'));
fileNames = {subFolder.name};
for iFile = 1 : numel( subFolder )
newName = fullfile(mainDirectory, subDirectory(m).name, sprintf('%00d.png',(iFile) ) );
movefile( fullfile(mainDirectory, subDirectory(m).name, fileNames{ iFile }), newName );
end
end
This code works well, but I'm new in MATLAB and I want to change the newName as follows : (number of the dataset)_(name of the image)
For example : 1_images_0, 1_images_1, 2_images_0, 2_images_1, ...
Please, any idea how can I change the newName to rename the images? Please help me and thanks in advance.
  6 件のコメント
jgg
jgg 2015 年 12 月 17 日
Sorry, I'm not clear what's wrong with the solution I suggested? Are your folders not arranged in order (so dataset 2 is folder 4 for instance?). If so, you should be able to easy adjust the code I gave with a simple lookup matrix.
Geoff Hayes
Geoff Hayes 2015 年 12 月 17 日
Or just remove the dataset_ portion and convert the remainder to a string. For example,
folderName = 'dataset_42';
folderId = str2num(strrep(folderName,'dataset_',''));
Or use regexp to do something similar.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2015 年 12 月 18 日
This will do it:
% Start with a folder and get a list of all subfolders.
% Finds and prints names of all files in
% that folder and all of its subfolders.
% Similar to imageSet() function in the Computer Vision System Toolbox: http://www.mathworks.com/help/vision/ref/imageset-class.html
clc; % Clear the command window.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
% Define a starting folder.
start_path = pwd; % %'C:\Users\Desktop\data';
if ~exist(start_path, 'dir')
start_path = matlabroot;
end
% Ask user to confirm or change.
uiwait(msgbox('Pick a starting folder on the next window that will come up.'));
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 ALL files in this folder.
filePattern = sprintf('%s/*.*', thisFolder);
baseFileNames = dir(filePattern);
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1
% Go through all those files.
for f = 1 : numberOfImageFiles
existingFullFileName = fullfile(thisFolder, baseFileNames(f).name);
if isdir(existingFullFileName)
% Skip folders . and ..
continue;
end
% Get the last character of the folder. It should be a number from 1 to 4.
lastDigit = thisFolder(end);
% Create a new name for it.
newBaseFileName = sprintf('%s_images_%d.png', lastDigit, f-1);
newFullFileName = fullfile(thisFolder, newBaseFileName);
fprintf(' Renaming file %s to %s\n', existingFullFileName, newFullFileName);
% Do the actual renaming.
movefile(existingFullFileName, newFullFileName);
end
else
fprintf(' Folder %s has no files in it.\n', thisFolder);
end
end
  9 件のコメント
Image Analyst
Image Analyst 2016 年 1 月 13 日
編集済み: Image Analyst 2016 年 1 月 13 日
Originally you said it only went up to 4 but now you say it can go past 9 and be two or more digits. The code I wrote was for what you said originally. If you want a more general solution, then replace this line
% Get the last character of the folder. It should be a number from 1 to 4.
lastDigit = thisFolder(end);
with this code to extract everything after the underline.
% Get the last characters of the folder, after the only underline.
% It should be a number.
underlineLocation = strfind(thisFolder, '_');
lastDigit = thisFolder((underlineLocation +1):end);
Of course this will also fail if you present thisFolder that has no underlines or more than 1 underline. You'd need more code to detect and handle those situations.
souha abdalah
souha abdalah 2016 年 1 月 13 日
thank you very much, that is the solution that I'm looking for

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by