Apply transformation matrix to batch of images

321 views
Skip to first unread message

samp...@gmail.com

unread,
Jun 10, 2017, 8:23:47 PM6/10/17
to Discuss
Hello,

I have a batch of images: (num, height, width, channels)

I also have a spatial transformation matrix: (2, 3)

This is meant to be applied pixel-wise across each channel, for each sample.

For example, applying this transformation to pixel (80, 90) of the first channel of the first image in the batch:
inputImgs = tf.Variable(np.zeros((batch_size, height, width, 3)))
outputImgs = tf.Variable(np.zeros((batch_size, height, width, 3)))

// Apply transformation to point (80, 90) of channel 0 of image 0
trans
= [[-1, 0, -0.5 * width], [0, 1, -0.5 * height]] // size (2, 3)
loc
= [[80], [90], [1]] // add row with 1 to make size (3, 1)
transformed_loc
= trans * loc

outputImgs
[0, 80, 90, 0] = inputImgs[0, transformed_loc[0], transformed_loc[1], 0]


I'm having difficulty conceptualizing the fastest way to perform this transformation to a single image, let alone the entire batch of images.

In effect, I want to add another dimension to the input multiply every pixel by a transformation matrix somehow.

Thanks for your help in advance.

samp...@gmail.com

unread,
Jun 10, 2017, 8:32:58 PM6/10/17
to Discuss, samp...@gmail.com
Basically, I'm looking for Matlabs imtransform (preferably for batches of images).

Sebastian Raschka

unread,
Jun 10, 2017, 8:42:58 PM6/10/17
to samp...@gmail.com, TensorFlow Mailinglist
Hi,
I am not sure if I understand your transformation that you want to apply to the images, but in general, you could use a map function to apply this to all images (in tensorflow it's called tf.map_fn)

For example, sth like


def transf_1_img(x):
# your transf function here
return transformed_x

transformed_images = tf.map_fn(transf_1_img, your_images)

For more details, also see https://www.tensorflow.org/api_docs/python/tf/map_fn

Best,
Sebastian
> --
> You received this message because you are subscribed to the Google Groups "Discuss" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to discuss+u...@tensorflow.org.
> To post to this group, send email to dis...@tensorflow.org.
> To view this discussion on the web visit https://groups.google.com/a/tensorflow.org/d/msgid/discuss/5230ad76-dc8c-499e-bc32-ec6210f2e12a%40tensorflow.org.

Rob

unread,
Jun 10, 2017, 11:13:45 PM6/10/17
to samp...@gmail.com, Discuss
The functions in tf.contrib.image may be helpful to you. 



Sent from my mobile device

samp...@gmail.com

unread,
Jun 11, 2017, 6:43:23 PM6/11/17
to Discuss, samp...@gmail.com
Thanks, Rob, they are useful. Any idea how to access them? For instance angles_to_projective_transforms: https://github.com/tensorflow/tensorflow/blob/r1.1/tensorflow/contrib/image/python/ops/image_ops.py#L81

I could copy/paste them out, but I'd rather stay up-to-date as they get updated.

samp...@gmail.com

unread,
Jun 11, 2017, 6:54:17 PM6/11/17
to Discuss, samp...@gmail.com
Ah, I'm still on 1.0, thanks!
Reply all
Reply to author
Forward
0 new messages