How can I fill an array of images from inside a for loop?

Hi all.
I'm trying to read a bunch of images of faces from a directory within the current directory into an accessible array I can use later on to display one of them randomly.
Here's what I've got so far:
FaceFolder = 'Faces';
if ~isdir(FaceFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', FaceFolder);
uiwait(warndlg(errorMessage));
return;
end
filePattern = fullfile(FaceFolder, '*.bmp');
bitmapFiles = dir(filePattern);
AllFaces=zeros(length(bitmapFiles), 638);
for k = 1:length(bitmapFiles)
baseFileName = bitmapFiles(k).name;
fullFileName = fullfile(FaceFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
currentim = imread(fullFileName);
AllFaces(k,:) = currentim;
end
I keep getting "Subscripted assignment dimension mismatch" errors for the AllFaces(k,:) = currentim; line.
638 is what I believe to be the size of one of the bitmap files (they're all the same dimensions) hence that number, so I tried using that to set up an array of zeros before the loop to fill it in but clearly I'm doing something wrong.
Anyone have any ideas?

Respuestas (1)

Evan
Evan el 17 de Jun. de 2013
It looks like you're trying to assign a [nxm] matrix (the image) to a [1xm] row in AllFaces. Instead, try this:
AllFaces(:,:,k) = currentim;

4 comentarios

Max
Max el 17 de Jun. de 2013
Editada: Max el 17 de Jun. de 2013
Hmm I'm still returned with the same "Subscripted assignment dimension mismatch." error.
When I try:
length(currentim)
I'm returned with the value 638 which is where I got that number.
I also tried:
size(currentim)
And was returned with 480, 638, 3.
So I tried:
AllFaces=zeros(length(bitmapFiles), 480, 638, 3);
for k = 1:length(bitmapFiles)
baseFileName = bitmapFiles(k).name;
fullFileName = fullfile(FaceFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
currentim = imread(fullFileName);
AllFaces(:,:,:,k) = currentim;
end
But still same error.
Evan
Evan el 17 de Jun. de 2013
Editada: Evan el 17 de Jun. de 2013
Oh, I didn't see above where you initialized "AllFaces" before your loop. That's the problem. Does changing that part to the below code work?
AllFaces = zeros(480,638,3,length(bitmapFiles))
Ah, sorry I miscopied something.
Here's the most recent iteration that still returns the error:
AllFaces=zeros(length(bitmapFiles), 480, 638, 3);
for k = 1:length(bitmapFiles)
baseFileName = bitmapFiles(k).name;
fullFileName = fullfile(FaceFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
currentim = imread(fullFileName);
AllFaces(k,:,:,:) = currentim;
end
The pre-allocated array's "length" column is aligned with the "k" column I'd like to fill which I think is right.

Iniciar sesión para comentar.

Categorías

Más información sobre Loops and Conditional Statements en Centro de ayuda y File Exchange.

Preguntada:

Max
el 17 de Jun. de 2013

Community Treasure Hunt

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

Start Hunting!

Translated by