自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

目標(biāo)檢測算法中規(guī)則矩形和不規(guī)則四邊形IOU的Python實(shí)現(xiàn)

開發(fā) 前端 算法
交并比(Intersection-over-Union,IoU),目標(biāo)檢測中使用的一個(gè)概念,我們在進(jìn)行目標(biāo)檢測算法測試時(shí),重要的指標(biāo),是產(chǎn)生的預(yù)測框(candidate bound)與標(biāo)記框(ground truth bound)的交疊率,即它們的交集與并集的比值。最理想情況是完全重疊,即比值為1。

目標(biāo)檢測算法中規(guī)則矩形和不規(guī)則四邊形IOU的Python實(shí)現(xiàn)

交并比(Intersection-over-Union,IoU),目標(biāo)檢測中使用的一個(gè)概念,我們在進(jìn)行目標(biāo)檢測算法測試時(shí),重要的指標(biāo),是產(chǎn)生的預(yù)測框(candidate bound)與標(biāo)記框(ground truth bound)的交疊率,即它們的交集與并集的比值。最理想情況是完全重疊,即比值為1。

通常,我們所說的目標(biāo)檢測檢測的框是規(guī)則的矩形框,計(jì)算IOU也非常簡單,一般兩種方法:

  • 兩個(gè)矩形的寬之和減去組合后的矩形的寬就是重疊矩形的寬,同比重疊矩形的高。
  • 右下角的最小值減去左上角的最大值就是重疊矩形的寬,同比高。

上述規(guī)則四邊形(矩形)IOU計(jì)算方式一的 Python實(shí)現(xiàn)

 

  1. def calculate_regular_iou(rec1, rec2): 
  2.     ""
  3.     computing IoU 
  4.     :param rec1: (y0, x0, y1, x1), which reflects 
  5.             (topleft, bottom, right
  6.     :param rec2: (y0, x0, y1, x1) 
  7.     :return: scala value of IoU 
  8.     ""
  9.  
  10.     S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1]) 
  11.     S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) 
  12.  
  13.     sum_area = S_rec1 + S_rec2 
  14. ​ 
  15.     left_line = max(rec1[1], rec2[1]) 
  16.     right_line = min(rec1[3], rec2[3]) 
  17.     top_line = max(rec1[0], rec2[0]) 
  18.     bottom_line = min(rec1[2], rec2[2]) 
  19. ​ 
  20.     if left_line >= right_line or top_line >= bottom_line: 
  21.         return 0 
  22.     else
  23.         intersect = (right_line - left_line) * (bottom_line - top_line) 
  24.         return (intersect / (sum_area - intersect)) * 1.0 
  25.      
  26. if __name__ == '__main__'
  27.     # (topleft, bottom, right
  28.     rect1 = [551, 26, 657, 45] 
  29.     rect2 = [552, 27, 672, 46] 
  30.     iou = calculate_regular_iou(rect1, rect2) 

