当前位置:   article > 正文

正常矩形计算IOU与与NMS,多边形计算IOU_多边形的iou

多边形的iou

一.计算IOU

  1. def intersect(box_a, box_b):
  2. max_xy = np.minimum(box_a[:, 2:], box_b[2:])
  3. min_xy = np.maximum(box_a[:, :2], box_b[:2])
  4. inter = np.clip((max_xy - min_xy), a_min=0, a_max=np.inf)
  5. return inter[:, 0] * inter[:, 1]
  6. def jaccard_numpy(box_a, box_b):
  7. """Compute the jaccard overlap of two sets of boxes. The jaccard overlap
  8. is simply the intersection over union of two boxes.
  9. E.g.:
  10. Args:
  11. box_a: Multiple bounding boxes, Shape: [num_boxes,4]
  12. box_b: Single bounding box, Shape: [4]
  13. Return:
  14. jaccard overlap: Shape: [box_a.shape[0], box_a.shape[1]]
  15. """
  16. inter = intersect(box_a, box_b)
  17. area_a = ((box_a[:, 2]-box_a[:, 0]) *
  18. (box_a[:, 3]-box_a[:, 1])) # [A,B]
  19. area_b = ((box_b[2]-box_b[0]) *
  20. (box_b[3]-box_b[1])) # [A,B]
  21. union = area_a + area_b - inter
  22. return inter / union # [A,B]

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

  1. def compute_IOU(algrim_bboxs,fix_bboxs):
  2. # print('algrim_bboxs:', algrim_bboxs)
  3. # print('fix_bboxs:', fix_bboxs)
  4. for i, fix_bbox in enumerate(fix_bboxs):
  5. print('fix_bbox:', fix_bbox)
  6. fix_x1, fix_y1, fix_x2, fix_y2 = fix_bbox
  7. x1, y1, x2, y2 = algrim_bboxs[:,0], algrim_bboxs[:,1], algrim_bboxs[:,-2], algrim_bboxs[:,-1]
  8. area = (y2-y1)*(x2-x1)
  9. 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)
  10. union = area+(fix_y2-fix_y1)*(fix_x2-fix_x1)-inter
  11. IOU = inter/union
  12. # print('IOU:', IOU)
  13. index = np.where(IOU > 0.9)[0]
  14. print('algrim_bboxs[index]:', algrim_bboxs[index])

 

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

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

二,一般加上score,这样用

  1. scores = np.array([0.5, 0.7, 0.3, 0.2])
  2. ixs=scores.argsort()[::-1]
  3. print('ixs=',ixs)
  4. boxes=np.array([[1,2,3,4],
  5. [1.2, 0.4,2.1, 0.8],
  6. [5,6,7,8],
  7. [9,10,11,12]])
  8. y1 = boxes[:, 0]
  9. x1 = boxes[:, 1]
  10. y2 = boxes[:, 2]
  11. x2 = boxes[:, 3]
  12. area = (y2 - y1) * (x2 - x1)
  13. print('area=',area)
  14. box_area=area[ixs[0]]
  15. print('box_area=',box_area)
  16. boxes_area=area[ixs[1:]]
  17. print('boxes_area=',boxes_area)
  18. box=boxes[ixs[0]]
  19. boxes=boxes[ixs[1:]]
  20. y1 = np.maximum(box[0], boxes[:, 0])
  21. y2 = np.minimum(box[2], boxes[:, 2])
  22. x1 = np.maximum(box[1], boxes[:, 1])
  23. x2 = np.minimum(box[3], boxes[:, 3])
  24. #注意与0判断 若不想交返回0值,即intersection为0
  25. intersection = np.maximum(x2 - x1, 0) * np.maximum(y2 - y1, 0)
  26. print('intersection=',intersection)
  27. union = box_area + boxes_area - intersection[:]
  28. print('union=',union)
  29. iou = intersection / union
  30. print('iou=',iou)

三,求其NMS后的框的小trick

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

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

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

