計算兩個多邊形的iou。可以是不同類的多邊形,如乙個矩形和乙個三角形;也可以是兩個同類的多邊形。
import cv2
import math
from skimage.draw import polygon
from skimage.feature import peak_local_max
import torch.nn.functional as f
import numpy as np
defpolygon_iou
(polygon_1, polygon_2)
:"""
計算兩個多邊形的iou
:param polygon_1: [[row1, col1], [row2, col2], ...]
:param polygon_2: 同上
:return:
"""rr1, cc1 = polygon(polygon_2[:,
0], polygon_2[:,
1]) rr2, cc2 = polygon(polygon_1[:,
0], polygon_1[:,
1])try
: r_max =
max(rr1.
max(
), rr2.
max())
+1c_max =
max(cc1.
max(
), cc2.
max())
+1except
:return
0 canvas = np.zeros(
(r_max, c_max)
) canvas[rr1, cc1]+=1
canvas[rr2, cc2]+=1
union = np.
sum(canvas >0)
if union ==0:
return
0 intersection = np.
sum(canvas ==2)
return intersection / union
if __name__ ==
'__main__'
: dx =
15# 三角形輸入為三個點座標
********_1 = np.array([[
200,
100],[
180,
180],[
220,
180]])
********_2 = np.array([[
200+ dx,
100],[
180+ dx,
180],[
220+ dx,
180]])
# [row, col]
rect_1 = np.array([[
100,
100],[
100,
200],[
200,
200],[
200,
100]])
rect_2 = np.array([[
100,
150],[
100,
250],[
200,
250],[
200,
150]])
iou = polygon_iou(rect_1, rect_2)
print
('iou='
.format
(iou)
)
計算兩個多邊形的重疊面積
include include include using namespace std const int maxn 300 const double eps 1e 6 位置標識 int dcmp double x struct point double cross point a,point b,...
兩個簡單多邊形求交 CGAL
兩個多邊形求交的實現需要幾個模組 cgal中有insect函式,但是必須要求使用cgal exact predicates exact constructions kernel 本人出於其他想法,沒有把基於exact predicates inexact constructions kernel核的...
求兩個多邊形的交面積(模板)
別人的部落格 多邊形的交,多邊形的邊一定是要按逆時針方向給出 還要判斷是凸包還是凹包,呼叫相應的函式 面積並,只要和面積減去交即可 include using namespace std const int maxn 300 const double eps 1e 8 int dcmp double...