RGB通道分别进行Tensorflow二维卷积?

3

我希望对一个RGB图像应用高斯模糊。

我希望每个通道都可以独立操作。下面的代码输出了一个有3个通道的模糊图像,但是所有通道的值都相同,导致了一张灰色的图像

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] 

我正在寻找一个Tensorflow函数,它可以分别在每个R/G/B通道上应用卷积并输出一张RGB模糊图像。
1个回答

2
你可以使用tf.nn.separable_conv2d来实现这个功能:
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

谢谢,我看到了但是忽略了使点过滤器无用的技巧。TF 应该将其添加到文档中 :)。 - natoucs

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接