1

Please allow me to start the question with a simplest task:If I have four points which are vertices of a rectangle, stored in a 4x2 matrix, how can I turn this into a rectangular window? (Please do not use any special command specific to drawing rectangles as the rectangle is raised just to represent a general class of regular geometrical object)

To make things more complicated, suppose I have a nx2 matrix, how can I connect all of the n points so that it becomes a polygon? Note the object is not necessarily convex. I think the main difficulty is that, how can R know which point should be connected with which?

The reason I am asking is that I was doing some image processing on a fish, and I managed to get the body line of the fish by finding the contour with opencv in python, and output it as a nx2 csv file. When I read the csv file into R and tried to use the SpatialPolygnos in the sp package to turn this into a polygon, some very unexpected behavior happened; there seems to be a break somewhere in the middle that the polygon got cut in half, i.e. the boundary of the polygon was not connected. Is there anyway I can fix this problem?

Thank you.

Edit: Someone kindly pointed out that this is possibly a duplicate of another question: drawing polygons in R. However the solution to that question relies on the shape being drawn is convex and hence it makes sense to order by angels; However here the shape is not necessarily convex and it will not work.

nobody
  • 815
  • 1
  • 9
  • 24
  • 1
    possible duplicate of [Plot polygon in R](http://stackoverflow.com/questions/26756615/plot-polygon-in-r) – Marius Jul 26 '15 at 23:55
  • 1
    If you add some specific details, including some example data and code that reproduces the problems you're having, this will be an OK question, until you do it's basically just a duplicate of "How do I plot a polygon in R" – Marius Jul 26 '15 at 23:57
  • Hi thanks for your comment. I have looked at the question and I think it was not enough to answer my question. As pointed out in the question the shape I am concerned is not necessarily convex and therefore it is hard to order by angles. – nobody Jul 27 '15 at 00:45
  • 2
    OK, if that's the case you really need to come up with some example data that demonstrates the weird behaviour you're getting, see [here](http://stackoverflow.com/q/5963269/1222578) for tips. – Marius Jul 27 '15 at 01:34
  • You can try taking the convex hull of the points, see the `chull` function, and pass that as the window object. This will eliminate any slivers or holes within the polygon. – Andy W Jul 27 '15 at 01:37
  • Sounds like an ordering problem. If your points are random and in a random order, the edges you define will go back and forth and probably have lots of crossings. – Gregor Thomas Jul 27 '15 at 01:40
  • @Marius Thank you for your comment. As this is a research problem I think it is difficult for me to show the original data, but I will try to come up with something tomorrow to illustrate my idea better. – nobody Jul 27 '15 at 02:01
  • @AndyW Thanks for the comment. I am aware of the convex hull, but the nature of the study region is such that it cannot be convex; because there are some areas where point pattern occurs with 0 probability, and they need to be excluded from the study region – nobody Jul 27 '15 at 02:02
  • Actually could someone tell me if it is possible to write out the contour as a shape file from python and then read the shapefile into R -- hopefully the shape will be well-preserved in this way? (I am really just guessing as I have never used shape files) – nobody Jul 27 '15 at 02:06
  • I'm sure there are python libraries to export a shapefile. If you just want a non-convex shape, you can see the [alphahull](https://cran.r-project.org/web/packages/alphahull/index.html) package. It isn't clear from your description though if your polygon intentionally has holes in it though (like a donut). If that is the case then not messing up the order of the points to begin with is important. – Andy W Jul 27 '15 at 16:36

1 Answers1

1

Do you want it to be a spatstat study region (of class owin) since you have the spatstat tag on there? In that case you can just use owin(poly=x) where x is your nx2 matrix (after loading the spatstat library of course). The rows in this matrix should contain the vertices of the polygon in the order that you want them connected (that's how R knows which point to connect with which). See help(owin) for more details.

Ege Rubak
  • 4,347
  • 1
  • 10
  • 18
  • Thanks for your answer. However as I have said the polygon constructed had some very strange behavior.. that it looks like it's been cut in half; as in the edge of the polygon has a very small disconnected bits somewhere in the middle. I really need it to be connected otherwise some points will lie outside the polygon – nobody Jul 27 '15 at 00:41
  • OK. So the ordering of the points from your python routine is somewhat arbitrary? How dense are the points along the boundary -- is it always the case that you want every point connected to its two nearest neighbours? In that case you can probably devise a reasonably good solution in R. Otherwise it might be better to find a solution from the python side so you can export the points in the correct order or retain some information about connectivity between the points. – Ege Rubak Jul 27 '15 at 09:01