gpt4 book ai didi

tensorflow - 如何在 Tensorflow 中制作二维高斯滤波器?

转载 作者:行者123 更新时间:2023-12-03 23:20:34 27 4
gpt4 key购买 nike

如何使用高斯内核在 Tensorflow 中实现 2D 低通(也称为模糊)滤波器?

最佳答案

首先定义一个归一化的二维高斯核:

def gaussian_kernel(size: int,
mean: float,
std: float,
):
"""Makes 2D gaussian Kernel for convolution."""

d = tf.distributions.Normal(mean, std)

vals = d.prob(tf.range(start = -size, limit = size + 1, dtype = tf.float32))

gauss_kernel = tf.einsum('i,j->ij',
vals,
vals)

return gauss_kernel / tf.reduce_sum(gauss_kernel)

接下来,使用 tf.nn.conv2d 将此内核与图像卷积:
# Make Gaussian Kernel with desired specs.
gauss_kernel = gaussian_kernel( ... )

# Expand dimensions of `gauss_kernel` for `tf.nn.conv2d` signature.
gauss_kernel = gauss_kernel[:, :, tf.newaxis, tf.newaxis]

# Convolve.
tf.nn.conv2d(image, gauss_kernel, strides=[1, 1, 1, 1], padding="SAME")

关于tensorflow - 如何在 Tensorflow 中制作二维高斯滤波器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52012657/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com