Filter löschen
Filter löschen

Changing the size info of images per iteration in a for loop

2 Ansichten (letzte 30 Tage)
The situation: I'm running a for loop which anaylzes images. Each folder contains separate dataset and all are analyzed using a code that runs on subfolders within a parent folder. In some sub-folders, the Height and Width (H x W) of the images to be analyzed are different.
The problem: I get error whenever the code runs an iteration on a subfolder that contains images of different dimensions (H x W) compared to the sub-folder analyzed in the first iteration (k=1=True). What is missing here such that the code can be executed on images of different dimensions, smoothly?
Here is the relevent lines of the code I have, please read the comments:
for k = 1 : numberOfFolders;
if numberOfImageFiles >= 1600
%....some code
for s=InitFrameLast10:StartInt:LastFrameLast10;
for t=s:s+ImpFrames;
fullFileNameLast10=fullfile(thisFolder, baseFileNames{t});
fprintf(' Now reading %s\n', fullFileNameLast10);
%I want to assigning W and H (which changes per iteration) to PupilBigLast10
%The line above is where I get the error that Right (not changing--want it to be flexible) and left (changes per folder iteration) are not equal
%....some more code
%....more and more code
  4 Kommentare
Rik am 24 Mär. 2020
You are currently storing the entire content of imageArray_uncropLast10, not just its height and width. You can of course change the size of PupilBigLast10 to make imageArray_uncropLast10 fit inside it. Which of these two would you want to happen?
Lina Koronfel
Lina Koronfel am 25 Mär. 2020
Yes exactly!! How can I change the size of PupilBigLast10 to make the changing dimensions of imageArray_uncropLast10 (which changes per iterartion of "k", or "numberOfFolders") fit inside it? Thank you

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Ameer Hamza
Ameer Hamza am 25 Mär. 2020
Bearbeitet: Ameer Hamza am 25 Mär. 2020
In MATLAB, it is not possible to create a 3D matrix with a different number of rows and columns in each slice. For such situations, we use cell arrays. You can do something like this:
Add this line at the place where you are initializing PupilBigLast10
PupilBigLast10 = {};
Then, replace the line
PupilBigLast10(:,:,t) = imageArray_uncropLast10;
PupilBigLast10{t} = imageArray_uncropLast10;
  9 Kommentare
Lina KORONFEL am 30 Mär. 2020
Thank you so much Ameer!! I tested the code on several folders and it worked everytime with no errors. However, I had to create a zeros matrix for both PupilBig as well as BinaryPupilLast10, and to change the type from double to uint8, which is the type of imageArray_uncropLast10.
Here is the final code as it worked
for k = 1 : numberOfFolders;
close all
% Get this folder and print it out.
thisFolder = listOfFolderNames{k};
fprintf('Processing folder %s\n', thisFolder);
filePattern = sprintf('%s/*.tif', thisFolder);
baseFileNames = dir(filePattern);
baseFileNames= natsortfiles({});
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1601
%some constants
fullFileNameOfFolderForDimensionPurpose=fullfile(thisFolder, baseFileNames{1600});
PupilBig=zeros(H,W,k, 'uint8');
BinaryPupilLast10=zeros(H,W,k, 'uint8');
for s=InitFrameLast10:StartInt:LastFrameLast10;
for t=s:s+ImpFrames; %Selected frames/trial to be analyzed
fullFileNameLast10=fullfile(thisFolder, baseFileNames{t});
fprintf(' Now reading %s\n', fullFileNameLast10);
BinaryPupilLast10(:,:,t)=im2bw(PupilBigLast10(:,:,t), 0.4);
PixelCountLast10(t) = sum(sum(BinaryPupilLast10(:,:,t)));
%more code

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)


Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange


Community Treasure Hunt

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

Start Hunting!

Translated by