예제 #1
0
 def convert(self,im):
     input_height,input_width = im.shape[:2]
     coords = tf.stack([input_width*self.lutx,input_height*self.luty],axis=-1)
     coords_flat = tf.reshape(coords,(1,-1,2))
     pano_flat = interpolate_bilinear(tf.cast(im[None,...],'float32'),coords_flat,indexing='xy')
     pano = tf.reshape(pano_flat[0],(self.output_height,self.output_width) + im.shape[2:])
     #coords = np.stack([self.luty*input_height,self.lutx*input_width],axis=0)
     #print(coords.shape)
     #pano = warp(im,coords,preserve_range=True).astype('uint8')
     return pano
예제 #2
0
    def test_interpolate_small_grid_xy(self):
        grid = tf.constant([[0., 1., 2.], [3., 4., 5.], [6., 7., 8.]],
                           shape=[1, 3, 3, 1])
        query_points = tf.constant(
            [[0., 0.], [0., 1.], [0.5, 2.0], [1.5, 1.5]], shape=[1, 4, 2])
        expected_results = np.reshape(np.array([0., 3., 6.5, 6.]), [1, 4, 1])

        interp = interpolate_bilinear(grid, query_points, indexing="xy")

        self.assertAllClose(expected_results, interp)
예제 #3
0
    def test_interpolate_small_grid_batched(self):
        grid = tf.constant([[[0., 1.], [3., 4.]], [[5., 6.], [7., 8.]]],
                           shape=[2, 2, 2, 1])
        query_points = tf.constant([[[0., 0.], [1., 0.], [0.5, 0.5]],
                                    [[0.5, 0.], [1., 0.], [1., 1.]]])
        expected_results = np.reshape(np.array([[0., 3., 2.], [6., 7., 8.]]),
                                      [2, 3, 1])

        interp = interpolate_bilinear(grid, query_points)

        self.assertAllClose(expected_results, interp)
예제 #4
0
def test_interpolate_small_grid_batched():
    grid = tf.constant([[[0.0, 1.0], [3.0, 4.0]], [[5.0, 6.0], [7.0, 8.0]]],
                       shape=[2, 2, 2, 1])
    query_points = tf.constant([[[0.0, 0.0], [1.0, 0.0], [0.5, 0.5]],
                                [[0.5, 0.0], [1.0, 0.0], [1.0, 1.0]]])
    expected_results = np.reshape(np.array([[0.0, 3.0, 2.0], [6.0, 7.0, 8.0]]),
                                  [2, 3, 1])

    interp = interpolate_bilinear(grid, query_points)

    np.testing.assert_allclose(expected_results, interp)
예제 #5
0
def test_interpolate_small_grid_xy():
    grid = tf.constant(
        [[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0], [9.0, 10.0, 11.0]],
        shape=[1, 4, 3, 1],
    )
    query_points = tf.constant(
        [[0.0, 0.0], [0.0, 1.0], [0.5, 2.0], [1.5, 1.5], [2.0, 3.0]], shape=[1, 5, 2],
    )
    expected_results = np.reshape(np.array([0.0, 3.0, 6.5, 6.0, 11.0]), [1, 5, 1])

    interp = interpolate_bilinear(grid, query_points, indexing="xy")

    np.testing.assert_allclose(expected_results, interp)
예제 #6
0
    def interpolate(self, I, x):

        y = tf.transpose(x, perm=[0, 1, 3, 2])

        shape = tf.shape(y)
        shape_I = tf.shape(I)

        y = tf.reshape(y, shape=[shape[0], shape[1] * shape[2], shape[3]])

        I_int = interpolate_bilinear(I, y, indexing='xy')

        I_int = tf.reshape(I_int,
                           shape=[shape[0], shape[1], shape[2], shape_I[-1]])

        return I_int
예제 #7
0
    def convert_to_calibration(self, Kc, Kd, image, depth):

        shape_i = tf.shape(image)
        shape_d = tf.shape(depth)

        grid_d = self.g.generate_homogeneous_points(depth)

        grid_d = tf.reshape(grid_d, [shape_d[0], shape_d[1]*shape_d[2], 3])
        grid_d = tf.transpose(grid_d, perm=[0, 2, 1])

        Kd_inv = tf.linalg.inv(Kd)

        x = tf.matmul(tf.matmul(Kc, Kd_inv), grid_d)
        x = tf.transpose(x, perm=[0, 2, 1])

        I_int = interpolate_bilinear(image, x[:, :, 0:2], indexing='xy')

        Im_transformed = tf.reshape(
            I_int, [shape_d[0], shape_d[1], shape_d[2], shape_i[-1]])

        return Im_transformed
예제 #8
0
def sample(image: type_alias.TensorLike,
           warp: type_alias.TensorLike,
           resampling_type: ResamplingType = ResamplingType.BILINEAR,
           border_type: BorderType = BorderType.ZERO,
           pixel_type: PixelType = PixelType.HALF_INTEGER,
           name: Optional[str] = "sample") -> tf.Tensor:
    """Samples an image at user defined coordinates.

  Note:
    The warp maps target to source. In the following, A1 to An are optional
    batch dimensions.

  Args:
    image: A tensor of shape `[B, H_i, W_i, C]`, where `B` is the batch size,
      `H_i` the height of the image, `W_i` the width of the image, and `C` the
      number of channels of the image.
    warp: A tensor of shape `[B, A_1, ..., A_n, 2]` containing the x and y
      coordinates at which sampling will be performed. The last dimension must
      be 2, representing the (x, y) coordinate where x is the index for width
      and y is the index for height.
   resampling_type: Resampling mode. Supported values are
     `ResamplingType.NEAREST` and `ResamplingType.BILINEAR`.
    border_type: Border mode. Supported values are `BorderType.ZERO` and
      `BorderType.DUPLICATE`.
    pixel_type: Pixel mode. Supported values are `PixelType.INTEGER` and
      `PixelType.HALF_INTEGER`.
    name: A name for this op. Defaults to "sample".

  Returns:
    Tensor of sampled values from `image`. The output tensor shape
    is `[B, A_1, ..., A_n, C]`.

  Raises:
    ValueError: If `image` has rank != 4. If `warp` has rank < 2 or its last
    dimension is not 2. If `image` and `warp` batch dimension does not match.
  """
    with tf.name_scope(name):
        image = tf.convert_to_tensor(value=image, name="image")
        warp = tf.convert_to_tensor(value=warp, name="warp")

        shape.check_static(image, tensor_name="image", has_rank=4)
        shape.check_static(warp,
                           tensor_name="warp",
                           has_rank_greater_than=1,
                           has_dim_equals=(-1, 2))
        shape.compare_batch_dimensions(tensors=(image, warp),
                                       last_axes=0,
                                       broadcast_compatible=False)

        if pixel_type == PixelType.HALF_INTEGER:
            warp -= 0.5

        if resampling_type == ResamplingType.NEAREST:
            warp = tf.math.round(warp)

        if border_type == BorderType.ZERO:
            image = tf.pad(image, ((0, 0), (1, 1), (1, 1), (0, 0)))
            warp = warp + 1

        warp_shape = tf.shape(warp)
        flat_warp = tf.reshape(warp, (warp_shape[0], -1, 2))
        flat_sampled = tfa_image.interpolate_bilinear(image,
                                                      flat_warp,
                                                      indexing="xy")
        output_shape = tf.concat(
            (warp_shape[:-1], tf.shape(flat_sampled)[-1:]), 0)
        return tf.reshape(flat_sampled, output_shape)