python - RGB通道上的Tensorflow 2D卷积分别?
问题描述
我想对 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 模糊图像。
解决方案
你可以用它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 - ImportError:尝试在没有已知父包的情况下进行相对导入?
- java - Java maven项目,构建失败版本不匹配
- spring-boot - 如何通过spring b使用roleID和secretID从保险库访问凭据?
- python - python打印以下输出
- r - 在 dplyr 中从变量中引用列名
- typescript - Typescript/cypress-cucumber-preprocessor :使用动态示例编写场景大纲
- multithreading - 两个类之间的非法跨线程操作。无法区分线程以解决问题
- c++ - GetWindowDC 工作不正常
- amazon-web-services - 取回 Lambda 函数返回的数据并更新我的 React App Amplify
- java - firebase 实时数据库未存储数据,logcat 未显示错误