Skip to content

Commit

Permalink
Gaussian Kernel and Derivative of Gaussian Kernel
Browse files Browse the repository at this point in the history
  • Loading branch information
pm3310 committed Nov 8, 2016
1 parent 23cb466 commit 96b9c87
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
Empty file added kernels/__init__.py
Empty file.
78 changes: 78 additions & 0 deletions kernels/kernels.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
from skimage import io

import numpy
from PIL import Image
from scipy import signal, array

from numpy import mgrid, exp


def gauss_kernel(size, size_y=None):
""" Returns a normalized 2D gauss kernel array for convolutions """
size = int(size)
if not size_y:
size_y = size
else:
size_y = int(size_y)

x, y = mgrid[-size: size + 1, -size_y: size_y + 1]

g = exp(-(x ** 2 / float(size) + y ** 2 / float(size_y)))
return g / g.sum()


def gauss_derivative_kernels(size, size_y=None):
""" returns x and y derivatives of a 2D
gauss kernel array for convolutions """
size = int(size)
if not size_y:
size_y = size
else:
size_y = int(size_y)
y, x = mgrid[-size: size + 1, -size_y: size_y + 1]

# x and y derivatives of a 2D gaussian with standard dev half of size
# (ignore scale factor)
gx = - x * exp(-(x ** 2 / float((0.5 * size) ** 2) + y ** 2 / float((0.5 * size_y) ** 2)))
gy = - y * exp(-(x ** 2 / float((0.5 * size) ** 2) + y ** 2 / float((0.5 * size_y) ** 2)))

return gx, gy


def gauss_derivatives(im, n, ny=None):
""" returns x and y derivatives of an image using gaussian
derivative filters of size n. The optional argument
ny allows for a different size in the y direction."""

gx, gy = gauss_derivative_kernels(n, size_y=ny)

imx = signal.convolve(im, gx, mode='same')
imy = signal.convolve(im, gy, mode='same')

return imx, imy


if __name__ == '__main__':
im = array(
Image.open('path_to_original_image').convert('L')
)

# Calculate the derivatives in x and y axis
# using the gauss derivative.
# You change the window size.
x, y = gauss_derivatives(im, 3)

x_min = x.min()
x_max = x.max()

x_final = (x - x_min) / (x_max - x_min)

y_min = y.min()
y_max = y.max()

y_final = (y - y_min) / (y_max - y_min)

# Concat the x and y derivative images
combined = numpy.concatenate((x_final, y_final), axis=1)

io.imsave('path_to_new_image', combined)
9 changes: 9 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
dask==0.12.0
decorator==4.0.10
networkx==1.11
numpy==1.11.2
Pillow==3.4.2
scikit-image==0.12.3
scikit-learn==0.18
scipy==0.18.1
six==1.10.0
toolz==0.8.0

0 comments on commit 96b9c87

Please sign in to comment.