![pgon.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247689/pgon.png)
Finding Closed area in a region
7 次查看(过去 30 天)
显示 更早的评论
Hi,
Fisrt of all thanks for your helping.
As a part of my thesis, I need to find all closed area in a set of data. For example as you see in the picture there is just one closed area. (Data has been attached)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247649/image.jpeg)
I need to find JUST closed area. I tried to use polygen command, but it will connect first and last points, so will create several which causes wrong areas and wrong answers. (here says there are 3 closed area)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247657/image.jpeg)
Do you have any Idea how can i handle it?
0 个评论
回答(1 个)
Akira Agata
2019-11-11
By using polyshape, simplify and regions functions, you can obtain polyshape object for each closed area. The following is an example:
load('Data.mat');
pgonAll = polyshape(Data(:,1),Data(:,2),'Simplify',false);
pgonAll = simplify(pgonAll);
pgonEach = regions(pgonAll);
figure
plot(Data(:,1),Data(:,2))
hold on
plot(pgonEach(2))
legend({'Data','pgonEach(2)'},'FontSize',12)
![pgon.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247689/pgon.png)
2 个评论
Akira Agata
2019-11-13
Hi Armin-san,
Thank you for your response.
>Using polygon, you turned "simplify" off, then use it again with "simplify" command (Why?).
This is because, in your case, polyshape function returns warning message when turning "simplify" on. So I separated each step. (Just in case, please note that this warning message is only for "warning". The output becomes the same)
>My problem is that how to choose JUST closed areas.
OK.
Using the fact that the vertices does NOT jump suddenly for closed area(s), you can extract these polyshape(s). The following is one example. I hope this will help you somehow!
load('Data.mat');
pgonAll = polyshape(Data(:,1),Data(:,2),'Simplify',false);
pgonAll = simplify(pgonAll);
pgonEach = regions(pgonAll);
% Find polyshape(s) whose neighbouring vertices doesn't "jump"
% (e.g distance > 1)
idx = false(size(pgonEach));
for kk = 1:numel(pgonEach)
v = pgonEach(kk).Vertices;
d = vecnorm(diff(v),2,2);
if all(d < 1)
idx(kk) = true;
end
end
% Extract target polyshape(s)
pgonEach = pgonEach(idx);
% Visualize
figure
plot(Data(:,1),Data(:,2))
hold on
if ~isempty(pgonEach)
for kk = 1:numel(pgonEach)
plot(pgonEach(kk))
end
end
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Elementary Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!