Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drawing a polygon around groups of datapoints in MATLAB

I have a set of datapoints each of which belongs to a certain cluster (group). I need to draw a polygon around each of these clusters. Does anyone knows how to do it?

It doesn't matter if I use or not use the actual datapoints for drawing the polygon. I just need them to be wrapped in a polygon.

like image 250
Hossein Avatar asked May 11 '10 20:05

Hossein


People also ask

How do I plot a polygon in MATLAB?

View MATLAB Command Create and plot a polygon made up of four points, and compute its area, perimeter, and centroid coordinates. pgon = polyshape ([0 0 1 3], [0 3 3 0]); plot (pgon) A = area (pgon)

How to represent polygons in two dimensions in MATLAB?

Polygons in two dimensions are generally represented in MATLAB with two arrays, locations for the X vertices and Y vertices. There is no need to have the final points in these match the initial points; that is, when arrays as described are used in situations where they are interpreted as polygon vertices, the polygon is automatically closed.

Do you use the actual datapoints for drawing the polygon?

It doesn't matter if I use or not use the actual datapoints for drawing the polygon. I just need them to be wrapped in a polygon.

How do I display a polygon on a map?

Each element of the array is 1, which means that each boundary is the exterior boundary of its own region. Display the polygon on a map using the geoshow function, specifying 'DisplayType' as 'polygon'. Clip the polygon to the latitude and longitude limits of Isle Royale National Park using the maptrimp function.


3 Answers

Try the convhull function. It returns the indices from the points in your data set that will define the convex hull. You'll have to do this for each cluster that you plot.

For example:

x=rand(1,100); %#generate x and y data for your clusters
y=rand(1,100);
k=convhull(x,y); %#generate indices marking the outermost points

hold on
plot(x,y,'b.') %# plot your cluster points
plot(x(k),y(k),'r-') %# plots only k indices, giving the convex hull

This will give you a polygon whose indices coincide with the outliers of your clusters.

like image 67
Doresoom Avatar answered Sep 29 '22 08:09

Doresoom


I'm not sure if there's a pre-made solution for this as I'm not too familiar with MATLAB, however this sounds like you need a convex hull solution.

Hope this points you in the right direction.

like image 31
Moonshield Avatar answered Sep 29 '22 09:09

Moonshield


convhull only works if you have a convex shape (like an ellipsoid). If your data distribution has concave curves, such as a banana shape, then convhull will not work. Luckily, MATLAB has a function to handle this: alphashape

depending on the "alpha" value, you get more or less facets in the resulting polygon.

once you have the x,y coordinates of the facets, you can either plot them directly but the polygon will have flat sides, or:

instead of interpolating, you can define an x,y,z grid within which to view the data, and ask, is x,y within the alpha shape? If it is, give it a value z = 1 and if not give it a value z = 0. then simply contour the grid where z = 1.

you can also use impoly to draw the polygon manually

hobbysplines on the Matlab file exchange also allows you to smooth the edges of a polygon

like image 28
bloodrootfc Avatar answered Sep 29 '22 08:09

bloodrootfc