-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gaussian Kernel and Derivative of Gaussian Kernel
- Loading branch information
Showing
3 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |