How to access names of pictures in a file?
8 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
maaham banu
am 3 Jan. 2020
Beantwortet: Marco Riani
am 3 Jan. 2020
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
0 Kommentare
Akzeptierte Antwort
awezmm
am 3 Jan. 2020
Bearbeitet: awezmm
am 3 Jan. 2020
if all the image names follow the pattern: gh_someNumber1_someNumber2_someNumber3
and you need to store someNumber1 and someNumber2 in separate variables:
% example array of file names:
example_img_names = {...
'E:\ATR\gh_368053_209864_19.JPG',...
'E:\ATR\gh_542191_032861_19.JPG',...
'E:\ATR\gh_541666_417980_19.JPG'};
% make empty cell array to hold all the x numbers (all someNumber1's).
% initialize it with size of 1 x number of image names
all_x_numbers = cell(1,length(example_img_names));
% make empty cell array to hold all the y numbers (all someNumber2's).
% initialize it with size of 1 x number of image names
all_y_numbers = cell(1,length(example_img_names));
% for loop to go through all example_img_name
for i = 1:length(example_img_names)
% get image name at current index
curr_img_name = example_img_names{i};
% break up filename into parts
[filepath,name,ext] = fileparts(curr_img_name);
% split filename by '_' character
array_of_splits = strsplit(name, '_');
% second element in split array will be someNumber1
all_x_numbers{i} = array_of_splits{2};
% third element in split array will be someNumber2
all_y_numbers{i} = array_of_splits{3};
end
disp("x numbers:")
disp(all_x_numbers)
disp("y numbers:")
disp(all_y_numbers)
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}
0 Kommentare
Siehe auch
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!