上述規(guī)則四邊形(矩形)IOU計(jì)算方式二的 Python 實(shí)現(xiàn)

 

  1. def compute_regular_iou_other(rec1, rec2): 
  2.     ""
  3.     computing IoU 
  4.     :param rec1: (y0, x0, y1, x1), which reflects 
  5.             (topleft, bottom, right
  6.     :param rec2: (y0, x0, y1, x1) 
  7.     :return: scala value of IoU 
  8.     ""
  9.     areas1 = (rec1[3] - rec1[1]) * (rec1[2] - rec1[0]) 
  10.     areas2 = (rec2[3] - rec2[1]) * (rec2[2] - rec2[0]) 
  11. ​ 
  12.     left = max(rec1[1],rec2[1]) 
  13. ​ 
  14.     right = min(rec1[3],rec2[3]) 
  15. ​ 
  16.     top = max(rec1[0], rec2[0]) 
  17. ​ 
  18.     bottom = min(rec1[2], rec2[2]) 
  19. ​ 
  20.     w = max(0, right - left
  21.     h = max(0, bottom - top
  22. ​ 
  23.     return w*h / (areas2 + areas1 - w*h) 
  24. ​ 
  25. if __name__ == '__main__'
  26.     # (topleft, bottom, right
  27.     rect1 = [551, 26, 657, 45] 
  28.     rect2 = [552, 27, 672, 46] 
  29.     iou = compute_regular_iou_other(rect1, rect2) 

但是,對于不規(guī)則四邊形就不能通過上述這兩種方式來計(jì)算,這里可以使用Python的 Shapely 庫實(shí)現(xiàn),Python 實(shí)現(xiàn)如下:

 

  1. import numpy as np 
  2. import shapely 
  3. from shapely.errors import TopologicalError 
  4. from shapely.geometry import Polygon,MultiPoint 
  5. ​ 
  6. def to_polygon(quadrilateral): 
  7.     ""
  8. ​ 
  9.     :param quadrilateral: 四邊形四個(gè)點(diǎn)坐標(biāo)的一維數(shù)組表示,[x,y,x,y....] 
  10.     :return: 四邊形二維數(shù)組, Polygon四邊形對象 
  11.     ""
  12.     # 四邊形二維數(shù)組表示 
  13.     quadrilateral_array = np.array(quadrilateral).reshape(4, 2) 
  14.     # Polygon四邊形對象,會自動計(jì)算四個(gè)點(diǎn),最后四個(gè)點(diǎn)順序?yàn)椋鹤笊?nbsp;左下  右下 右上 左上 
  15.     quadrilateral_polygon = Polygon(quadrilateral_array).convex_hull 
  16. ​ 
  17.     return quadrilateral_array, quadrilateral_polygon 
  18. ​ 
  19. def calculate_iou(actual_quadrilateral, predict_quadrilateral): 
  20.     ""
  21. ​ 
  22.     :param actual_quadrilateral: 預(yù)測四邊形四個(gè)點(diǎn)坐標(biāo)的一維數(shù)組表示,[x,y,x,y....] 
  23.     :param predict_quadrilateral: 期望四邊形四個(gè)點(diǎn)坐標(biāo)的一維數(shù)組表示,[x,y,x,y....] 
  24.     :return
  25.     ""
  26.     # 預(yù)測四邊形二維數(shù)組, 預(yù)測四邊形 Polygon 對象 
  27.     actual_quadrilateral_array, actual_quadrilateral_polygon = to_polygon(actual_quadrilateral) 
  28.     # 期望四邊形二維數(shù)組, 期望四邊形 Polygon 對象 
  29.     predict_quadrilateral_array, predict_quadrilateral_polygon = to_polygon(predict_quadrilateral) 
  30. ​ 
  31.     # 合并兩個(gè)box坐標(biāo),變?yōu)?*2 便于后面計(jì)算并集面積 
  32.     union_poly = np.concatenate((actual_quadrilateral_array, predict_quadrilateral_array)) 
  33.     # 兩兩四邊形是否存在交集 
  34.     inter_status = actual_quadrilateral_polygon.intersects(predict_quadrilateral_polygon) 
  35.     # 如果兩四邊形相交,則進(jìn)iou計(jì)算 
  36.     if inter_status: 
  37.         try: 
  38.             # 交集面積 
  39.             inter_area = actual_quadrilateral_polygon.intersection(predict_quadrilateral_polygon).area 
  40.             # 并集面積 計(jì)算方式一 
  41.             #union_area = poly1.area + poly2.area - inter_area 
  42.             # 并集面積 計(jì)算方式二 
  43.             union_area = MultiPoint(union_poly).convex_hull.area 
  44.             # 若并集面積等于0,則iou = 0 
  45.             if union_area == 0: 
  46.                 iou = 0 
  47.             else
  48.                 # 第一種計(jì)算的是: 交集部分/包含兩個(gè)四邊形最小多邊形的面積 
  49.                 iou = float(inter_area) / union_area 
  50.                 #  第二種: 交集 / 并集(常見矩形框IOU計(jì)算方式) 
  51.                 # iou=float(inter_area) /(poly1.area+poly2.area-inter_area) 
  52.         except shapely.errors.TopologicalError : 
  53.             print('shapely.errors.TopologicalError occured, iou set to 0'
  54.             iou = 0 
  55.     else
  56.         iou = 0 
  57. ​ 
  58.     return iou 
  59. ​ 
  60. if __name__ == '__main__'
  61.     actual_quadrilateral = [908, 215, 934, 312, 752, 355, 728, 252] 
  62.     predict_quadrilateral =  [923, 308, 758, 342, 741, 262, 907, 228] 
  63.     iou = calculate_iou(actual_quadrilateral, predict_quadrilateral) 
  64.     print(iou) 

避坑指南

運(yùn)行代碼拋出 WinError 126 錯誤

在使用Python中的使用 import shapely 時(shí)不會報(bào)錯,但是在使用 from shapely.geometry import Polygon,MultiPoint 會報(bào)錯,報(bào)錯的詳細(xì)信息如下圖:

 

目標(biāo)檢測算法中規(guī)則矩形和不規(guī)則四邊形IOU的Python實(shí)現(xiàn)

報(bào)錯的主要原因就出現(xiàn)在 geos_c.dll 這里,看了網(wǎng)上很多文章大部分說是由于 geos_c.dll 文件缺失導(dǎo)致報(bào)錯。嘗試在網(wǎng)上找了幾個(gè) geos_c.dll 文件放到 C:\Windows\System32 下仍然沒有解決問題。

 

目標(biāo)檢測算法中規(guī)則矩形和不規(guī)則四邊形IOU的Python實(shí)現(xiàn)

最終解決方案:通過 pip uninstall Shapely 卸載原來安裝的 Shapely 然后 在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely,如上圖,這里下載對應(yīng)版本的whl文件安裝,安裝這個(gè)whl 就可以解決該問題。

whl文件下載404錯誤

在 https://www.lfd.uci.edu/~gohlke/pythonlibs/#shapely 下載制定版本的whl時(shí),出現(xiàn)404錯誤。如下。

 

目標(biāo)檢測算法中規(guī)則矩形和不規(guī)則四邊形IOU的Python實(shí)現(xiàn)

此時(shí)改用 chrome 瀏覽器重新嘗試下載,即可解決。

責(zé)任編輯:未麗燕 來源: 今日頭條
相關(guān)推薦

2012-12-24 09:55:15

iOSUnity3D

2012-08-13 10:53:52

Web

2022-12-28 07:48:40

六邊形動畫CSS

2023-03-13 13:35:00

幾何算法矩形碰撞檢測

2015-07-17 13:31:20

按鈕單獨(dú)控制

2011-06-20 17:06:56

Qt Widget

2009-07-10 11:31:45

Swing支持透明和不規(guī)則窗口

2017-09-20 16:25:00

深度學(xué)習(xí)視覺領(lǐng)域計(jì)算機(jī)

2009-12-25 10:20:28

WPF窗口

2009-07-15 10:40:06

碰撞檢測算法Java ME

2020-10-18 07:15:53

Python異常檢測算法開發(fā)

2024-04-26 10:00:03

自動駕駛模型

2010-09-08 17:20:42

CSS

2018-01-23 16:16:03

開源技術(shù) Facebook

2023-11-13 22:17:54

YOLO-NAS目標(biāo)檢測

2021-08-29 18:32:18

CSS

2024-04-28 11:42:39

Python模型數(shù)據(jù)

2017-03-02 10:49:37

推薦算法原理實(shí)現(xiàn)

2015-02-02 16:21:26

android瀑布流圖片加載

2010-08-31 09:46:23

C#
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號