필터 지우기
필터 지우기

how to compute intersection of union for two polygons?

조회 수: 15 (최근 30일)
Abb
Abb 2023년 6월 18일
편집: Abb 2023년 6월 19일
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
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?
Abb
Abb 2023년 6월 18일
편집: Abb 2023년 6월 18일
@Steven Lord it gives me 28 percent overlaping but I expect both have aroud 80 to 90 percent overlapping!

댓글을 달려면 로그인하십시오.

채택된 답변

John D'Errico
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
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.
Abb
Abb 2023년 6월 19일
편집: Abb 2023년 6월 19일
@John D'Errico thanks alot. I think this works now, but the other ways may help me later

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Surface and Mesh Plots에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by