python不使用for計算兩組、多個矩形兩兩間的iou方式
解決問題: 不使用for計算兩組、多個矩形兩兩間的iou
使用numpy廣播的方法,在python程序中并不建議使用for語句,python中的for語句耗時較多,如果使用numpy廣播的思想將會提速不少。
代碼:
def calc_iou(bbox1, bbox2): if not isinstance(bbox1, np.ndarray): bbox1 = np.array(bbox1) if not isinstance(bbox2, np.ndarray): bbox2 = np.array(bbox2) xmin1, ymin1, xmax1, ymax1, = np.split(bbox1, 4, axis=-1) xmin2, ymin2, xmax2, ymax2, = np.split(bbox2, 4, axis=-1) area1 = (xmax1 - xmin1) * (ymax1 - ymin1) area2 = (xmax2 - xmin2) * (ymax2 - ymin2) ymin = np.maximum(ymin1, np.squeeze(ymin2, axis=-1)) xmin = np.maximum(xmin1, np.squeeze(xmin2, axis=-1)) ymax = np.minimum(ymax1, np.squeeze(ymax2, axis=-1)) xmax = np.minimum(xmax1, np.squeeze(xmax2, axis=-1)) h = np.maximum(ymax - ymin, 0) w = np.maximum(xmax - xmin, 0) intersect = h * w union = area1 + np.squeeze(area2, axis=-1) - intersect return intersect / union
程序中輸入為多個矩形[xmin, ymin, xmax,ymax]格式的數(shù)組或者list,輸出為numpy格式,例:輸入的shape為(3, 4)、(5,4)則輸出為(3, 5)各個位置為boxes間相互的iou值。后面會卡一個iou的閾值,然后就可以將滿足條件的索引取出。如:
def delete_bbox(bbox1, bbox2, roi_bbox1, roi_bbox2, class1, class2, idx1, idx2, iou_value): idx = np.where(iou_value > 0.4) left_idx = idx[0] right_idx = idx[1] left = roi_bbox1[left_idx] right = roi_bbox2[right_idx] xmin1, ymin1, xmax1, ymax1, = np.split(left, 4, axis=-1) xmin2, ymin2, xmax2, ymax2, = np.split(right, 4, axis=-1) left_area = (xmax1 - xmin1) * (ymax1 - ymin1) right_area = (xmax2 - xmin2) * (ymax2 - ymin2) left_idx = left_idx[np.squeeze(left_area < right_area, axis=-1)]#小的被刪 right_idx = right_idx[np.squeeze(left_area > right_area, axis=-1)] bbox1 = np.delete(bbox1, idx1[left_idx], 0) class1 = np.delete(class1, idx1[left_idx]) bbox2 = np.delete(bbox2, idx2[right_idx], 0) class2 = np.delete(class2, idx2[right_idx]) return bbox1, bbox2, class1, class2
IOU計算原理:

ymin = np.maximum(ymin1, np.squeeze(ymin2, axis=-1)) xmin = np.maximum(xmin1, np.squeeze(xmin2, axis=-1)) ymax = np.minimum(ymax1, np.squeeze(ymax2, axis=-1)) xmax = np.minimum(xmax1, np.squeeze(xmax2, axis=-1)) h = np.maximum(ymax - ymin, 0) w = np.maximum(xmax - xmin, 0) intersect = h * w
計算矩形間min的最大值,max的最小值,如果ymax-ymin值大于0則如左圖所示,如果小于0則如右圖所示
以上這篇python不使用for計算兩組、多個矩形兩兩間的iou方式就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
- 如何通過python實(shí)現(xiàn)IOU計算代碼實(shí)例
- python實(shí)現(xiàn)交并比IOU教程
- python shapely.geometry.polygon任意兩個四邊形的IOU計算實(shí)例
- python實(shí)現(xiàn)IOU計算案例
- python:目標(biāo)檢測模型預(yù)測準(zhǔn)確度計算方式(基于IoU)
- python實(shí)現(xiàn)的Iou與Giou代碼
- 淺談Python3實(shí)現(xiàn)兩個矩形的交并比(IoU)
- python計算二維矩形IOU實(shí)例
- Python計算機(jī)視覺里的IOU計算實(shí)例
- 解析目標(biāo)檢測之IoU
相關(guān)文章
Python貪心算法Greedy Algorithm解決案例小結(jié)
這篇文章主要為大家介紹了Python貪心算法Greedy Algorithm解決案例小結(jié),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
淺談Python 多進(jìn)程默認(rèn)不能共享全局變量的問題
今天小編就為大家分享一篇淺談Python 多進(jìn)程默認(rèn)不能共享全局變量的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-01-01
python和Appium移動端多設(shè)備自動化測試框架實(shí)現(xiàn)
這篇文章主要介紹了python和Appium移動端多設(shè)備自動化測試框架實(shí)現(xiàn),基于pytest和Appium框架,支持Android和iOS功能自動化的測試框架的相關(guān)內(nèi)容,需要的小伙伴可以參考一下2022-04-04
Django 后臺獲取文件列表 InMemoryUploadedFile的例子
今天小編就為大家分享一篇Django 后臺獲取文件列表 InMemoryUploadedFile的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08

