Function inside for-loop won't transfer elemnt into main program's vector
2 ビュー (過去 30 日間)
古いコメントを表示
Sokratis Panagiotidis
2022 年 3 月 2 日
コメント済み: Sokratis Panagiotidis
2022 年 3 月 3 日
Thank you for taking your time.
i am working right now on a project for my bachelor's thesis and am having trouble with a certain part of my code.
The code is meant to:
- read a folder selected by the user themselves
- Inside a for-loop it reads all the PNG files in the folder (meaning everything als like .txt, .xlsx or simply another folder will be ignored)
- binarize every image, which is simply a circle in gray scale
- fill the hole with imfill, so you'll have an image of a white circle on a black background
- fit an ellipse on the circle and by assuming we have symmetry around the z-axis, we manage to determne the ellipsoids volume Vm.
Now, since this is supposed to be a subprogramm for a much bigger programm, I am supposed to put all that (and more but that won't be neccessary to mention as the other parts work) into a for-loop and reference a function, which I called Bildauswertung.
So this is how the main programm (minus the folder selection) looks like:
filePattern = fullfile(FolderWithImages, '*.png'); % Makes sure to only select PNG-files.
theFiles = dir(filePattern); % Get a list of all files in the folder with the desired file name pattern.
%% II. Read p images, binarize and fill the holes.
Vm(1:length(theFiles),1)= NaN; % Starts a vector here to insert the volume of every drop at the end.
for p = 1 : length(theFiles) % for-loop to go through every single PNG-file in the folder
% baseFileName = theFiles(p).name;
fullFileName = fullfile(theFiles(p).folder, theFiles(p).name);
% FUNCTION
Bildauswertung(fullFileName);
Vm(p) = Vm;
drawnow; % Select file # p+1.
end
Right before the loop I created a vector with as many rows as the folder has images, but with empty entries. The goal here is to get the Volume Vm from every image and add it into the current row of the vector (p), before going to the next PNG-file (p+1) and repeating it.
The problem is once I leave the function, to get back into the main programm's loop to save the value Vm I just managed to get in the function, it gets lost. I only get a vector Vm full of 'NaN'-entries. this worked perfectly fine inside the loop. the problem occured once I had to insert that part into a function.
And here is the important part of the function.
function [c, el_a, el_b, el_t, Vm] = Bildauswertung(im)
%% Use Threshold and fill the inside.
I = imread(im); % Read the image as an image-array
BW1 = ~imbinarize(I); % Binarize the image-array, but iverse the value;
BW2 = imfill(BW1,'holes'); % Converts the white hole into pure black
%% Extract biggest blob
BW = bwareafilt(BW2, 1);
%% Fit an elipse around every drop and determine its volume (ellipsoid, symmetry)
%Calculate centroid, orientation and major/minor axis length of the ellipse
s = regionprops(BW, 'Orientation', 'MajorAxisLength','MinorAxisLength','Centroid', 'Circularity');
% Orientation - Angle between the x-axis and the major axis of the ellipse
% ranging from {-90;90}
% Calculate the ellipse line
theta = linspace(0,2*pi);
col = (s.MajorAxisLength/2)*cos(theta);
row = (s.MinorAxisLength/2)*sin(theta);
M = makehgtform('translate',[s.Centroid, 0],'zrotate',deg2rad(-1*s.Orientation));
D = M*[col;row;zeros(1,numel(row));ones(1,numel(row))];
% D wäre die Ellipse. Die würde man eigentlich unten plotten, aber bei
% der Menge der Bilder macht es keinen Sinn so viel zu plotten.
z =4/3;
a = (s.MajorAxisLength)./2; % Breite/2 [px] (Abweichung um ~ 0,1 px)
ela_a = (a*7.5)./1000; % Breite [mm]
%Output 3
b = (s.MinorAxisLength)./2; % Hoehe/2 [px](Abweichung um ~ 0,1-0,2 px)
el_b = (b*7.5)./1000; % Hoehe [mm]
%Output 4
Area = ela_a*el_b*pi;
Vm = (Area*el_b*z); % [mm^3] bzw. [µL]
%Output 6
%% Outputs
%Output 1
c = s.Circularity; % Neigung der Ellipse %Output 2
el_t = s.Orientation; %Output 5
%% Plots (won't be used but can help you understand some parts of the code
% plot(D(1,:),D(2,:),'r','LineWidth',2)
%%% plots the ellipse on the surface (makes sure to plot around the drop and not leave anything out)
end
(There is one output I didn't insert because this is already too long so I wanted to cut out the unnecessary parts.)
How can I manage to transfer the values of Vm from my function to the main programm, so that I can put them all into a vector? I figured it should've been able to do so as I marked it as one of the outputs (output 6).
If you need any more Information just ask me! I'd be glad if someone could help me with an answer. Thank you for your time anyways, wether you can help me or not!
0 件のコメント
採用された回答
David Hill
2022 年 3 月 2 日
for p = 1 : length(theFiles) % for-loop to go through every single PNG-file in the folder
% baseFileName = theFiles(p).name;
fullFileName = fullfile(theFiles(p).folder, theFiles(p).name);
% FUNCTION
[c, el_a, el_b, el_t, Vm]=Bildauswertung(fullFileName);%just add the outputs
Vm(p) = Vm;
drawnow;
end
その他の回答 (1 件)
David Hill
2022 年 3 月 2 日
[~,~,~,~,Vm1(p)]=Bildauswertung(fullFileName);%you could also just assign it without an additional line
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!