IOU计算代码及可视化效果

在这里插入图片描述

# _*_ coding:utf-8 _*_
# 计算iou

"""
bbox的数据结构为(xmin,ymin,xmax,ymax)--(x1,y1,x2,y2),
每个bounding box的左上角和右下角的坐标
输入:
    bbox1, bbox2: Single numpy bounding box, Shape: [4]
输出:
    iou值
"""
import numpy as np
import cv2

def iou(bbox1, bbox2):
    """
    计算两个bbox(两框的交并比)的iou值
    :param bbox1: (x1,y1,x2,y2), type: ndarray or list
    :param bbox2: (x1,y1,x2,y2), type: ndarray or list
    :return: iou, type float
    """
    if type(bbox1) or type(bbox2) != 'ndarray':
        bbox1 = np.array(bbox1)
        bbox2 = np.array(bbox2)

    assert bbox1.size == 4 and bbox2.size == 4, "bounding box coordinate size must be 4"
    xx1 = np.max((bbox1[0], bbox2[0]))
    yy1 = np.min((bbox1[1], bbox2[1]))
    xx2 = np.max((bbox1[2], bbox2[2]))
    yy2 = np.min((bbox1[3], bbox2[3]))
    bwidth = xx2 - xx1
    bheight = yy2 - yy1
    area = bwidth * bheight  # 求两个矩形框的交集
    union = (bbox1[2] - bbox1[0])*(bbox1[3] - bbox1[1]) + (bbox2[2] - bbox2[0])*(bbox2[3] - bbox2[1]) - area  # 求两个矩形框的并集
    iou = area / union

    return iou


if __name__=='__main__':
    rect1 = (461, 97, 599, 237)
    # (top, left, bottom, right)
    rect2 = (522, 127, 702, 257)
    iou_ret = round(iou(rect1, rect2), 3) # 保留3位小数
    print(iou_ret)

    # Create a black image
    img=np.zeros((720,720,3), np.uint8)
    cv2.namedWindow('iou_rectangle')
    """
    cv2.rectangle 的 pt1 和 pt2 参数分别代表矩形的左上角和右下角两个点,
    coordinates for the bounding box vertices need to be integers if they are in a tuple,
    and they need to be in the order of (left, top) and (right, bottom). 
    Or, equivalently, (xmin, ymin) and (xmax, ymax).
    """
    cv2.rectangle(img,(461, 97),(599, 237),(0,255,0),3)
    cv2.rectangle(img,(522, 127),(702, 257),(0,255,0),3)
    font  = cv2.FONT_HERSHEY_SIMPLEX
    cv2.putText(img, 'IoU is ' + str(iou_ret), (341,400), font, 1,(255,255,255),1)
    cv2.imshow('iou_rectangle', img)
    cv2.waitKey(0)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
这里给出三个问题的 Python 代码以及可方法: 1. 查看样本大小分布: ```python import matplotlib.pyplot as plt # 计算每个样本的大小 sample_sizes = [] for sample in samples: sample_sizes.append(len(sample)) # 绘制直方图 plt.hist(sample_sizes, bins=20, color='steelblue', edgecolor='k') plt.xlabel('Sample Size') plt.ylabel('Frequency') plt.title('Distribution of Sample Sizes') plt.show() ``` 2. 查看样本比例分布: ```python import matplotlib.pyplot as plt # 计算每个样本的正负样本比例 sample_ratios = [] for sample in samples: pos_num = len([bbox for bbox in sample if bbox[4] == 1]) neg_num = len(sample) - pos_num sample_ratios.append(pos_num / neg_num) # 绘制直方图 plt.hist(sample_ratios, bins=20, color='steelblue', edgecolor='k') plt.xlabel('Positive/Negative Ratio') plt.ylabel('Frequency') plt.title('Distribution of Sample Ratios') plt.show() ``` 3. 查看样本 GT 与锚框的 IoU 分布: ```python import matplotlib.pyplot as plt # 计算每个样本中 GT 和锚框的 IoU ious = [] for sample in samples: for bbox in sample: iou = calculate_iou(bbox[:4], gt_bbox) ious.append(iou) # 绘制直方图 plt.hist(ious, bins=20, color='steelblue', edgecolor='k') plt.xlabel('IoU') plt.ylabel('Frequency') plt.title('Distribution of IoUs') plt.show() ``` 其中,`samples` 为样本数据,每个样本包含多个锚框(或其他检测框),`gt_bbox` 为对应的真实边界框。`calculate_iou` 为计算两个边界框 IoU 的函数。可结果包括直方图和标题,可以根据需要进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值