Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyTorch中的高斯滤波器_Python_Pytorch - Fatal编程技术网

Python PyTorch中的高斯滤波器

Python PyTorch中的高斯滤波器,python,pytorch,Python,Pytorch,我正在寻找一种仅使用PyTorch函数将高斯滤波器应用于图像张量的方法。使用numpy,等效代码为 import numpy as np from scipy import signal import matplotlib.pyplot as plt # Define 2D Gaussian kernel def gkern(kernlen=256, std=128): """Returns a 2D Gaussian kernel array.""" gkern1d = si

我正在寻找一种仅使用PyTorch函数将高斯滤波器应用于图像张量的方法。使用numpy,等效代码为

import numpy as np
from scipy import signal
import matplotlib.pyplot as plt

# Define 2D Gaussian kernel
def gkern(kernlen=256, std=128):
    """Returns a 2D Gaussian kernel array."""
    gkern1d = signal.gaussian(kernlen, std=std).reshape(kernlen, 1)
    gkern2d = np.outer(gkern1d, gkern1d)
    return gkern2d

# Generate random matrix and multiply the kernel by it
A = np.random.rand(256*256).reshape([256,256])

# Test plot
plt.figure()
plt.imshow(A*gkern(256, std=32))
plt.show()
我发现的最接近的建议基于:

但是它给了我一个错误名称error:name‘gaussian_weights’没有定义。我怎样才能让它工作


是的,我也有同样的想法。现在的问题是:有没有一种方法可以定义高斯核或二维高斯核,而不使用Numpy和/或明确指定权重

是的,很简单。只需查看的功能文档。有一个链接到。因此,该方法的作用如下:

def gaussian(M, std, sym=True):
    if M < 1:
        return np.array([])
    if M == 1:
        return np.ones(1, 'd')
    odd = M % 2
    if not sym and not odd:
        M = M + 1
    n = np.arange(0, M) - (M - 1.0) / 2.0
    sig2 = 2 * std * std
    w = np.exp(-n ** 2 / sig2)
    if not sym and not odd:
        w = w[:-1]
    return w
你很幸运,因为在Pytork中转换非常简单,几乎只需用torch替换np,你就完成了

此外,请注意,火炬中的np.外部等效物为


是的,我也有同样的想法。现在的问题是:有没有一种方法可以定义高斯核或二维高斯核,而不使用Numpy和/或明确指定权重

是的,很简单。只需查看的功能文档。有一个链接到。因此,该方法的作用如下:

def gaussian(M, std, sym=True):
    if M < 1:
        return np.array([])
    if M == 1:
        return np.ones(1, 'd')
    odd = M % 2
    if not sym and not odd:
        M = M + 1
    n = np.arange(0, M) - (M - 1.0) / 2.0
    sig2 = 2 * std * std
    w = np.exp(-n ** 2 / sig2)
    if not sym and not odd:
        w = w[:-1]
    return w
你很幸运,因为在Pytork中转换非常简单,几乎只需用torch替换np,你就完成了


另外,注意torch中的np.outer等价物是。

我认为gaussian_权重应该是由gaussian制成的内核,就像scipy.signal.gaussianYupp一样,我也有同样的想法。现在的问题是:有没有一种方法可以定义高斯核或二维高斯核而不使用Numpy和/或显式指定权重?也许这可以帮助youpytorch实现:我认为高斯权重应该是由高斯核制成的,就像scipy.signal.gaussianYupp一样,我也有同样的想法。现在的问题是:有没有一种方法可以定义高斯核或二维高斯核而不使用Numpy和/或显式指定权重?也许这有助于youpytorch的实现: