一.计算IOU

def intersect(box_a, box_b):max_xy = np.minimum(box_a[:, 2:], box_b[2:])min_xy = np.maximum(box_a[:, :2], box_b[:2])inter = np.clip((max_xy - min_xy), a_min=0, a_max=np.inf)return inter[:, 0] * inter[:, 1]def jaccard_numpy(box_a, box_b):"""Compute the jaccard overlap of two sets of boxes.  The jaccard overlapis simply the intersection over union of two boxes.E.g.:Args:box_a: Multiple bounding boxes, Shape: [num_boxes,4]box_b: Single bounding box, Shape: [4]Return:jaccard overlap: Shape: [box_a.shape[0], box_a.shape[1]]"""inter = intersect(box_a, box_b)area_a = ((box_a[:, 2]-box_a[:, 0]) *(box_a[:, 3]-box_a[:, 1]))  # [A,B]area_b = ((box_b[2]-box_b[0]) *(box_b[3]-box_b[1]))  # [A,B]union = area_a + area_b - interreturn inter / union  # [A,B]

人工修正后的每一个框与算法输出的所有框去计算IOU,取出IOU大于0.9的算法输出框

def compute_IOU(algrim_bboxs,fix_bboxs):# print('algrim_bboxs:', algrim_bboxs)# print('fix_bboxs:', fix_bboxs)for i, fix_bbox in enumerate(fix_bboxs):print('fix_bbox:', fix_bbox)fix_x1, fix_y1, fix_x2, fix_y2 = fix_bboxx1, y1, x2, y2 = algrim_bboxs[:,0], algrim_bboxs[:,1], algrim_bboxs[:,-2], algrim_bboxs[:,-1]area = (y2-y1)*(x2-x1)inter = np.maximum((np.minimum(x2,fix_x2) - np.maximum(x1,fix_x1)),0)*np.maximum((np.minimum(y2,fix_y2) - np.maximum(y1, fix_y1)),0)union = area+(fix_y2-fix_y1)*(fix_x2-fix_x1)-interIOU = inter/union# print('IOU:', IOU)index = np.where(IOU > 0.9)[0]print('algrim_bboxs[index]:', algrim_bboxs[index])

注释:下面的代码假设了第0行是IOU最大的,那么其余做NMS计算

boxes=np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
y1 = boxes[:, 0]
x1 = boxes[:, 1]
y2 = boxes[:, 2]
x2 = boxes[:, 3]
area = (y2 - y1) * (x2 - x1)
print('area=',area)
box_area=area[0]
print('box_area=',box_area)
boxes_area=area[1:]
print('boxes_area=',boxes_area)
box=boxes[0]
y1 = np.maximum(box[0], boxes[1:, 0])
y2 = np.minimum(box[2], boxes[1:, 2])
x1 = np.maximum(box[1], boxes[1:, 1])
x2 = np.minimum(box[3], boxes[1:, 3])
#注意与0判断 若不想交返回0值,即intersection为0
intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
print('intersection=',intersection)
union = box_area + boxes_area - intersection[:]
print('union=',union)
iou = intersection / union
print('iou=',iou)

二,一般加上score,这样用

scores = np.array([0.5, 0.7, 0.3, 0.2])
ixs=scores.argsort()[::-1]
print('ixs=',ixs)
boxes=np.array([[1,2,3,4],[1.2, 0.4,2.1, 0.8],[5,6,7,8],[9,10,11,12]])
y1 = boxes[:, 0]
x1 = boxes[:, 1]
y2 = boxes[:, 2]
x2 = boxes[:, 3]
area = (y2 - y1) * (x2 - x1)
print('area=',area)
box_area=area[ixs[0]]
print('box_area=',box_area)
boxes_area=area[ixs[1:]]
print('boxes_area=',boxes_area)
box=boxes[ixs[0]]
boxes=boxes[ixs[1:]]
y1 = np.maximum(box[0], boxes[:, 0])
y2 = np.minimum(box[2], boxes[:, 2])
x1 = np.maximum(box[1], boxes[:, 1])
x2 = np.minimum(box[3], boxes[:, 3])
#注意与0判断 若不想交返回0值,即intersection为0
intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
print('intersection=',intersection)
union = box_area + boxes_area - intersection[:]
print('union=',union)
iou = intersection / union
print('iou=',iou)

三,求其NMS后的框的小trick

scores=np.array([0.8,0.4,0.7,0.2,0.5])
ixs = scores.argsort()[::-1]
print('ixs=',ixs)
#iou要比score少一个数值,因为iou的计算是由score的第一个值确定的,故下面的np.where要加1
iou=np.array([0.3,0.6,0.1,0.4])
threshold=0.3
remove_ixs = np.where(iou > threshold)[0]+1
print('remove_ixs=',remove_ixs)
# Remove indices of the picked and overlapped boxes.
ixs = np.delete(ixs, remove_ixs)
print('ixs=',ixs)
ixs = np.delete(ixs, 0)
print('ixs=',ixs)

四,加入while,就把不要的框删掉

