python - 分别在 RGB channel 上进行 Tensorflow 2D 卷积?

标签 python tensorflow rgb gaussian

我想对 RGB 图像应用高斯模糊。 我希望它在每个 channel 上独立运行。下面的代码输出具有 3 个 channel 但都具有相同值的模糊图像,导致灰色图像

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 channel 应用卷积并输出 RGB 模糊图像。

最佳答案

您可以使用 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

关于python - 分别在 RGB channel 上进行 Tensorflow 2D 卷积?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55687616/

相关文章:

python - 我们如何使用 tensorflow 使用 MNIST 数据集一类作为输入?

android - 如何在Android上应用将图像从彩色转灰度的算法?

c++ - 从高度编码的 RGB 值

python - 贪婪的正则表达式回顾

python - 将功能分解为被动(算法)和主动(执行)对象

python - 如何配置 uWSGI 以便使用 pdb 进行调试(--honour-stdin 配置问题)

python - TensorFlow - 值错误 : features should be a dictionary of `Tensor` s

python - 使用 VGG16 预训练模型处理灰度图像时出错

python - 从 Google Cloud Storage 将 zip 文件加载到内存中的用法是否正确?

python - 使用 ffmpeg 从 yuv 到 rgb 的错误转换视频