boxes_convert.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import numpy as np
  2. def convert_xyxy_to_xywh(boxes, copy=True):
  3. """Convert [x_min, y_min, x_max, y_max] format to [x_min, y_min, width, height] format.
  4. """
  5. if copy:
  6. boxes = boxes.copy()
  7. boxes[:, 2:4] -= boxes[:, 0:2]
  8. return boxes
  9. def convert_xywh_to_xyxy(boxes, copy=True):
  10. """Convert [x_min, y_min, width, height] format to [x_min, y_min, x_max, y_max] format.
  11. """
  12. if copy:
  13. boxes = boxes.copy()
  14. boxes[:, 2:4] += boxes[:, 0:2]
  15. return boxes
  16. def convert_xywh_to_cxcywh(boxes, copy=True):
  17. """Convert [x_min, y_min, width, height] format to [cx, cy, width, height] format.
  18. """
  19. if copy:
  20. boxes = boxes.copy()
  21. boxes[:, 0:2] += boxes[:, 2:4] * 0.5
  22. return boxes
  23. def convert_cxcywh_to_xywh(boxes, copy=True):
  24. """Convert [cx, cy, width, height] format to [x_min, y_min, width, height] format.
  25. """
  26. if copy:
  27. boxes = boxes.copy()
  28. boxes[:, 0:2] -= boxes[:, 2:4] * 0.5
  29. return boxes
  30. def convert_xyxy_to_cxcywh(boxes, copy=True):
  31. """Convert [x_min, y_min, x_max, y_max] format to [cx, cy, width, height] format.
  32. """
  33. boxes = convert_xyxy_to_xywh(boxes, copy)
  34. boxes = convert_xywh_to_cxcywh(boxes, False)
  35. return boxes
  36. def convert_cxcywh_to_xyxy(boxes, copy=True):
  37. """Convert [x_min, y_min, x_max, y_max] format to [cx, cy, width, height] format.
  38. """
  39. boxes = convert_cxcywh_to_xywh(boxes, copy)
  40. boxes = convert_xywh_to_xyxy(boxes, False)
  41. return boxes
  42. def convert_boxes_format(boxes, in_fmt, out_fmt, copy=True):
  43. """Converts boxes from given in_fmt to out_fmt.
  44. Supported in_fmt and out_fmt are:
  45. 'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
  46. 'xywh' : boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
  47. 'cxcywh' : boxes are represented via centre, width and height, cx, cy being center of box, w, h
  48. being width and height.
  49. Args:
  50. boxes: boxes which will be converted.
  51. in_fmt (str): Input format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh'].
  52. out_fmt (str): Output format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh']
  53. Returns:
  54. boxes: Boxes into converted format.
  55. References:
  56. torchvision.ops.box_convert
  57. """
  58. allowed_fmts = ("xyxy", "xywh", "cxcywh")
  59. if in_fmt not in allowed_fmts or out_fmt not in allowed_fmts:
  60. raise ValueError("Unsupported Bounding Box Conversions for given in_fmt and out_fmt")
  61. if copy:
  62. boxes = boxes.copy()
  63. if in_fmt == out_fmt:
  64. return boxes
  65. if (in_fmt, out_fmt) == ("xyxy", "xywh"):
  66. boxes = convert_xyxy_to_xywh(boxes, copy=False)
  67. elif (in_fmt, out_fmt) == ("xywh", "xyxy"):
  68. boxes = convert_xywh_to_xyxy(boxes, copy=False)
  69. elif (in_fmt, out_fmt) == ("xywh", "cxcywh"):
  70. boxes = convert_xywh_to_cxcywh(boxes, copy=False)
  71. elif (in_fmt, out_fmt) == ("cxcywh", "xywh"):
  72. boxes = convert_cxcywh_to_xywh(boxes, copy=False)
  73. elif (in_fmt, out_fmt) == ("xyxy", "cxcywh"):
  74. boxes = convert_xyxy_to_cxcywh(boxes, copy=False)
  75. elif (in_fmt, out_fmt) == ("cxcywh", "xyxy"):
  76. boxes = convert_cxcywh_to_xyxy(boxes, copy=False)
  77. return boxes