def apply_homography_to_point(hmg, x, y, is_matrix=True):
  """Transforms a point (x, y) with a homography hmg.

  Args:
    hmg: a homography transformation of data type float32. If mode is 'matrix',
      it is of shape [3, 3]; otherwise its shape is [8].
    x: the x coordinate of the point
    y: the y coordinate of the point
    is_matrix: whether the parameterization of the homography is matrix or not
  Returns:
    the transformed coordinate of the input point
  """
  if is_matrix:
    z1 = hmg[2, 0] * x + hmg[2, 1] * y + hmg[2, 2]
    x1 = (hmg[0, 0] * x + hmg[0, 1] * y + hmg[0, 2]) / z1
    y1 = (hmg[1, 0] * x + hmg[1, 1] * y + hmg[1, 2]) / z1
  else:
    z1 = hmg[6] * x + hmg[7] * y + 1.0
    x1 = (hmg[0] * x + hmg[1] * y + hmg[2]) / z1
    y1 = (hmg[3] * x + hmg[4] * y + hmg[5]) / z1
  return x1, y1