Reputation: 103
I want to apply a Gaussian blur to an RGB image. I want it to be operated on each channel independently. The code below outputs a blurred image with 3 channels but all with the same value, resulting in a grey image.
gauss_kernel_2d = gaussian_kernel(2, 0.0, 1.0) # outputs a 5*5 tensor
gauss_kernel = tf.tile(gauss_kernel_2d[:, :, tf.newaxis, tf.newaxis], [1, 1, 3, 3]) # 5*5*3*3
image = tf.nn.conv2d(tf.expand_dims(image, 0), gauss_kernel, strides=[1, 1, 1, 1], padding='SAME') # 1*600*800*3
image = tf.squeeze(image) # 600*800*3
# shape of image needs to be [batch, in_height, in_width, in_channels]
# shape of filter needs to be [filter_height, filter_width, in_channels, out_channels]
I am looking for a Tensorflow function that applies the convolution on each R/G/B channel separately and outputs an RGB blurred image.
Upvotes: 3
Views: 1969
Reputation: 59731
You can use tf.nn.separable_conv2d
to do that:
import tensorflow as tf
# ...
gauss_kernel_2d = gaussian_kernel(2, 0.0, 1.0) # outputs a 5*5 tensor
gauss_kernel = tf.tile(gauss_kernel_2d[:, :, tf.newaxis, tf.newaxis], [1, 1, 3, 1]) # 5*5*3*1
# Pointwise filter that does nothing
pointwise_filter = tf.eye(3, batch_shape=[1, 1])
image = tf.nn.separable_conv2d(tf.expand_dims(image, 0), gauss_kernel, pointwise_filter,
strides=[1, 1, 1, 1], padding='SAME')
image = tf.squeeze(image) # 600*800*3
Upvotes: 2