How to select, or filter, the external border/boundary in a set of (x,y)-points? [Part 2]
9 次查看(过去 30 天)
显示 更早的评论
Hi, in a previous question, I asked about a method to obtain the external border from a set of points.
% Obtain coordinates corresponding to outside border by performing
% exclusive OR on the data, between "border b1" and "border b2":
z = setxor(b1,b2,'rows','stable');
to a larger dataset, that composes numerous "borders". The larger dataset is the following (here attached as well):
% load borders and plot them
load('borders2.mat','a')
figure; hold on
for i = 1 : length(a)
line(a{i}(:,1),a{i}(:,2),'color','k');
end
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1431593/image.png)
When I tried to use iteratively the method proposed by @Dyuman Joshi, I got a wrong output (and stuck) and I do not know how to solve the issue.. any idea or suggestion?
% remove iteratively "common edges", and plot the external common border
figure; hold on
b=a{1};
for i = 2 : length(a)
c=a{i};
b=setxor(b,c,'rows','stable');
plot(b(:,1),b(:,2),'r')
end
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1431598/image.png)
Note: as alternative method, I also tried to use the boundary function, with the maximum shrinking factor, but, still, it does not follow the exact shape of the external common border (as I would like).
2 个评论
Dyuman Joshi
2023-7-12
The points that still appear on the inside (of outer boundary) are the points of intersection of 3 or more borders, which makes sense.
One approach would be to eliminate the intersection points (as mentioned below) before processing the data via for loop, but that seems difficult to implement.
I'll keep you updated if I find a solution.
load('borders2.mat','a')
figure
hold on
for i = 1 : length(a)
line(a{i}(:,1),a{i}(:,2),'color','k');
end
%a = cellfun(@(x) unique(x,'rows', 'stable'), a, 'uni', 0);
b=a{1};
for i = 2:numel(a)
c=a{i};
b=setxor(b,c,'rows','stable');
end
plot(b(:,1),b(:,2),'ro ','MarkerSize',5)
采纳的回答
Bruno Luong
2023-7-12
编辑:Bruno Luong
2023-7-12
load('borders2.mat')
n = length(a);
warning('off','MATLAB:polyshape:repairedBySimplify')
Q = polyshape(zeros(0,2));
for k=1:n
Q = union(polyshape(a{k}),Q);
end
plot(Q)
9 个评论
Bruno Luong
2023-7-12
IMHO it requires quite a bit of programming and reflexion. Your attemps seem too quick to be effective.
更多回答(1 个)
Kanishk Singhal
2023-7-11
If you plot after the loop,
figure; hold on
b=a{1};
for i = 2 : length(a)
c=a{i};
b=setxor(b,c,'rows','stable');
end
plot(b(:,1),b(:,2), '.')
hold off
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1431648/image.png)
you will get a plot like this.
Most boundary is removed but this the data is not perfect, some points which are inside are must be shared at unique to a cell.
Here you have two possibilities,
- Either clean the data remove these points or add these points in another cell.
- You get all the points from b, plot the points but make some kind of distance function which can identify these points.
In my opinion cleaning the data is a better option.
2 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Polygons 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!