本文介绍了如何在Tensorflow中制作2D高斯滤波器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I implement a 2D low pass (also known as blurring) filter in Tensorflow using a gaussian kernel?

推荐答案

首先定义标准化的2D高斯核:

First define a normalized 2D gaussian kernel:

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将该内核与图像进行卷积:

Next, use tf.nn.conv2d to convolve this kernel with an image:

# 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中制作2D高斯滤波器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 00:20