Getting area of tilted ellipse with regionprops
8 views (last 30 days)
Show older comments
Sokratis Panagiotidis
on 3 Mar 2022
Commented: Sokratis Panagiotidis
on 23 Mar 2022
Hi and thank you for thank you for taking your time.
I am using the following code, made by Akira Agata, to fit an ellipse to an image:
https://de.mathworks.com/matlabcentral/answers/495720-how-to-fit-an-ellipse-to-an-image-in-matlab; 03.03.2022; 2:11 pm
% The multiplication with (7.5/1000) has simply to do with the camera's
% calibration. The mewasures are in mm^2 and mm^3 (therefore µL).
%% The code I got from Akira Agata:
s = regionprops(BW, 'Orientation', 'MajorAxisLength','MinorAxisLength','Centroid', 'Circularity');
% 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))];
The images I am using are pretty similar with the one used in the question so unless the exact image is necessary to understand the situation, I'd rather not post it. I am not sure wether I am allowed to do so or not.
Now the following part of my task is to get the volume Vm of the ellipsoid. We assume we have a symmetry around the z-axis so we use the following code to get the volume (nothing special):
% Calculate the volume
z =4/3;
a = (s.MajorAxisLength)./2;
el_a = (a*7.5)./1000; % wideness
b = (s.MinorAxisLength)./2;
el_b = (b*7.5)./1000; % height
Area = el_a*el_b*pi;
Vm = (Area*el_b*z); % The volume of the elipsoid.
I have trouble to understand the code completely, so I'd like to ask if anybody knows wether this code also makes sure to adjust it's variable to the ellipse's angle? I mean we have the 'Orientation' with regionpros but I am not sure if we use it here to adjust the Major- and MinorAxisLength. Otherwise it might use different points of the image to set as axis-lengths and I'd get a different volume.
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 Comments
Accepted Answer
Image Analyst
on 3 Mar 2022
Did you email him back, or call him?
I don't think you need the Orientation (angle) to compute volume. The area of an ellipse is pi*a*b and the volume is pi*a*b^2.
The stuff about 7.5 you probably don't need to do if you have square pixels or voxels. In fact I don't think you need most of that. You can simply do
s = regionprops(BW, 'Orientation', 'MajorAxisLength','MinorAxisLength','Centroid', 'Circularity');
lengths = [s.MajorAxisLength];
widths = [s.MinorAxisLength];
areas = pi * lengths .* widths;
volumes = areas .* widths;
3 Comments
Image Analyst
on 8 Mar 2022
The area of an ellipse is pi*a*b regardless of the tilt/angle/orientation. The axis lengths are not the length along the x or y axis so they're not multiplied by the sine of cosine of the angle. They're the full length just like you'd draw them by hand. You just use them directly in the formula.
The only reason why you'd need to know the angle is if you want to draw a line segment along the major and minor axes. In that case you'd need two endpoints (x1, y1) and (x2, y2)
s = regionprops(BW, 'Orientation', 'MajorAxisLength','MinorAxisLength','Centroid', 'Circularity');
lengths = [s.MajorAxisLength];
widths = [s.MinorAxisLength];
areas = pi * lengths .* widths;
volumes = areas .* widths;
allAngles = [s.Orientation]; % In degrees so need sind() not sin().
% Get centroids.
xy = vertcat(s.Centroid);
xc = xy(:, 1);
yc = xy(:, 2);
% Get line segments along major axes.
x1 = xc - lengths .* cosd(allAngles);
y1 = yc - lengths .* sind(allAngles);
x2 = xc + lengths .* cosd(allAngles);
y2 = yc + lengths .* sind(allAngles);
% Plot line segments along the major axes of each blob.
for k = 1 : length(x1)
plot([x1,x2], [y1,y2], 'r.-', 'LineWidth', 2, 'MarkerSize', 15);
hold on;
end
hold off;
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!