how to compute intersection of union for two polygons?
17 次查看(过去 30 天)
显示 更早的评论
I have two polygons (mat files attached), how to compute their IOU (Intersection of union)? here is the code I'm using, but it gives me a wrong values.
T1 = importdata('T1.mat');
T2 = importdata('T2.mat');
polygon1= [T1(:,1),T1(:,2)];
polygon2= [T2(:,1),T2(:,2)];
poly1 = polyshape(polygon1(:,1),polygon1(:,2));
%plot(poly1)
poly2 = polyshape(polygon2(:,1),polygon2(:,2));
hold on
%plot(poly2)
ply_inter = intersect(poly1, poly2);
area_inter = area(ply_inter);
% plot(ply_inter)
ply_union = union(poly1,poly2);
area_union = area(ply_union);
% plot(ply_union)
IoU = area_inter/area_union
2 个评论
Steven Lord
2023-6-18
At first glance that looks right. What value are you getting for IoU and what value do you expect to get?
采纳的回答
John D'Errico
2023-6-18
编辑:John D'Errico
2023-6-18
T1 = importdata('T1.mat');
T2 = importdata('T2.mat');
polygon1= [T1(:,1),T1(:,2)];
plot(T1(:,1),T1(:,2),'b.')
hold on
plot(T1(:,1),T1(:,2),'r.')
hold off
And that SEEMS like the two curves are pretty much identical. They overlap almost perfectly, so that we see only one set of dots. So what is the problem?
Next, I'll draw the points given, but with a line between each consecutive pair of points, AS PROVIDED, IN THE ORDER PROVIDED.
plot(T1(:,1),T1(:,2),'b-')
Ok. That tells the complete story!
Do you understand that polyshape does NOT sort the points around the perimeter?
These two polyshapes are NOT the ones that you see in the first figure. Instead, each of those polyshapes zig-zag all over the place.
If you want the two polyshape, you would need to sort the points in an order, perhaps going either clockwise, or counter-clockwise around the curve.
3 个评论
John D'Errico
2023-6-19
编辑:John D'Errico
2023-6-19
That would work perfectly. You effectively converted to polar coordinates. around the centroid of the data. Then you sorted in terms of polar angle. (Good job!) It would not matter in which direction the sort went of course, as polyshape does not care about that.
A second, more sophisticated option would be to use what is called the CRUST algorithm. It allows a set of disordered points to be untangled. That would be useful, if the polygons were not almost convex things. So if the polygons were U-shaped, CRUST would be more useful.
A third approach would be to use a traveling salesman algorithm to find the shortest path through all of the points in each set. This would work quite well of course, and would handle even failrly messy curves. But for that you would need to download a tool to perform the solve. (Or write one yourself.)
But by far easiest is exactly what you did. Again, well done. I'm always happy to see someone take an idea and fly with it, then producing the correct result.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Computational Geometry 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!