yolov3 代码中iou 交并比理解

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue May  4 19:50:35 2021

@author: ledi
"""
import numpy as np

a=np.array([  [[ 40,  90],
               [70, 60]]
           ])

anchor_maxes = a/ 2.
anchor_mins = -anchor_maxes

b=np.array([[[50, 80]]])


box_maxes = b / 2.

box_mins = -box_maxes

#-----------------------------------------------------------#
#   计算所有真实框和先验框的交并比
#   intersect_area  [n,9]
#   box_area        [n,1]
#   anchor_area     [1,9]
#   iou             [n,9]
#-----------------------------------------------------------

#最小值中的最大值
intersect_mins = np.maximum(box_mins, anchor_mins)

#最大值中的最小值
intersect_maxes = np.minimum(box_maxes, anchor_maxes)
intersect_wh = np.maximum(intersect_maxes - intersect_mins, 0.)
#交集
intersect_area = intersect_wh[..., 0] * intersect_wh[..., 1]

box_area =b[..., 0] * b[..., 1]
anchor_area = a[..., 0] * a[..., 1]
  
#比较交集与并集的比
iou = intersect_area / (box_area + anchor_area - intersect_area)
#-----------------------------------------------------------#
#   维度是[n,] 感谢 消尽不死鸟 的提醒
#-----------------------------------------------------------#
best_anchor = np.argmax(iou, axis=-1)





#画图
#---------------------------------------------------------------------------->

import numpy as np
import cv2 as cv

H=200
W=200
img = np.zeros((H, W, 3), np.uint8) #生成一个空灰度图像
print( img.shape) # 输出:(320, 320, 3)


for p in range(a.shape[1]):
    # 矩形左上角和右下角的坐标,绘制一个绿色矩形
    ptLeftTop = (int(H/2-a[0][p][0]/2), int(W/2+a[0][p][1]/2))
    ptRightBottom = (int(H/2+a[0][p][0]/2), int(W/2-a[0][p][1]/2))
    point_color = (0, 255, 0) # BGR
    thickness = 1 
    lineType = 4
    cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)

# # 绘制一个红色矩形
# ptLeftTop = (120, 100)
# ptRightBottom = (200, 150)
# point_color = (0, 0, 255) # BGR
# thickness = 1
# lineType = 8
# cv.rectangle(img, ptLeftTop, ptRightBottom, point_color, thickness, lineType)

cv.namedWindow("AlanWang")
cv.imshow('AlanWang', img)
cv.waitKey (0) # 显示 10000 ms 即 10s 后消失
cv.destroyAllWindows()

在这里插入图片描述

posted @ 2022-08-19 22:49  luoganttcc  阅读(20)  评论(0)    收藏  举报