1

I have a sequence of points which are distributed in 2D space. They represent a shape but they are not ordered. So, I can plot them as points to give an idea of the shape, but if I plot the line connecting them, I miss the shape because the order of points is not the right order of connection.

I'm wondering, how can I put them in the right order such that, if I connect them one by one in sequence, I get a spline showing the shape they represent? I found and tried the convex hull in Matlab but with no results. The shape could be complex, for example a star and with convex hull I get a shape that is too much simplified (many points are not taken into account). Thanks for help!

EDIT Could be everything the image. I've randomly created one to show you a possible case, with some parts that are coming into the shape, and also points can have different distances.

I've tried with convex hull function in Matlab, that's what I get. Every time the contour have a "sharp corner", I miss it and the final shape is not what I'm looking for. Also, Matlab function has no parameter to set to change convex hull result (at least I can't see anything in the help).

hull = convhull(coords(:,1),coords(:,2)); plot(coords(hull,1),coords(hull,2),'.r');

enter image description here

Emanuele
  • 147
  • 1
  • 2
  • 13
  • 3
    How do you define the ordering of your points? – Reblochon Masque Sep 12 '17 at 09:30
  • It would be useful if you show your points. – hello123 Sep 12 '17 at 09:33
  • Concerning the convex hull, you should be able to specify a parameter how tight you want it, but you should definitely show a [mcve]. – m7913d Sep 12 '17 at 09:34
  • Can you post the x,y coordinates for your example so we can try some approaches on your sample data? I would rather not wast time on extracting the point positions, too. Also, post 1 or 2 of the shapes where a convex hull approach fails (in your opinion). – Paul Brodersen Sep 12 '17 at 10:45
  • @ReblochonMasque There is no rule behind and that's also something I'm looking for. The only thing I know is that I have these points in X,Y plane and they represent a polygon (more or less). I want to connect them one by one in sequence. I child would be able to do it, but how is it possible with an algorithm? – Emanuele Sep 12 '17 at 10:47
  • 1
    My current base idea is: start with an extremum. Add that point point to your list of explored points. Go to the nearest unexplored point. Repeat until all points are explored. – Paul Brodersen Sep 12 '17 at 10:50
  • @Paul I was just writing that in a comment. It will work if the shapes are roughly the same size in any axis (like the one in the example) and if there are no small angles between any neighbor 3 points. Its has some very particular criteria but it does seem to me like a good approach if all the shapes are like this. – armatita Sep 12 '17 at 10:53
  • @Paul That would work probably most of the time but not always. I could have a situation where the correct next point is not the nearest one but a different one .. and in that case I would not attach the correct point to the output series. – Emanuele Sep 12 '17 at 11:10
  • You can still use nearest points, you'll just have to add more criteria such as no lines can ever cross and if two points are equally close within a tolerance try both paths. The ultimate problem here is that you do not have an objective quality function. Heuristics is deciding if the shape is good or not. I can think of multiple possible shapes from the example you gave. Any of those could be correct depending on circumstances. You should know that problems similar to this are common in the fields of image processing but they either rely on specific rules or training sets. – armatita Sep 12 '17 at 11:42
  • Look at `alpha shapes` – MBo Sep 13 '17 at 05:12

2 Answers2

1

You need to somehow order your points, so they can be in a sequence; in the case of your drawing example, the points can likely be ordered using the minimal distance, to the next -not yet used- point, starting at one end (you'll probably have to provide the end).

Then you can draw a spline, maybe using Chaikin's algorithm for curves that will locally approximate a bezier curve.

You need to start working on this, and post another question with your code, if you are having difficulties.

Reblochon Masque
  • 35,405
  • 10
  • 55
  • 80
0

Alpha shapes may perform better than convexhulls for this problem. Alpha shapes will touch all the points in the exterior of a point cloud, even can carve out holes.

But for complicated shape reconstruction, I would recommend you to try a beta-skeleton bsed approach discussed in https://people.eecs.berkeley.edu/~jrs/meshpapers/AmentaBernEppstein.pdf

See more details on β-Skeleton at https://en.wikipedia.org/wiki/Beta_skeleton

Quote from the linked article:

The circle-based β-skeleton may be used in image analysis to reconstruct the shape of a two-dimensional object, given a set of sample points on the boundary of the object (a computational form of the connect the dots puzzle where the sequence in which the dots are to be connected must be deduced by an algorithm rather than being given as part of the puzzle).

it is possible to prove that the choice β = 1.7 will correctly reconstruct the entire boundary of any smooth surface, and not generate any edges that do not belong to the boundary, as long as the samples are generated sufficiently densely relative to the local curvature of the surface

Cheers

Community
  • 1
  • 1
hkrish
  • 1,259
  • 1
  • 10
  • 11
  • PS: The CGAL page for alpha skeleton has this /delicious/ definition "Imagine a huge mass of ice-cream ... containing the points as "hard" chocolate pieces. Using one of these sphere-formed ice-cream spoons we carve out all parts of the ice-cream block we can reach without bumping into chocolate pieces, thereby even carving out holes in the inside ... . We will eventually end up with a (not necessarily convex) object bounded by caps, arcs and points. If we now straighten all "round" faces to triangles and line segments, we have an intuitive description of what is called the α-shape of S" – hkrish Sep 19 '17 at 14:04