Given is a grayscale image I as 2D Tensor (Dimension W,H) and a Tensor of coordinates C (Dim. None,2). I want to interpret the rows of
There is no built-in op that performs this kind of interpolation, but you should be able to do it using a composition of existing TensorFlow ops. I'd suggest the following strategy for the bilinear case:
From your tensor C of indices, compute integer tensors corresponding to the four corner points. For example (with names assuming that the origin is at the top left):
top_left = tf.cast(tf.floor(C), tf.int32)
top_right = tf.cast(
tf.concat(1, [tf.floor(C[:, 0:1]), tf.ceil(C[:, 1:2])]), tf.int32)
bottom_left = tf.cast(
tf.concat(1, [tf.ceil(C[:, 0:1]), tf.floor(C[:, 1:2])]), tf.int32)
bottom_right = tf.cast(tf.ceil(C), tf.int32)
From each tensor representing a particular corner point, extract a vector of values from I at those points. For example, for the following function does this for the 2-D case:
def get_values_at_coordinates(input, coordinates):
input_as_vector = tf.reshape(input, [-1])
coordinates_as_indices = (coordinates[:, 0] * tf.shape(input)[1]) + coordinates[:, 1]
return tf.gather(input_as_vector, coordinates_as_indices)
values_at_top_left = get_values_at_coordinates(I, top_left)
values_at_top_right = get_values_at_coordinates(I, top_right)
values_at_bottom_left = get_values_at_coordinates(I, bottom_left)
values_at_bottom_right = get_values_at_coordinates(I, bottom_right)
Compute the interpolation in the horizontal direction first:
# Varies between 0.0 and 1.0.
horizontal_offset = C[:, 0] - tf.cast(top_left[:, 0], tf.float32)
horizontal_interpolated_top = (
((1.0 - horizontal_offset) * values_at_top_left)
+ (horizontal_offset * values_at_top_right))
horizontal_interpolated_bottom = (
((1.0 - horizontal_offset) * values_at_bottom_left)
+ (horizontal_offset * values_at_bottom_right))
Now compute the interpolation in the vertical direction:
vertical_offset = C[:, 1] - tf.cast(top_left[:, 1], tf.float32)
interpolated_result = (
((1.0 - vertical_offset) * horizontal_interpolated_top)
+ (vertical_offset * horizontal_interpolated_bottom))