在张量流中实现高通滤波器

时间:2017-04-07 13:33:41

标签: tensorflow convolution

我需要从张量流中提取高频率的图像。 基本上来自ndimage.gaussian_filter(img, sigma)的功能 以下代码按预期工作:

import tensorflow as tf
import cv2
img = cv2.imread(imgpath, cv2.IMREAD_GRAYSCALE)
img = cv2.normalize(img.astype('float32'), None, 0.0, 1.0, cv2.NORM_MINMAX)

# Gaussian Filter
K = np.array([[0.003765,0.015019,0.023792,0.015019,0.003765],
[0.015019,0.059912,0.094907,0.059912,0.015019],
[0.023792,0.094907,0.150342,0.094907,0.023792],
[0.015019,0.059912,0.094907,0.059912,0.015019],
[0.003765,0.015019,0.023792,0.015019,0.003765]], dtype='float32')

# as tensorflow constants with correct shapes
x = tf.constant(img.reshape(1,img.shape[0],img.shape[1], 1))
w = tf.constant(K.reshape(K.shape[0],K.shape[1], 1, 1))


with tf.Session() as sess:
    # get low/high pass ops
    lowpass = tf.nn.conv2d(x, w, strides=[1, 1, 1, 1], padding='SAME')
    highpass = x-lowpass

    # get high pass image
    l = sess.run(highpass)
    l = l.reshape(img.shape[0],img.shape[1])

    imshow(l)

但是我不知道如何使用给定的sigma在tensorflow中形成高斯权重。

1 个答案:

答案 0 :(得分:0)

只需参考此tflearn数据扩充 - http://tflearn.org/data_augmentation/,您可以在这里找到 add_random_blur(sigma_max = 5.0),它通过应用带随机sigma的高斯滤波器(0,随机模糊图像) sigma_max)。