Search code examples
computer-visionhomographyprojective-geometry

The reason for the half integer coordinates


I am reading some projective geometry image warping code from Google

def WarpCoordinatesWithHomography(homography, rect, cfg):
  """Computes the warped coordinates from rect through homography.

  Computes the corresponding coordinates on the image for each pixel of rect.
  Note that the returned coordinates are in x, y order.
  The returned image can be used to warp from the image to the
  pixels of the depth_plane within rect.
  warp_coordinates = ApplyHomographyToCoords(....)
  warped_from_image(x, y) = image(warp_coordinates(x, y)[0],
                                  warp_coordinates(x, y)[1])

  Args:
    homography: A 3x3 tensor representing the transform applied to the
      coordinates inside rect.
   rect: An integer tensor [start_y, start_x, end_y, end_x] representing a rect.

  Returns:
    Returns a rect.height * rect.width * 2 tensor filled with image
    coordinates.
  """
  ys = tf.cast(tf.range(rect[0], rect[2]), cfg.vx_tf_dtype)
  xs = tf.cast(tf.range(rect[1], rect[3]), cfg.vx_tf_dtype)

  # Adds 0.5, as pixel centers are assumed to be at half integer coordinates.
  image_coords_t = tf.stack(tf.meshgrid(xs, ys), axis=-1) + 0.5
  hom_image_coords_t = tf.concat(
      (image_coords_t, tf.ones([rect[2] - rect[0], rect[3] - rect[1], 1])),
      axis=-1)

  hom_warped_coords = tf.einsum('ijk,lk->ijl', hom_image_coords_t, homography)
  res = tf.math.divide_no_nan(hom_warped_coords[:, :, :-1], hom_warped_coords[:, :, 2:3])
  return  res

What is the reason to use "half integer coordinates" that started at 0.5?


Solution

  • Some people consider a pixel to be a point sample in a grid, some people consider them to be a 1x1 square.

    In that latter category, some people consider the 1x1 square to be centered on integer coordinates, such that one square ranges from 0.5 to 1.5, for example. Other people consider the square to range from 0.0 to 1.0, for example, and thus the pixel is centered on "half integer".

    In short, it is just a choice of coordinate system. It does not matter what coordinate system you use, as long as you use it consistently.