boxes_convert.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. if copy:
  34. boxes = boxes.copy()
  35. boxes[..., 2:4] -= boxes[..., 0:2]
  36. boxes[..., 0:2] += boxes[..., 2:4] * 0.5
  37. return boxes
  38. def convert_cxcywh_to_xyxy(boxes, copy=True):
  39. """Convert [cx, cy, width, height] format to [x_min, y_min, x_max, y_max] format.
  40. """
  41. if copy:
  42. boxes = boxes.copy()
  43. boxes[..., 0:2] -= boxes[..., 2:4] * 0.5
  44. boxes[..., 2:4] += boxes[..., 0:2]
  45. return boxes
  46. def convert_boxes_format(boxes, in_fmt, out_fmt, copy=True):
  47. """Converts boxes from given in_fmt to out_fmt.
  48. Supported in_fmt and out_fmt are:
  49. 'xyxy': boxes are represented via corners, x1, y1 being top left and x2, y2 being bottom right.
  50. 'xywh' : boxes are represented via corner, width and height, x1, y2 being top left, w, h being width and height.
  51. 'cxcywh' : boxes are represented via centre, width and height, cx, cy being center of box, w, h
  52. being width and height.
  53. Args:
  54. boxes: boxes which will be converted.
  55. in_fmt (str): Input format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh'].
  56. out_fmt (str): Output format of given boxes. Supported formats are ['xyxy', 'xywh', 'cxcywh']
  57. Returns:
  58. boxes: Boxes into converted format.
  59. References:
  60. torchvision.ops.box_convert
  61. """
  62. allowed_fmts = ("xyxy", "xywh", "cxcywh")
  63. if in_fmt not in allowed_fmts or out_fmt not in allowed_fmts:
  64. raise ValueError("Unsupported Bounding Box Conversions for given in_fmt and out_fmt")
  65. if copy:
  66. boxes = boxes.copy()
  67. if in_fmt == out_fmt:
  68. return boxes
  69. if (in_fmt, out_fmt) == ("xyxy", "xywh"):
  70. boxes = convert_xyxy_to_xywh(boxes, copy=False)
  71. elif (in_fmt, out_fmt) == ("xywh", "xyxy"):
  72. boxes = convert_xywh_to_xyxy(boxes, copy=False)
  73. elif (in_fmt, out_fmt) == ("xywh", "cxcywh"):
  74. boxes = convert_xywh_to_cxcywh(boxes, copy=False)
  75. elif (in_fmt, out_fmt) == ("cxcywh", "xywh"):
  76. boxes = convert_cxcywh_to_xywh(boxes, copy=False)
  77. elif (in_fmt, out_fmt) == ("xyxy", "cxcywh"):
  78. boxes = convert_xyxy_to_cxcywh(boxes, copy=False)
  79. elif (in_fmt, out_fmt) == ("cxcywh", "xyxy"):
  80. boxes = convert_cxcywh_to_xyxy(boxes, copy=False)
  81. return boxes