How to read and loop through multiple files in a folder and assign them to unique variables.

조회 수: 12 (최근 30일)
Hi there,
I am working on a lab project on facial recognition. This lab has instructions for each function (this is a beginner's course), but I am stuck on one function.
Here is a description of the function as found in the instructions:
function [newDatabase, indices] = unScrambleDatabase(imagePath, database)
This function should read all the images from the folder Player Images and search for their position (column) in the scrambled database. Then it should unscramble the database by reordering its columns according to the results of this search. The function accepts as inputs the imagePath or folder where the player images are stored as well as the database (matrix with vectorized images) that the user wants to unscramble. In this function, you should:
  1. Initialize the variable indices to be an array of 0’s of proper dimensions.
  2. Read all the images in the path imagePath using the function readImage() you created above.
  3. Find the minimum error position for this image in the database using the function findMinimumErrorPosition() you created above.
  4. Store all the minimum error positions, for each tested image, in the array indices.
  5. After finding the correct indices, reorder the columns in the database (shuffle the images around) so that the new ordering is according to the ordering of the stored names of the players, as it was explained earlier in this document.
  6. Finally, assign the resulting reordered database to the output variable newDatabase.
So the purpose of this entire project is to take a folder with the images of 100 NBA players faces, analyze the images, and compare them to unscramble the database.
I am having trouble with number 2 above. I want to read each of the files (found in Player_Images\player#.png), find the minimum error position (that is, find which column in the scrambled matrix contains the image that has just been read), and assign this column to the column number corresponding to the number of the image file (ex: column 3 for player3.png).
I cannot figure out how to loop through the images. I would assume I need to change the file path in the loop, but I am unsure how to do that.
This is what I am using now, but it is not working.
n = size(database,2);
indices = zeros(n,1);
newDatabase = database
for ii = 1:n
eval(sprintf('Playerimage = readImage(Player_Images\player%d.png)',ii));
indices(ii,1) = findMinimumErrorPosition(Playerimage,database);
newDatabase(:,ii) = database(:,indices(ii,1));
end
Any help is appreciated. Thank you!

답변 (2개)

Walter Roberson
Walter Roberson 2015년 11월 14일

Stephen23
Stephen23 2015년 11월 14일
편집: Stephen23 2019년 6월 19일

카테고리

Help CenterFile Exchange에서 Geometric Transformation and Image Registration에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by