I have x and y values, but, how can I draw the shape?
Mostrar comentarios más antiguos
I have the following x and y values:
149.5000 75.5000 451.5000 279.5000
which denote (respectively):
x-min y-min x-max y-max
Having those dimensions, how can I draw them as a shape that will represent the boundary with the above values?
Thanks.
5 comentarios
José-Luis
el 19 de Dic. de 2013
You mean a rectangle?
med-sweng
el 19 de Dic. de 2013
José-Luis
el 19 de Dic. de 2013
There are infinitely many polygons that could be defined that way. I am afraid you will need to be more specific. Is something like convhull() what you are looking for?
Image Analyst
el 19 de Dic. de 2013
With a convex hull, if the shape were a "E" shape, you'd get a rectangle. The convex hull is like if you stretched a rubber band around all of your vertices. It's the shape of the rubber band. All of the concave parts would get "filled in."
Respuestas (2)
doc convhull
nVal = 50;
xx = 149.5 + rand(nVal,1).*(451.5-149.5);
yy = 75.5 + rand(nVal,1).*(279.5-75.5);
k = convhull(xx,yy);
plot(xx(k),yy(k),'r-',xx,yy,'b+')
doc patch if you want a filled polygon.
10 comentarios
med-sweng
el 19 de Dic. de 2013
José-Luis
el 20 de Dic. de 2013
Have you tried patch? Please look at the documentation and feel free to come back with questions if you don't understand something.
med-sweng
el 20 de Dic. de 2013
José-Luis
el 20 de Dic. de 2013
nVal = 50;
xx = 149.5 + rand(nVal,1).*(451.5-149.5);
yy = 75.5 + rand(nVal,1).*(279.5-75.5);
k = convhull(xx,yy);
plot(xx(k),yy(k),'r-',xx,yy,'b+');
patch(xx(k),yy(k),[1 0 0],'FaceAlpha',0.5);
Please accept an answer if it helps you.
Youssef Khmou
el 20 de Dic. de 2013
i vote for this answer
Image Analyst
el 20 de Dic. de 2013
But med-sweng shouldn't call convhull unless he needs to.
Youssef Khmou
el 20 de Dic. de 2013
@Image Analyst : convhull seems mandatory if the shape is not rectangle no?
José-Luis
el 20 de Dic. de 2013
There's always an alpha shape, but Matlab does not do that natively.
Image Analyst
el 20 de Dic. de 2013
Editada: Image Analyst
el 20 de Dic. de 2013
convhull is not mandatory. The original poster merely said that he had corners of the bounding box of some shape and he wanted to plot it. No convex hull is necessary. Moreover, even if the shape inside the bounding box was weird, say a star shape or irregular blob shape, and say you wanted to plot that, then there is no reason to call convhull. For example, look at this code that draws a soft star. No convhull is called. And med-swing didn't ask for convhull he just said he wanted to know how to plot the bounding box defined by a vector where the x and y were all in the same vector.
% Demo macro to draw a rounded star (like a splat).
%
clc;
close all;
clear all;
workspace;
% Select the inner and outer radius.
outerRadius = 44 % You can change this
innerRadius = 19 % You can change this
% Select the number of lobes around the circle.
numberOfLobes = 8; % You can change this
period = 2 * pi / numberOfLobes;
meanRadius = (outerRadius + innerRadius)/2
amplitude = (outerRadius - innerRadius)/2
t = (0:.005:1)*2*pi; % Independent parameter.
variableRadius = amplitude * cos(2*pi*t/period) + meanRadius
subplot(2,2,1);
plot(variableRadius);
ylim([0 outerRadius]);
title('VariableRadius');
period = 2*pi; % Need to change this now.
x2 = variableRadius .* cos(2*pi*t/period);
y2 = variableRadius .* sin(2*pi*t/period);
subplot(2,2,2);
plot(t, x2);
title('x2 vs. t');
subplot(2,2,3);
plot(t, y2);
title('y2 vs. t');
subplot(2,2,4);
plot(x2,y2,'b')
title('x2 vs y2');
% Maximize window.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
set(gcf,'name','Image Analysis Demo','numbertitle','off')
I think José-Luis may have misunderstood the question when med-sweng asked how to draw "a shape that will represent the boundary with the above values" - he had the shape already - an x min and max, and a y min and max. He was not asking how to find a different shape of the bounding shape. If he had , then a convex hull might have been a good suggestion (or a call to regionprops), but he already has the bounding box, given by that array, and just needed to know how to plot it.
Youssef Khmou
el 20 de Dic. de 2013
he did not specify the shape so there is an infinity, you first gave the first shape ; a rectangle, then José gave a deformed rectangle using rand. The idea is clear now .
Image Analyst
el 19 de Dic. de 2013
Try this:
m = [149.5000 75.5000 451.5000 279.5000]
x1 = m(1);
x2 = m(3);
y1 = m(2);
y2 = m(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'r', 'LineWidth', 3);
grid on;
7 comentarios
Image Analyst
el 19 de Dic. de 2013
You can use
plot(x,y)
where x and y are vectors of the x coordinates and y coordinates of a polygon. I repeated x and y because I had 5 points to visit: the upper left, the upper right, the bottom right, the bottom left, and, to close off the rectangle I had to visit the upper left again (otherwise you'd get a rectangle with no left side.)
You can fill using fill(), patch(), or, if you have a rectangle or circle, using the rectangle() function.
Image Analyst
el 20 de Dic. de 2013
Editada: Image Analyst
el 20 de Dic. de 2013
For example, I added a call to patch onto my previous answer.
m = [149.5000 75.5000 451.5000 279.5000]
x1 = m(1);
x2 = m(3);
y1 = m(2);
y2 = m(4);
x = [x1 x2 x2 x1 x1];
y = [y1 y1 y2 y2 y1];
plot(x, y, 'r', 'LineWidth', 3);
grid on;
patch(x, y, 'g');
to the code I gave you above to fill your rectangle with green color.
Ramesh Bala
el 26 de Jul. de 2018
also could you say how can one draw an ellipse with the major axis coords without knowing the minor axis values?
Image Analyst
el 26 de Jul. de 2018
Not sure what this means exactly. Try reading the FAQ: https://matlab.wikia.com/wiki/FAQ#How_do_I_create_an_ellipse.3F
Categorías
Más información sobre Surface and Mesh Plots en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!