scores = np.array([0.8, 0.4, 0.7, 0.2, 0.5])
ixs = scores.argsort()[::-1]
while len(ixs) > 0:print('================================')print('ixs=', ixs)# iou要比score少一个数值,因为iou的计算是由score的第一个值确定的,故下面的np.where要加1iou = np.array([0.3, 0.6, 0.1, 0.4])threshold = 0.3remove_ixs = np.where(iou > threshold)[0] + 1print('remove_ixs=', remove_ixs)# Remove indices of the picked and overlapped boxes.ixs = np.delete(ixs, remove_ixs)print('ixs=', ixs)ixs = np.delete(ixs, 0)print('ixs=', ixs)

五,faster rcnn NMS

def py_cpu_nms(dets, thresh):"""Pure Python NMS baseline."""x1 = dets[:, 0]y1 = dets[:, 1]x2 = dets[:, 2]y2 = dets[:, 3]scores = dets[:, 4]areas = (x2 - x1 + 1) * (y2 - y1 + 1)order = scores.argsort()[::-1]print('order',order)keep = []while order.size > 0:i = order[0]keep.append(i)xx1 = np.maximum(x1[i], x1[order[1:]])yy1 = np.maximum(y1[i], y1[order[1:]])xx2 = np.minimum(x2[i], x2[order[1:]])yy2 = np.minimum(y2[i], y2[order[1:]])w = np.maximum(0.0, xx2 - xx1 + 1)h = np.maximum(0.0, yy2 - yy1 + 1)inter = w * hovr = inter / (areas[i] + areas[order[1:]] - inter)print('ovr=',ovr)print('np.where(ovr <= thresh)',np.where(ovr <= thresh))inds = np.where(ovr <= thresh)[0]print('inds=',inds)print('inds + 1',inds + 1)print('order[inds + 1]',order[inds + 1])order = order[inds + 1]print('order=',order)return keep
def nms():# ###self nmsresult = [np.array([[7.9301813e+02, 6.3052429e+02, 8.9795898e+02,             7.2513965e+02,9.9307442e-01],[8.0682990e+02, 6.4606281e+02, 8.7198071e+02, 7.1328979e+02,9.5732883e-02]], dtype=np.float32)]##faster rcnn  nmskeep=py_cpu_nms(result[0],0.7)print('keep=',keep)for i in keep:print('i=',i)print(result[0][i])

此处大于0.7IOU的框就抑制, 小于0.7的就留下.

六.多边形利用polygon计算IOU

def compute_IOU_():import numpy as npimport shapelyfrom shapely.geometry import Polygon, MultiPoint  # 多边形path = './134.jpg'img = cv2.imread(path)print('img.shape:', img.shape)line1 = [728, 252, 908, 215, 934, 312, 752, 355]  # 四边形四个点坐标的一维数组表示,[x,y,x,y....]line2 = [741, 262, 907, 228, 923, 308, 758, 342]# #debug to show# line1 = np.array(line1).reshape(4, 2)# line2 = np.array(line2).reshape(4, 2)# cv2.polylines(img, [np.array(line1).reshape(-1, 1, 2)], True, (0, 255, 0), thickness=2)# cv2.polylines(img, [np.array(line2).reshape(-1, 1, 2)], True, (0, 0, 255), thickness=2)# cv2.imwrite('134_with_rect.jpg',img)line1_box = np.array(line1).reshape(4, 2)  # 四边形二维坐标表示# 凸多边形# poly1 = Polygon(line1_box).convex_hull#凸多边形与凹多边形poly1 = Polygon(line1_box)#.convex_hull  # python四边形对象,会自动计算四个点,最后四个点顺序为:左上 左下  右下 右上 左上print(Polygon(line1_box).convex_hull)line2_box = np.array(line2).reshape(4, 2)# 凸多边形# poly2 = Polygon(line2_box).convex_hull# 凸多边形与凹多边形poly2 = Polygon(line2_box)print(Polygon(line2_box).convex_hull)union_poly = np.concatenate((line1_box, line2_box))  # 合并两个box坐标,变为8*2# print(union_poly)print(MultiPoint(union_poly).convex_hull)  # 包含两四边形最小的多边形点if not poly1.intersects(poly2):  # 如果两四边形不相交iou = 0else:try:inter_area = poly1.intersection(poly2).area  # 相交面积print(inter_area)union_area = MultiPoint(union_poly).convex_hull.areaprint(union_area)if union_area == 0:iou = 0# iou = float(inter_area) / (union_area-inter_area)  #错了iou = float(inter_area) / union_areaexcept shapely.geos.TopologicalError:print('shapely.geos.TopologicalError occured, iou set to 0')iou = 0print('line1_box:', line1_box)print('iou:', iou)

