python 图像iou_如何通过python实现IOU计算代码实例
Intersection over Union(IOU)是一種測量在特定數據集中檢測相應物體準確度的一個標準。IoU是一個簡單的測量標準,只要是在輸出中得出一個預測范圍(bounding boxes)的任務都可以用IoU來進行測量。
IoU分數是對象類別分割問題的標準性能度量 [1] 。 給定一組圖像,IoU測量給出了在該組圖像中存在的對象的預測區域和地面實況區域之間的相似性
計算兩個矩形的交并比,通常在檢測任務里面可以作為一個檢測指標。你的預測bbox和groundtruth之間的差異,就可以通過IOU來體現。
代碼如下
#!/usr/bin/env python
# encoding: utf-8
import numpy as np
'''
函數說明:計算兩個框的重疊面積
輸入:
rec1 第一個框xmin ymin xmax ymax
rec2 第二個框xmin ymin xmax ymax
輸出:
iouv 重疊比例 0 沒有
'''
def compute_iou(rec1, rec2):
# computing area of each rectangles
S_rec1 = (rec1[2] - rec1[0]) * (rec1[3] - rec1[1]) # H1*W1
S_rec2 = (rec2[2] - rec2[0]) * (rec2[3] - rec2[1]) # H2*W2
# computing the sum_area
sum_area = S_rec1 + S_rec2 #總面積
# find the each edge of intersect rectangle
left_line = max(rec1[0], rec2[0])
right_line = min(rec1[2], rec2[2])
top_line = max(rec1[1], rec2[1])
bottom_line = min(rec1[3], rec2[3])
# judge if there is an intersect
if left_line >= right_line or top_line >= bottom_line:
#print("沒有重合區域")
return 0
else:
#print("有重合區域")
intersect = (right_line - left_line) * (bottom_line - top_line)
iouv=(float(intersect) / float(sum_area - intersect))*1.0
return iouv
'''
函數說明:獲取兩組匹配結果
輸入:
rectA 車位
rectB 車輛
threod 重疊面積最小數值界限 默認0.6
輸出:
CarUse 一維數組保存是否占用 1 占用 0 沒有
'''
def TestCarUse(rectA,rectB,threod=0.6,debug=0):
#threod=0.8#設定最小值
ALength=len(rectA)
BLength=len(rectB)
#創建保存匹配結果的矩陣
recIOU=np.zeros((ALength,BLength),dtype=float,order='C')
#用于記錄車位能夠使否占用
CarUse=np.zeros((1,ALength),dtype=int,order='C')
for i in range(0,ALength):
for j in range(0,BLength):
iou = compute_iou(rectA[i], rectB[j])
recIOU[i][j]=format(iou,'.3f')
if iou>=threod:
CarUse[0,i]=1 #有一個超過匹配認為車位i被占用
if debug==1:
print('----匹配矩陣----')
print(recIOU)
'''
print('----車位占用情況----')
for i in range(0,ALength):
msg='車位'+str(i)+"-"+str(CarUse[0][i])
print(msg)
'''
return CarUse
if __name__=='__main__':
#A代表車位
rectA1 = (30, 10, 70, 20)
rectA2 = (70, 10, 80, 20)
rectA =[rectA1,rectA2]
#B代表檢測車輛
rectB1 = (20, 10, 35, 20)
rectB2 = (30, 15, 70, 25)
rectB3 = (70, 10, 80, 20)
rectB =[rectB1,rectB2,rectB3]
#獲取車位占用情況 rectA車位 rectB車輛 0.6占面積最小比
CarUse=TestCarUse(rectA,rectB,0.6,1)
print('----車位占用情況----')
for i in range(0,len(CarUse)+1):
msg='車位'+str(i)+"-"+str(CarUse[0][i])
print(msg)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持龍方網絡。
總結
以上是生活随笔為你收集整理的python 图像iou_如何通过python实现IOU计算代码实例的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安迪樊是否会邀请哪位明星来为企业代言?
- 下一篇: 夏至未至祭司的画是谁画的啊?