How to access names of pictures in a file?
Ältere Kommentare anzeigen
The name of the images have important information that I need to store in different variables for further processing.
For example the name of the image is: gh_368053_209864_19
And there are 8 images with different numbers. I need to access 368053 and store it in x separately and store 209864 in y. And I want to do this for all the images in the file in a loop. Store x and y in two different arrays.
I will also apply other functions to the accessed images!
How can this be done only with names of the images?
clear all
clc;
location = 'E:\ATR\*.JPG'; % folder in which your template images exists
img= imread('E:\ATR\gh_368053_209864_19.JPG');
ds = imageDatastore(location); % Creates a datastore for all images in your folder
% fileNames={a.name};
while hasdata(ds)
temp = read(ds); % read image from datastore
% Image_name = temp.name;
% figure, imshow(temp)
end
Akzeptierte Antwort
Weitere Antworten (1)
Marco Riani
am 3 Jan. 2020
Hi, please let me know if the code below works.
clear
clc;
% location = folder in which your template images exists
location = 'D:\MATLAB\answers\images';
% ds = datastore for all images in your folder
ds = imageDatastore(location);
% Get all the filenames
temp = ds.Files;
% ltemp = number of images
ltemp=length(temp);
% maxFileNames = maximum number of filenames in the strings of File Names
maxFileNames=5;
% X= cell which will contain in column 1 first name,
% in column 2 second name,
% ....
X=cell(ltemp,maxFileNames);
for i=1:ltemp
% FullName = full name with path
FullName=temp{i};
% Extract just the name (e.g. name='DSC_2834_34567')
[~,name]=fileparts(FullName);
% Find the position of "_" in the filename
posUs=regexp(name,'_');
% We assume that the first name is after "_" sign
if ~isempty(posUs)
lname=length(name);
% pad posUs with length(name) at the end to make sure that every time you have the
% same length
posUsSL=[posUs (lname+1)*ones(1,maxFileNames+2-length(posUs))];
% Insert in row i col j of cell X the filename
for j=1:maxFileNames
X{i,j}=name(posUsSL(j)+1:posUsSL(j+1)-1);
end
end
end
% In my case (given that I specified maxFileNames=5)
%
% temp =
%
% 9×1 cell array
%
% {'D:\MATLAB\answers\images\DSC_2834_34567.jpg' }
% {'D:\MATLAB\answers\images\DSC_2835.jpg' }
% {'D:\MATLAB\answers\images\DSC_2836.jpg' }
% {'D:\MATLAB\answers\images\DSC_2837.jpg' }
% {'D:\MATLAB\answers\images\DSC_2911_78976_876.jpg'}
% {'D:\MATLAB\answers\images\DSC_2914.jpg' }
% {'D:\MATLAB\answers\images\DSC_2921_6785.jpg' }
% {'D:\MATLAB\answers\images\DSC_2922_9843.jpg' }
% {'D:\MATLAB\answers\images\DSC_2923_8765.jpg' }
%
%
% X =
%
% 9×5 cell array
%
% {'2834'} {'34567' } {1×0 char} {1×0 char} {1×0 char}
% {'2835'} {1×0 char} {1×0 char} {1×0 char} {1×0 char}
% {'2836'} {1×0 char} {1×0 char} {1×0 char} {1×0 char}
% {'2837'} {1×0 char} {1×0 char} {1×0 char} {1×0 char}
% {'2911'} {'78976' } {'876' } {1×0 char} {1×0 char}
% {'2914'} {1×0 char} {1×0 char} {1×0 char} {1×0 char}
% {'2921'} {'6785' } {1×0 char} {1×0 char} {1×0 char}
% {'2922'} {'9843' } {1×0 char} {1×0 char} {1×0 char}
% {'2923'} {'8765' } {1×0 char} {1×0 char} {1×0 char}
Kategorien
Mehr zu Matrix Indexing finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!