0

I'm building a facial recognition program and loading a whole bunch of images which will be used for training.

Currently, I'm reading my images in using double loops, iterating through subfolders in a folder.

Is there any way, as it iterates, that the images file name can be used before the image is read and stored?

eg. I have an image person001.jpg. How can you retrieve that name (person001) then read the image in like: person001 = imread('next iteration of loop which happens to be person001');

Thanks in advance.

2
  • You can retrieve the contents of a directory using dir(). You are essentially looking for dynamic variable name generation, so try genvarname(). It should be pretty straight forward to continue from these two functions. Commented Mar 5, 2014 at 21:21
  • Awesome, I'll have to remember that function. Thanks. Commented Mar 5, 2014 at 22:38

1 Answer 1

5

I strongly recommend not to use unstructured variables. First it's very difficult to do operations like "iterate over all images", second you can get strange problems covering a function name with a variable name. Instead i would use a struct with dynamic field names or a map. A solution with a Map propably allows all possible file names.

Dynamic field names:

dirlisting=dir('.jpg');
for imageIX=1:numel(dirlisting)
 %cut of extension:
 [~,name,~]=fileparts(dirlisting(imageIX).name);
 allImages.(name)=imread(dirlisting(imageIX).name);
end

You can access the images in a struct with allImages.person001 or allImages.(x)

Map:

allImages=containers.Map
dirlisting=dir('.jpg');
for imageIX=1:numel(dirlisting)
 %cut of extension:
 [~,name,~]=fileparts(dirlisting(imageIX).name);
 allImages(name)=imread(dirlisting(imageIX).name);
end

You can access the images in a Map using allImages('person001'). Using a Map there is no need to cut of the file extension.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.