1 旋转IOU

"""
2019.7.4 计算旋转的iou
"""
#coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport os
import numpy as np
import cv2
import tensorflow as tf
import time
import torchdef rbbx_overlaps(boxes1, boxes2, gpu_id):pass"""
计算旋转面积
boxes1,boxes2格式为x,y,w,h,theta
"""
def iou_rotate_calculate(boxes1, boxes2, use_gpu=False, gpu_id=1):print("####boxes2:", boxes1.shape)print("####boxes2:", boxes2.shape)os._exit()# start = time.time()if use_gpu:print("暂时用不了")ious = rbbx_overlaps(boxes1, boxes2, gpu_id)else:area1 = boxes1[0] * boxes1[1]area2 = boxes2[2] * boxes2[3]r1 = ((boxes2[0], boxes2[1]), (boxes1[0], boxes1[1]), boxes1[2])r2 = ((boxes2[0], boxes2[1]), (boxes2[2], boxes2[3]), boxes2[4])int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]if int_pts is not None:order_pts = cv2.convexHull(int_pts, returnPoints=True)int_area = cv2.contourArea(order_pts)# 计算出iouious = int_area * 1.0 / (area1 + area2 - int_area)else:ious=0return ious"""
计算旋转面积
boxes1,boxes2格式为x,y,w,h,theta
输出为张量格式
"""
def iou_rotate_calculate1(boxes1, boxes2, use_gpu=False, gpu_id=1):#将gpu的tensor转成cpu的boxes1=boxes1.cpu()boxes2 = boxes2.cpu()# 将tensor转成numpy的boxes1 = boxes1.numpy()boxes2 = boxes2.numpy()ious_total=[]# start = time.time()if use_gpu:print("@@@@暂时用不了")ious = rbbx_overlaps(boxes1, boxes2, gpu_id)else:for num in range(0,len(boxes2)):area1 = boxes1[0] * boxes1[1]area2 = boxes2[num,2] * boxes2[num,3]r1 = ((boxes2[num,0], boxes2[num,1]), (boxes1[0], boxes1[1]), boxes1[2])r2 = ((boxes2[num,0], boxes2[num,1]), (boxes2[num,2], boxes2[num,3]), boxes2[num,4])int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]if int_pts is not None:order_pts = cv2.convexHull(int_pts, returnPoints=True)int_area = cv2.contourArea(order_pts)# 计算出iouious = int_area * 1.0 / (area1 + area2 - int_area)ious_total.append(ious)else:ious = 0ious_total.append(ious)# numpy转为CPU tensortotal_ious = torch.from_numpy(np.array(ious_total))total_ious = total_ious.type(torch.FloatTensor)  # 转Float# CPU tensor转GPU tensorious = total_ious.cuda()return ious"""
计算旋转面积
boxes1,boxes2格式为x,y,w,h,theta
输出为张量格式
"""
def bbox_iou_rotate_calculate1(boxes1, boxes2, use_gpu=False, gpu_id=1):#将gpu的tensor转成cpu的boxes1=boxes1.cpu()boxes2 = boxes2.cpu()# 将tensor转成numpy的boxes1 = boxes1.numpy()boxes2 = boxes2.numpy()ious_total=[]# start = time.time()if use_gpu:print("暂时用不了")ious = rbbx_overlaps(boxes1, boxes2, gpu_id)else:for num in range(0,len(boxes2)):area1 = boxes1[num,2] * boxes1[num,3]area2 = boxes2[num,2] * boxes2[num,3]r1 = ((boxes1[num,0], boxes1[num,1]), (boxes1[num,2], boxes1[num,3]), boxes1[num,4])r2 = ((boxes2[num,0], boxes2[num,1]), (boxes2[num,2], boxes2[num,3]), boxes2[num,4])int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]if int_pts is not None:order_pts = cv2.convexHull(int_pts, returnPoints=True)int_area = cv2.contourArea(order_pts)# 计算出iouious = int_area * 1.0 / (area1 + area2 - int_area)ious_total.append(ious)else:ious = 0ious_total.append(ious)# numpy转为CPU tensortotal_ious = torch.from_numpy(np.array(ious_total))total_ious = total_ious.type(torch.FloatTensor)  # 转Float# CPU tensor转GPU tensorious = total_ious.cuda()return iousif __name__ == '__main__':import osos.environ["CUDA_VISIBLE_DEVICES"] = '1'boxes1 = np.array([50, 50, 100, 300, 0],np.float32)boxes2 = np.array([50, 50, 100, 300, -45.], np.float32)ious_area = iou_rotate_calculate(boxes1, boxes2, use_gpu=False)print('###ious_area:',ious_area)

2 旋转框NMS

"""
2019.7.4 : 对斜框计算来计算nms
"""
#coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_functionimport numpy as np
import cv2
import tensorflow as tfdef nms_rotate(decode_boxes, scores, iou_threshold, max_output_size,use_angle_condition=False, angle_threshold=0, use_gpu=False, gpu_id=1):""":param boxes: format [x_c, y_c, w, h, theta]:param scores: scores of boxes:param threshold: iou threshold (0.7 or 0.5):param max_output_size: max number of output:return: the remaining index of boxes"""if use_gpu:#采用gpu方式keep = nms_rotate_gpu(boxes_list=decode_boxes,scores=scores,iou_threshold=iou_threshold,angle_gap_threshold=angle_threshold,use_angle_condition=use_angle_condition,device_id=gpu_id)keep = tf.cond(tf.greater(tf.shape(keep)[0], max_output_size),true_fn=lambda: tf.slice(keep, [0], [max_output_size]),false_fn=lambda: keep)else: #采用cpu方式keep = tf.py_func(nms_rotate_cpu,inp=[decode_boxes, scores, iou_threshold, max_output_size],Tout=tf.int64)return keepdef nms_rotate_cpu(boxes, scores, iou_threshold, max_output_size):print("###运行cpu")keep = [] #保留框的结果集合order = scores.argsort()[::-1] #对检测结果得分进行降序排序num = boxes.shape[0] #获取检测框的个数suppressed = np.zeros((num), dtype=np.int)for _i in range(num):# 若当前保留框集合中的个数大于max_output_size时,直接返回if len(keep) >= max_output_size:breaki = order[_i]# 对于抑制的检测框直接跳过if suppressed[i] == 1:continuekeep.append(i) #保留当前框的索引#根据box信息组合成opencv中的旋转bboxr1 = ((boxes[i, 0], boxes[i, 1]), (boxes[i, 2], boxes[i, 3]), boxes[i, 4])#计算当前检测框的面积area_r1 = boxes[i, 2] * boxes[i, 3]#对剩余的而进行遍历for _j in range(_i + 1, num):j = order[_j]if suppressed[i] == 1:continuer2 = ((boxes[j, 0], boxes[j, 1]), (boxes[j, 2], boxes[j, 3]), boxes[j, 4])area_r2 = boxes[j, 2] * boxes[j, 3]inter = 0.0#求两个旋转矩形的交集,并返回相交的点集合int_pts = cv2.rotatedRectangleIntersection(r1, r2)[1]if int_pts is not None:#求点集的凸边形order_pts = cv2.convexHull(int_pts, returnPoints=True)# 计算当前点集合组成的凸边形的面积int_area = cv2.contourArea(order_pts)#计算出iouinter = int_area * 1.0 / (area_r1 + area_r2 - int_area + 0.0000001)# 对大于设定阈值的检测框进行滤除if inter >= iou_threshold:suppressed[j] = 1return np.array(keep, np.int64)# gpu的实现方式
def nms_rotate_gpu(boxes_list, scores, iou_threshold, use_angle_condition=False, angle_gap_threshold=0, device_id=0):if use_angle_condition:y_c, x_c, h, w, theta = tf.unstack(boxes_list, axis=1)boxes_list = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1)keep = tf.py_func(rotate_gpu_nms,inp=[det_tensor, iou_threshold, device_id],Tout=tf.int64)return keepelse:y_c, x_c, h, w, theta = tf.unstack(boxes_list, axis=1)boxes_list = tf.transpose(tf.stack([x_c, y_c, w, h, theta]))det_tensor = tf.concat([boxes_list, tf.expand_dims(scores, axis=1)], axis=1)keep = tf.py_func(rotate_gpu_nms,inp=[det_tensor, iou_threshold, device_id],Tout=tf.int64)keep = tf.reshape(keep, [-1])return keepif __name__ == '__main__':boxes = np.array([[50, 40, 100, 100, 0],[60, 50, 100, 100, 0],[50, 30, 100, 100, -45.],[200, 190, 100, 100, 0.]])scores = np.array([ 0.77,0.79, 0.88, 0.66,])keep = nms_rotate(tf.convert_to_tensor(boxes, dtype=tf.float32), tf.convert_to_tensor(scores, dtype=tf.float32),0.7, 5)import osos.environ["CUDA_VISIBLE_DEVICES"] = '1'with tf.Session() as sess:print(sess.run(keep))

目标检测-斜框IOU,nms计算相关推荐

  1. 解决rotatedRectangleIntersection计算目标检测旋转框IOU不准确问题C++、opencv

    问题 语言 :C++ OpenCV版本:3.4.0 在目标检测中,后处理阶段会用到非极大值抑制来过滤目标框,而计算两个框的IOU(交并比)则是其关键的一环,先计算两个框相交的点,再求出这些点构成的多边 ...

  2. 目标检测中的Iou与map指标详细介绍(零基础)

    目标检测中的Iou与map指标详细介绍(零基础) 最近在算法岗实习,更新的频率会低一点,希望在实习过程中学到更多有用的视觉知识. IOU指标 下图中Ground truth为标记的正确框,Predic ...

  3. 解决图像目标检测两框重叠问题

    文章目录 1 问题现象 2 解决办法 3 Non-Maximum Suppression 原理 3.1 什么是非极大值抑制 3.2 为什么要用非极大值抑制 3.3 如何使用非极大值抑制 3.4 效果 ...

  4. 目标检测后处理:从nms到softer nms

    文章目录 1 NMS 1.1 动机 1.2 步骤 2 Soft-NMS 2.1 动机 2.2 算法思想 2.3 步骤 3 Softer-NMS 3.1 动机 3.1.1 现有方法的问题 3.1.2 本 ...

  5. 目标检测的置信度和NMS

    置信度(confidence) 还存在一个很关键的问题:在训练中我们挑选哪个bounding box的准则是选择预测的box与ground truth box的IOU最大的bounding box做为 ...

  6. 一文看懂目标检测之RCNN(NMS和Soft-NMS算法)

    目标检测之RCNN 1. RCNN:Regions with CNN features[2014 Ross] 图片输入 区域建议(region proposal) 特征提取(feature extra ...

  7. 目标检测中的IoU、GIoU、DIoU与CIoU

    什么是IOU? 简单来说IOU就是用来度量目标检测中预测框与真实框的重叠程度.在图像分类中,有一个明确的指标准确率来衡量模型分类模型的好坏.其公式为: acc=PtrueNN=全部样本的数量,Ptru ...

  8. 香港大学提出OneNet:一阶段端到端目标检测网络,无需NMS!无需二分匹配!

    点击上方,选择星标或置顶,不定期资源大放送! 阅读大概需要15分钟 Follow小博主,每天更新前沿干货 本文作者:孙培泽 |  编辑:Amusi https://zhuanlan.zhihu.com ...

  9. 目标检测回归损失函数——IOU、GIOU、DIOU、CIOU、EIOU

    一.IOU Loss 上一篇文章提到L1,L2及其变种只将Bounding box的四个角点分别求loss然后相加,没有引入box四个顶点之间的相关性并且模型在训练过程中更偏向于尺寸更大的物体.在此基 ...

最新文章

  1. 鸿蒙os操作系统合作伙伴,华为公布三大鸿蒙OS系统 已有大量合作伙伴进行开发...
  2. leetcode-206 反转链表
  3. MyBatis学习总结(9)——使用MyBatis Generator自动创建代码
  4. 蒋林涛:SDN/NFV仍有大量问题未解决 大网应用尚需努力
  5. go语言学习(一)——go语言简介和环境搭建
  6. Win32环境下两种用于C++的线程同步类(上)
  7. 一个现金流量表的代码
  8. 想学习java,如果学不会java怎么办?
  9. linux 批量创建用户和删除用户
  10. 插入排序,二分查找插入排序,使用二叉树的插入排序
  11. 原力计划·精英季来了!第一周周榜揭晓,你喜欢的博主上榜了吗?
  12. gluoncv 目标检测,训练自己的数据集
  13. 杭电 1142 十字链表存储
  14. Matlab安装过程
  15. 考研心得--一个差劲的ACMer
  16. html+css+js实现小游戏flybird(完整版)
  17. JavaScript生成唯一uuid
  18. coreldraw16开三折页_cdr怎么制作三折页?cdrX6制作三折页模板教程
  19. 陕西电力同业对标管理系统
  20. win32asm写的红警2的修改器

热门文章

  1. Run、RunOnce 键值解析
  2. Cortex-A7 MPCore简单介绍
  3. 漫画 | 这位助理教授,把整个数据库行业搅了个天翻地覆!
  4. 设计模式23种通熟解释和简明教程
  5. Android 开发 voip/sip 程序
  6. DataGrip工具链接Mysql报【08S01】解决方案
  7. 【AntDesign】巧妙修改Timeline时间轴
  8. 三维GIS可视化技术在城市管理中的作用
  9. 家里全面翻修,破旧家电全部换,电视选哪台?
  10. 2020AVR单片机课程项目汇总