Is there a quick way to convert tensors which encode an image from RGB to BGR?
In python that would be something like:
image = image[:, :, [2,1,0]]
You can use tf.strided_slice
or tf.reverse
.
For example,
import tensorflow as tf
img = tf.reshape(tf.range(30), [2, 5, 3])
# the following two lines produce equivalent result:
img_channel_swap = img[..., ::-1]
img_channel_swap_1 = tf.reverse(img, axis=[-1])
Note that the api of tf.reverse
is changed from tensorflow r1.0.
Assuming the channel is the last dimension.
channels = tf.unstack (image, axis=-1)
image = tf.stack ([channels[2], channels[1], channels[0]], axis=-1)
You can use tf.split
as well.
Snippet from tensorflow-vgg16/blob/master/vgg16.py#L5:
red, green, blue = tf.split(3, 3, rgb_scaled)
bgr = tf.concat(3, [blue, green, red])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With