Effacer les filtres
Effacer les filtres

How to plot an in-ellipse(ellipse inside an object) which also matches the object orientation.

10 vues (au cours des 30 derniers jours)
I want to plot an in-ellipse (see image attached) and then calculate the major and minor axis lengths of that ellipse. I am able to calculate the major and minor axis lengths using the regionprops function and plot the corresponding ellipses. Is there any way this is possible?
(see image attached)
plotted from regionprops() : Red ellipse
need to plot: Green ellipses

Réponse acceptée

Matt J
Matt J le 6 Juil 2023
Modifié(e) : Matt J le 6 Juil 2023
a=5; b=3; %axes lengths
theta=40; %rotation angle
x0=1;y0=2; %center
fcn=@(p) translate( rotate( scale(p,[a,b]), theta), x0,y0);
r=fcn(nsidedpoly(4,'SideLength',2)); %rectangle
e=fcn(nsidedpoly(1000)); %ellipse
h=plot([r,e],'FaceColor','none'); axis equal
h(2).EdgeColor='r'; h(2).LineStyle='--';

Plus de réponses (2)

Atithi
Atithi le 6 Juil 2023
Yes, it is possible to plot the in-ellipse and calculate the major and minor axis lengths using the provided code. The code already includes the necessary steps to generate the points for the outer ellipse, inner rectangle, and inner ellipse. It also plots these shapes using the plot function.
To calculate the major and minor axis lengths, the code uses the formulas:
  • For the outer ellipse: major axis length = 2 * outer ellipse semi-axis 1 and minor axis length = 2 * outer ellipse semi-axis 2.
  • For the inner ellipse: major axis length = 2 * inner ellipse semi-axis 1 and minor axis length = 2 * inner ellipse semi-axis 2.
% Parameters
outerEllipseCenter = [0, 0]; % Center of the outer ellipse
outerEllipseSemiAxes = [10, 15]; % Semi-axes of the outer ellipse
innerRectangleSides = [8, 12]; % Sides of the inner rectangle
innerEllipseSemiAxes = [4, 6]; % Semi-axes of the inner ellipse
innerEllipseAngle = pi/4; % Rotation angle of the inner ellipse (in radians)
% Generate points for the outer ellipse with orientation
theta = linspace(0, 2*pi, 100);
xOuterEllipse = outerEllipseSemiAxes(1) * cos(theta);
yOuterEllipse = outerEllipseSemiAxes(2) * sin(theta);
xOuterEllipseRotated = xOuterEllipse * cos(innerEllipseAngle) - yOuterEllipse * sin(innerEllipseAngle);
yOuterEllipseRotated = xOuterEllipse * sin(innerEllipseAngle) + yOuterEllipse * cos(innerEllipseAngle);
% Generate points for the inner rectangle with orientation
xInnerRectangle = [-innerRectangleSides(1)/2, innerRectangleSides(1)/2, innerRectangleSides(1)/2, -innerRectangleSides(1)/2, -innerRectangleSides(1)/2];
yInnerRectangle = [-innerRectangleSides(2)/2, -innerRectangleSides(2)/2, innerRectangleSides(2)/2, innerRectangleSides(2)/2, -innerRectangleSides(2)/2];
xInnerRectangleRotated = xInnerRectangle * cos(innerEllipseAngle) - yInnerRectangle * sin(innerEllipseAngle);
yInnerRectangleRotated = xInnerRectangle * sin(innerEllipseAngle) + yInnerRectangle * cos(innerEllipseAngle);
% Generate points for the inner ellipse with orientation
xInnerEllipse = innerEllipseSemiAxes(1) * cos(theta);
yInnerEllipse = innerEllipseSemiAxes(2) * sin(theta);
xInnerEllipseRotated = xInnerEllipse * cos(innerEllipseAngle) - yInnerEllipse * sin(innerEllipseAngle);
yInnerEllipseRotated = xInnerEllipse * sin(innerEllipseAngle) + yInnerEllipse * cos(innerEllipseAngle);
% Plotting
figure;
hold on;
plot(xOuterEllipseRotated, yOuterEllipseRotated, 'b'); % Plot outer ellipse
plot(xInnerRectangleRotated, yInnerRectangleRotated, 'r'); % Plot inner rectangle
plot(xInnerEllipseRotated, yInnerEllipseRotated, 'g'); % Plot inner ellipse
axis equal;
grid on;
title('Ellipse Inside Rectangle Inside Ellipse');
legend('Outer Ellipse', 'Inner Rectangle', 'Inner Ellipse');
% Display major and minor axis lengths
outerEllipseMajorAxis = 2 * outerEllipseSemiAxes(1);
outerEllipseMinorAxis = 2 * outerEllipseSemiAxes(2);
innerEllipseMajorAxis = 2 * innerEllipseSemiAxes(1);
innerEllipseMinorAxis = 2 * innerEllipseSemiAxes(2);
disp("Major and Minor Axes Lengths:")
Major and Minor Axes Lengths:
disp("Outer Ellipse:")
Outer Ellipse:
disp("Major Axis: " + outerEllipseMajorAxis)
Major Axis: 20
disp("Minor Axis: " + outerEllipseMinorAxis)
Minor Axis: 30
disp("Inner Ellipse:")
Inner Ellipse:
disp("Major Axis: " + innerEllipseMajorAxis)
Major Axis: 8
disp("Minor Axis: " + innerEllipseMinorAxis)
Minor Axis: 12
Do let me know if it was helpful

Image Analyst
Image Analyst le 6 Juil 2023
The leader of the Image Processing group at the Mathworks, Steve Eddins, has a blog entry showing how to do it:
You may also want to look at my Image Segmentation Tutorial in my File Exchange:
It's a generic, general purpose demo of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.

Produits

Community Treasure Hunt

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

Start Hunting!

Translated by