![pgon.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/247689/pgon.png)
Finding Closed area in a region
7 views (last 30 days)
Show older comments
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 Comments
Answers (1)
Akira Agata
on 11 Nov 2019
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 Comments
Akira Agata
on 13 Nov 2019
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
See Also
Categories
Find more on Elementary Polygons 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!