RGB通道上的Tensorflow 2D卷积分 别?

问题描述 投票:2回答:1

我想将高斯模糊应用于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模糊图像。

python tensorflow rgb gaussian
1个回答
1
投票

你可以使用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
© www.soinside.com 2019 - 2024. All rights reserved.