# Finding Closed area in a region

23 views (last 30 days)
Armin Mashhadi on 10 Nov 2019
Commented: Akira Agata on 13 Nov 2019
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)
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)
Do you have any Idea how can i handle it?

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:
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)

Armin Mashhadi on 12 Nov 2019
Hi again.
Thank you a lot, but i don't think that workdes for me.
Using polygon, you turned "simplify" off, then use it again with "simplify" command (Why?). So you re-creat the regions and choosed the close one manually.
My problem is that how to choose JUST closed areas. not to find them all and see whitch one is closed.
I think i had not describe my problem well, sorry about that.
Akira Agata on 13 Nov 2019
Hi Armin-san,
>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!
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