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

3 次查看(过去 30 天)
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 个评论
Rik
Rik 2020-3-24
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 2020-3-25
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

请先登录,再进行评论。

采纳的回答

Ameer Hamza
Ameer Hamza 2020-3-25
编辑:Ameer Hamza 2020-3-25
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 个评论
Lina KORONFEL
Lina KORONFEL 2020-3-30
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!!

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Images 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by