def calc_rectangle_bbox(points, img_h, img_w):
  """
  calculate bbox from a rectangle.

  Parameters
  ----------
  points : list
    a list of two points. E.g. `[[x1, y1], [x2, y2]]`
  img_h : int
    maximal image height
  img_w : int
    maximal image width

  Returns
  -------
  dict
    corresponding bbox. I.e. `{ 'xmin': xmin, 'ymin': ymin, 'xmax': xmax, 'ymax': ymax }`
  """
  lt, rb = points
  xmin, ymin = lt
  xmax, ymax = rb
  xmin = min(max(0, xmin), img_w)
  xmax = min(max(0, xmax), img_w)
  ymin = min(max(0, ymin), img_h)
  ymax = min(max(0, ymax), img_h)
  return { 'xmin':xmin, 'ymin':ymin, 'xmax':xmax, 'ymax':ymax }