Changing the size info of images per iteration in a for loop
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Lina Koronfel
am 24 Mär. 2020
Kommentiert: Ameer Hamza
am 30 Mär. 2020
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);
imageArray_uncropLast10=imread(fullFileNameLast10);
FigInfo=imfinfo(fullFileNameLast10);
W=FigInfo.Width;
H=FigInfo.Height;
%I want to assigning W and H (which changes per iteration) to PupilBigLast10
PupilBigLast10(:,:,t)=imageArray_uncropLast10;
%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
end
end
%....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?
Akzeptierte Antwort
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;
with
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
try
% 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({baseFileNames.name});
numberOfImageFiles = length(baseFileNames);
if numberOfImageFiles >= 1601
%some constants
fullFileNameOfFolderForDimensionPurpose=fullfile(thisFolder, baseFileNames{1600});
%imageArrayofFolderForDimensionPurpose=imread(fullFileNameOfFolderForDimensionPurpose);
FigInfo=imfinfo(fullFileNameOfFolderForDimensionPurpose);
H=FigInfo.Height;
W=FigInfo.Width;
%PupilBigLast10=zeros(H,W,k);
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);
imageArray_uncropLast10=imread(fullFileNameLast10);
PupilBigLast10(:,:,t)=imageArray_uncropLast10;
BinaryPupilLast10(:,:,t)=im2bw(PupilBigLast10(:,:,t), 0.4);
PixelCountLast10(t) = sum(sum(BinaryPupilLast10(:,:,t)));
end
end
%more code
Cheers!!
Weitere Antworten (0)
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!