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

조회 수: 1 (최근 30일)
Lina Koronfel
Lina Koronfel 2020년 3월 24일
댓글: Ameer Hamza 2020년 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!!
Ameer Hamza
Ameer Hamza 2020년 3월 30일
Glad to be of help.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by