How do I convert multiple discontinuous lines into a single polyshape?

For the image attached, I have the coordinates of the points along each individual line, with these lines having been extracted from a contour map. The blue lines show the low amplitude boundaries, while the yellow lines indicate the direction of increasing amplitude. From these line coordinates I am hoping to create polyshapes.
The issue I'm having is that because some of the lines are discontinuous, going beyond the axes limits, when I fill them in they only fill the region bound by that line, not considering the other lines around them. As such, I was wondering if there was an easy way to have Matlab automatically join up the lines that should contribute to the same region (i.e. the 2 seen in the top left or the 3 at the bottom of the figure below) so that I can get polyshapes of the regions I'm interested in?
Thank you in advance for any help you can offer!

3 件のコメント

Star Strider
Star Strider 2022 年 10 月 14 日
It would help to have the data and the necesary code, including the contour call and the code that extracted the lines. Are the two roughly parallel lines at the lower end supposed to have the area between them filled, instead of the way they’re plotted here?
Also, is it necessary that they all be polyshapes, or do you just want to fill in the areas?
Fraser Douglas
Fraser Douglas 2022 年 10 月 14 日
編集済み: Fraser Douglas 2022 年 10 月 14 日
I have attached the data and code to my original post, hopefully you are able to download it okay!
The lines at the bottom are meant to have the area between them filled, but it isn't entirely necessary to make them into polyshapes, although I was wanting to do this so that I can easily track their centroids. The image is simply one of a long timeseries of images where the shaded regions are expected to move and grow.
I may be going about this in a rather convoluted way though, if there is a way to go directly from the contour plot to polyshapes or similar this would be a better solution.
Matt J
Matt J 2022 年 10 月 14 日
it isn't entirely necessary to make them into polyshapes, although I was wanting to do this so that I can easily track their centroids.
You could do that easily enough with regionprops, e.g.,
regionprops(im>0.41,'Centroid')

サインインしてコメントする。

 採用された回答

Matt J
Matt J 2022 年 10 月 14 日
編集済み: Matt J 2022 年 10 月 14 日
Using this FEX download,
load im.mat
imp=padarray(im,[1,1]);
[m,n]=size(im);
levels=[0.4,0.41];
clear p
for i=1:numel(levels)
M=contourc(imp,[levels(i),levels(i)]);
[~,XY]=getContourLineCoordinates(M);
X=cellfun(@(z) z(:,1)-1, XY,'uni',0);
Y=cellfun(@(z) z(:,2)-1, XY,'uni',0);
p(i)=polyshape(X,Y);
end
Warning: Polyshape has duplicate vertices, intersections, or other inconsistencies that may produce inaccurate or unexpected results. Input data has been modified to create a well-defined polyshape.
plot(p);axis equal;
set(gca,'YDir','reverse');
axis([1,m,1,n]);

1 件のコメント

Fraser Douglas
Fraser Douglas 2022 年 10 月 14 日
That has done just the trick! Thank you so much for your help, hope you have a great day ahead of you :)

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeElementary Polygons についてさらに検索

製品

リリース

R2022b

質問済み:

2022 年 10 月 14 日

コメント済み:

2022 年 10 月 14 日

Community Treasure Hunt

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

Start Hunting!

Translated by