五,faster rcnn NMS

  1. def py_cpu_nms(dets, thresh):
  2. """Pure Python NMS baseline."""
  3. x1 = dets[:, 0]
  4. y1 = dets[:, 1]
  5. x2 = dets[:, 2]
  6. y2 = dets[:, 3]
  7. scores = dets[:, 4]
  8. areas = (x2 - x1 + 1) * (y2 - y1 + 1)
  9. order = scores.argsort()[::-1]
  10. print('order',order)
  11. keep = []
  12. while order.size > 0:
  13. i = order[0]
  14. keep.append(i)
  15. xx1 = np.maximum(x1[i], x1[order[1:]])
  16. yy1 = np.maximum(y1[i], y1[order[1:]])
  17. xx2 = np.minimum(x2[i], x2[order[1:]])
  18. yy2 = np.minimum(y2[i], y2[order[1:]])
  19. w = np.maximum(0.0, xx2 - xx1 + 1)
  20. h = np.maximum(0.0, yy2 - yy1 + 1)
  21. inter = w * h
  22. ovr = inter / (areas[i] + areas[order[1:]] - inter)
  23. print('ovr=',ovr)
  24. print('np.where(ovr <= thresh)',np.where(ovr <= thresh))
  25. inds = np.where(ovr <= thresh)[0]
  26. print('inds=',inds)
  27. print('inds + 1',inds + 1)
  28. print('order[inds + 1]',order[inds + 1])
  29. order = order[inds + 1]
  30. print('order=',order)
  31. return keep
  32. def nms():
  33. # ###self nms
  34. result = [np.array([[7.9301813e+02, 6.3052429e+02, 8.9795898e+02,
  35. 7.2513965e+02,9.9307442e-01],
  36. [8.0682990e+02, 6.4606281e+02, 8.7198071e+02, 7.1328979e+02,
  37. 9.5732883e-02]], dtype=np.float32)]
  38. ##faster rcnn nms
  39. keep=py_cpu_nms(result[0],0.7)
  40. print('keep=',keep)
  41. for i in keep:
  42. print('i=',i)
  43. print(result[0][i])

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

六.多边形利用polygon计算IOU

  1. def compute_IOU_():
  2. import numpy as np
  3. import shapely
  4. from shapely.geometry import Polygon, MultiPoint # 多边形
  5. path = './134.jpg'
  6. img = cv2.imread(path)
  7. print('img.shape:', img.shape)
  8. line1 = [728, 252, 908, 215, 934, 312, 752, 355] # 四边形四个点坐标的一维数组表示,[x,y,x,y....]
  9. line2 = [741, 262, 907, 228, 923, 308, 758, 342]
  10. # #debug to show
  11. # line1 = np.array(line1).reshape(4, 2)
  12. # line2 = np.array(line2).reshape(4, 2)
  13. # cv2.polylines(img, [np.array(line1).reshape(-1, 1, 2)], True, (0, 255, 0), thickness=2)
  14. # cv2.polylines(img, [np.array(line2).reshape(-1, 1, 2)], True, (0, 0, 255), thickness=2)
  15. # cv2.imwrite('134_with_rect.jpg',img)
  16. line1_box = np.array(line1).reshape(4, 2) # 四边形二维坐标表示
  17. # 凸多边形
  18. # poly1 = Polygon(line1_box).convex_hull
  19. #凸多边形与凹多边形
  20. poly1 = Polygon(line1_box)#.convex_hull # python四边形对象,会自动计算四个点,最后四个点顺序为:左上 左下 右下 右上 左上
  21. print(Polygon(line1_box).convex_hull)
  22. line2_box = np.array(line2).reshape(4, 2)
  23. # 凸多边形
  24. # poly2 = Polygon(line2_box).convex_hull
  25. # 凸多边形与凹多边形
  26. poly2 = Polygon(line2_box)
  27. print(Polygon(line2_box).convex_hull)
  28. union_poly = np.concatenate((line1_box, line2_box)) # 合并两个box坐标,变为8*2
  29. # print(union_poly)
  30. print(MultiPoint(union_poly).convex_hull) # 包含两四边形最小的多边形点
  31. if not poly1.intersects(poly2): # 如果两四边形不相交
  32. iou = 0
  33. else:
  34. try:
  35. inter_area = poly1.intersection(poly2).area # 相交面积
  36. print(inter_area)
  37. union_area = MultiPoint(union_poly).convex_hull.area
  38. print(union_area)
  39. if union_area == 0:
  40. iou = 0
  41. # iou = float(inter_area) / (union_area-inter_area) #错了
  42. iou = float(inter_area) / union_area
  43. except shapely.geos.TopologicalError:
  44. print('shapely.geos.TopologicalError occured, iou set to 0')
  45. iou = 0
  46. print('line1_box:', line1_box)
  47. print('iou:', iou)

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/114159
推荐阅读