Using contours and inpolygon, an issue of unclosed contours

23 views (last 30 days)
Hello, I have an issue I've been working on for days. I am using ocean bathymetries, and I've created contours of depth. I have used:
[C,h] = contourfm(lat,lon,depth)
This works, and looks great, and I understand the structure of C.
The next task is to see if a series of points which have depth data are inside or above these contours, the implication being that these points are within the ocean, and not below the bottom of the ocean. I have used a series of loops, but basically:
patchstart = find(C(1,:)==pointdepth);
xv= C(1,patchstart+1:patchstart+C(2,patchstart));
yv= C(1,patchstart+1:patchstart+C(2,patchstart));
IN = inpolygon(pointx,pointy,xv,yv);
There may be some errors above, as I've simplified it.
Basically, this works just fine. The problem is that the shapes in C aren't necessarilly 'closed'. The contours (bathymetry lines) are sometimes drawn in curvy fashion from one side of the plot to the other. Thus, when INPOLYGON checks the polygon, it CLOSES that polygon in a straight line from where the shape hits each edge of the axis. Because these lines are curvy and complex, the result is a crazy polygon that is not even close to accurate (that straight line suggests that some areas that are OUTSIDE of the contour are in, and some areas that are INSIDE end up being out - think of drawing a sin-wave, then drawing a line down the middle, and shading just the 'closed' areas).
So the issue is this: Is there a way to close a polygon by wrapping around the current set of axes? I'm not even sure that would solve my problem in all cases, but it might get me closer.
The CONTOURFM function draws the contours PERFECTLY, but the shapes in C are INCORRECT when redrawn with PATCH, etc.
Thanks for the help! (first time post!)
  4 Comments
Stephen Foskey
Stephen Foskey on 16 Apr 2024 at 23:47
I know it's been 11 years, but I am having the exact same issue. I am using a similar structure as Dan, but looking at precipitation data instead of bathymetry. Basically I want the largest filled in contour on the bottom image, but I am only getting the yellow area on the top image when I use inpolygon. What would be a good way to fix this? Or is there another function I should use instead?
Stephen Foskey
Stephen Foskey about 13 hours ago
I realized that the solution to my problem is to pad the matrix with zeros and that makes the contour lines connect properly. Maybe that would help OP also.

Sign in to comment.

Answers (1)

Matt J
Matt J on 17 Apr 2024 at 1:37
Edited: Matt J on 17 Apr 2024 at 10:01
There's no way to define a unique shape based on knowledge of just the isocontour line cooordinates. This gives you the boundaries between regions of land and water, but there is no way to know whether ocean is the white region or the green region without further information.
What you should probably do is take an image processing approach and use watershed,
L=watershed(depth);
whichRegion = interp2(lat,lon, L , pointx, pointy,'nearest')

Categories

Find more on Oceanography and Hydrology in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!