正常矩形计算IOU与与NMS,多边形计算IOU相关推荐

  1. 目标检测:NMS和计算mAP时的置信度阈值和IoU阈值

    在目标检测问题中,好几处地方使用了阈值这个限制指标,主要有:1. NMS操作之前用到的置信度阈值a:2. NMS进行时用到的IoU阈值b:3.计算某类别AP时,统计TP,FP个数前,用到置信度阈值c: ...

  2. 【OpenCV3】几何图形(直线、矩形、圆、椭圆、多边形等)绘制

    在图像处理的过程中,我们有时需要在图像或者视频上画上一些图案或者绘上一些文字.OpenCV中提供了各种功能的绘图函数,使用这些函数,我们可以在图像上绘制直线.矩形.圆.椭圆.多边形等等. 1.cv:: ...

  3. OpenCV与图像处理学习三——线段、矩形、圆、椭圆、多边形的绘制以及文字的添加

    OpenCV与图像处理学习三--线段.矩形.圆.椭圆.多边形的绘制以及文字的添加 一.OpenCV中的绘图函数 1.1 线段绘制 1.2 矩形绘制 1.3 圆绘制 1.4 椭圆的绘制 1.5 多边形绘 ...

  4. 14、yolov5-6中数据预处理、模型输出nms单独计算、onnxruntime的gpu版本前向推理

    1.数据输入预处理.模型输出nms等 import os import torch import time import math import cv2 import numpy as np impo ...

  5. mtensor一个tensor计算库,支持cuda延迟计算

    1 mtensor mtensor是一个tensor计算库, 支持cuda的延迟计算, 项目地址为https://github.com/matazure/mtensor. 2 背景 延迟计算具有避免额 ...

  6. 计算机word表格计算教程F9,Word表格数据计算与域操作

    摘 要:Word是常用的文档编辑软件,用户在平时的工作中,利用它可以进行文字.图片.表格的排版处理等工作,但往往忽略WORD也具有强大的计算功能:Word的表格计算功能在表格项的定义方式.公式的定义方 ...

  7. group convolution (分组卷积)的计算量详解、卷积计算量特征图大小,池化特征图大小、深度通道deep-wise 卷积

    group convolution (分组卷积)的计算量详解.卷积计算量特征图大小,池化特征图大小.深度通道deep-wise 卷积 提示:最近忙着各种提前批的笔试面试,所以没太多空刷题了都,先复盘一 ...

  8. 给定经纬度计算距离_通过经纬度坐标计算距离的方法(经纬度距离计算)ZZ

    通过经纬度坐标计算距离的方法(经纬度距离计算) 最近在网上搜索"通过经纬度坐标计算距离的方法",发现网上大部分都是如下的代码: #define PI 3.14159265 stat ...

  9. 目标检测结果IOU不同取值的含义 IoU=0.50与IoU=0.50:0.95

    Average Precision (AP)和Average Recall (AR) AP是单个类别平均精确度,而mAP是所有类别的平均精确度. AP是Precision-Recall Curve曲线 ...

  10. R语言偏相关或者部分相关性系数计算实战:使用psych包计算(Partial Correlation)偏相关系数、拟合回归模型使用两个回归模型的残差计算偏相关性系数

    R语言偏相关或者部分相关性系数计算实战:使用psych包计算(Partial Correlation)偏相关系数.拟合回归模型使用两个回归模型的残差计算偏相关性系数 目录

最新文章

  1. Ts + React + Mobx 实现移动端浏览器控制台
  2. Linux内存映射--mmap函数
  3. java 短链跳转原理_给你代码:短链接生成原理
  4. Spring模板对象之RedisTemplate(Spring整合jedis)
  5. 理解进程调度时机跟踪分析进程调度与进程切换的过程
  6. 特斯拉自动驾驶使用的技术_使用自回归预测特斯拉股价
  7. html5不支持的属性,HTML5 常用语法一览(列举不支持的属性)
  8. html表格td的内容修改,点击table中的td,修改td中的内容功能实现
  9. html5星期,HTML5 时钟
  10. 下载与eclipse匹配的hibernate tools
  11. L1-008 求整数段和 (10 分)—团体程序设计天梯赛
  12. word参考文献后面的附录在目录显示不出来如何解决
  13. 什么是双线服务器?只是双线路接入?
  14. CRM系统源码PHP开发
  15. 《腾讯传》四、从寄生虫到蜕变上市—企鹅的成人礼
  16. tif构建金字塔失败arcgis_ArcGIS影像构建金字塔小窍门
  17. 【Linux 编程】线程绑定 CPU
  18. Flutter 3更新详解
  19. 树莓派内网穿透及其实现监控的相关整理
  20. Python spider爬取高清电影

热门文章

  1. NeurIPS 2020 | Glance and Focus: 通用、高效的神经网络自适应推理框架
  2. Spring Cloud源码分析(二)Ribbon
  3. 论文浅尝 | KGQR: 用于交互式推荐的知识图谱增强Q-learning框架
  4. 去月球“你知道戴维会变身成哪种动物吗?”
  5. 错误:子进程 已安装 pre-removal 脚本 返回了错误号 1
  6. FastLeaderElection
  7. Windows下MYSQL的安装与配置
  8. Fibonacci(求前四位数)
  9. UVALive 7455 Linear Ecosystem (高斯消元)
  10. LSP(分层服务提供程序)