MATLAB Answers

How do I select only 100 images in a folder of 200 images, according to a list of 100 different names ?

4 views (last 30 days)
Hello,
I have a list of 100 different pictures.
I have the name of the 100 pictures, for instance :
bananas
strawberrys
etc
I want to select those 100 images in the folder in which there are 200 images in total.
I can do this by hand, but does anyones have an idea of code to do so ?
Thanks a lot

  0 Comments

Sign in to comment.

Accepted Answer

Subhadeep Koley
Subhadeep Koley on 4 Feb 2020
Assuming you have the required image names in a text file. Refer the code below.
myFolder = 'putYourFolderPathHere';
filePattern = fullfile(myFolder, '*.tiff'); % Change the extension according to your file extension
tiffFiles = dir(filePattern);
fid = fopen('fileNameList.txt', 'rt'); % Assuming fileNameList.txt contains the names of the required images
idx = 1;
while feof(fid) == 0
tline = fgetl(fid);
fileNamesToBeInc{idx} = tline;
idx = idx+1;
end
for k = 1:length(tiffFiles)
baseFileName = tiffFiles(k).name;
for i = 1:length(fileNamesToBeInc)
if strcmp(fileNamesToBeInc{i}, baseFileName)
fullFileName = fullfile(myFolder, baseFileName);
temp{k} = imread(fullFileName);
end
end
end
imageArray = temp(~cellfun('isempty',temp)); % imageArray contains your image files
% Display all the selected images
montage(imageArray);

  6 Comments

Show 3 older comments
Subhadeep Koley
Subhadeep Koley on 5 Feb 2020
@Stephen Cobeldick: Yes sir, I have incorporated the changes. The updated code is below. Thanks!
myFolder = 'putYourFolderPathHere';
filePattern = fullfile(myFolder, '*.tiff'); % Change the extension according to your file extension
tiffFiles = dir(filePattern);
fid = fopen('fileNameList.txt', 'rt'); % Assuming fileNameList.txt contains the names of the required images
idx = 1;
while feof(fid) == 0
tline = fgetl(fid);
fileNamesToBeInc{idx} = tline;
idx = idx+1;
end
fclose(fid);
temp = cell(1, length(fileNamesToBeInc));
idx = 1;
for k = 1:length(tiffFiles)
baseFileName = tiffFiles(k).name;
if ismember(baseFileName, fileNamesToBeInc)
fullFileName = fullfile(myFolder, baseFileName);
temp{idx} = imread(fullFileName);
idx = idx + 1;
end
end
imageArray = temp(~cellfun('isempty', temp)); % imageArray contains your image files
Solene Frileux
Solene Frileux on 6 Feb 2020
Thanks a lot to you both.
Everything works perfectly until the creation of temp, which cells are created but empty.
imageArray contains 1x0 cell
fullFileName does not seem to be created.
Here is my file .txt as asked.
Thanks again
Subhadeep Koley
Subhadeep Koley on 6 Feb 2020
Yes, because your text file only contains the name of the images without their extension (e.g., .jpeg, .tiff, .png, etc.). Use the code below which appends the extension after the filenames.
myFolder = 'putYourFolderPathHere';
filePattern = fullfile(myFolder, '*.tiff'); % Change the '*.tiff' extension according to your file extension
tiffFiles = dir(filePattern);
fid = fopen('images_cent_names.txt', 'rt'); % Assuming fileNameList.txt contains the names of the required images
idx = 1;
while feof(fid) == 0
tline = fgetl(fid);
fileNamesToBeInc{idx} = tline;
idx = idx+1;
end
fclose(fid);
temp = cell(1, length(fileNamesToBeInc));
% Appending extension
for k = 1:length(fileNamesToBeInc)
fileNamesToBeInc{k} = [fileNamesToBeInc{k}, '.tiff']; % Change the '.tiff' extension also according to your file extension
end
% Reading files
idx = 1;
for k = 1:length(tiffFiles)
baseFileName = tiffFiles(k).name;
if ismember(baseFileName, fileNamesToBeInc)
fullFileName = fullfile(myFolder, baseFileName);
temp{idx} = imread(fullFileName);
idx = idx + 1;
end
end
imageArray = temp(~cellfun('isempty', temp)); % imageArray contains your image files

Sign in to comment.

More Answers (1)

J Chen
J Chen on 4 Feb 2020
Edited: J Chen on 4 Feb 2020
Use the folllwoing command to get a structure that contains file information
listing = dir('*.jpg');
Use the folllwoing command to load the image files
for i = 1:100
A(i) = imread(listing(i).name);
end

  1 Comment

Solene Frileux
Solene Frileux on 5 Feb 2020
Hello J Chen,
Thanks a lot for your answer.
I think I was not rpecise enough and the other answer is more adequate.
Indeed, I need to select a hundred pictures out of 200, from a file containing the 200, knowing I have a txt file where the names of the 100 pictures I want is written.
Thanks for your help,
Solène

Sign in to comment.