def gauss_kernel(size, sigma=None, size_y=None, sigma_y=None):
"""
Generates a 2D Gaussian kernel as a numpy array
Args:
size (int): 1/2 the width of the kernel; total width := 2*size+1
sigma (float): spread of the gaussian in the width direction
size_y (int): 1/2 the height of the kernel; defaults to size
sigma_y (float): spread of the gaussian in the height direction; defaults to sigma
Returns:
numpy array: normalized 2D gaussian array
"""
size = int(size)
if not size_y:
size_y = size
else:
size_y = int(size_y)
if not sigma:
sigma = 0.5 * size + .1
if not sigma_y:
sigma_y = sigma
x, y = np.mgrid[-size:size+1, -size_y:size_y+1]
g = np.exp(-0.5 * (x ** 2 / sigma ** 2 + y ** 2 / sigma_y ** 2))
return g / g.sum()
评论列表
文章目录