aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/filters
diff options
context:
space:
mode:
Diffstat (limited to 'filters')
-rw-r--r--filters/__init__.py0
-rw-r--r--filters/convolution.py16
-rw-r--r--filters/kernel/__init__.py0
-rw-r--r--filters/kernel/box_blur.py10
-rw-r--r--filters/kernel/gaussian_blur.py15
-rw-r--r--filters/kernel/shift.py49
6 files changed, 90 insertions, 0 deletions
diff --git a/filters/__init__.py b/filters/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/filters/__init__.py
diff --git a/filters/convolution.py b/filters/convolution.py
new file mode 100644
index 0000000..0a4cf9e
--- /dev/null
+++ b/filters/convolution.py
@@ -0,0 +1,16 @@
+# Copyright 2016 (c) Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Simple image filters" project.
+# For details, see https://github.com/egor-tensin/filters.
+# Distributed under the MIT License.
+
+import numpy as np
+
+def convolve(img, kernel):
+ #print(kernel)
+ radius = kernel.shape[0] // 2
+ output = np.zeros(img.shape, dtype=img.dtype)
+ for i in range(radius, img.shape[0] - radius):
+ for j in range(radius, img.shape[1] - radius):
+ neighborhood = img[i - radius:i + radius + 1, j - radius:j + radius + 1]
+ output[i, j] = np.sum(neighborhood * kernel)
+ return output
diff --git a/filters/kernel/__init__.py b/filters/kernel/__init__.py
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/filters/kernel/__init__.py
diff --git a/filters/kernel/box_blur.py b/filters/kernel/box_blur.py
new file mode 100644
index 0000000..b9c5d60
--- /dev/null
+++ b/filters/kernel/box_blur.py
@@ -0,0 +1,10 @@
+# Copyright 2016 (c) Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Simple image filters" project.
+# For details, see https://github.com/egor-tensin/filters.
+# Distributed under the MIT License.
+
+import numpy as np
+
+def gen_kernel(radius):
+ size = radius * 2 + 1
+ return np.ones((size, size)) / size ** 2
diff --git a/filters/kernel/gaussian_blur.py b/filters/kernel/gaussian_blur.py
new file mode 100644
index 0000000..5557b79
--- /dev/null
+++ b/filters/kernel/gaussian_blur.py
@@ -0,0 +1,15 @@
+# Copyright 2016 (c) Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Simple image filters" project.
+# For details, see https://github.com/egor-tensin/filters.
+# Distributed under the MIT License.
+
+import numpy as np
+
+def gen_kernel(radius, sigma):
+ kernel = np.array([[i ** 2 + j ** 2
+ for i in range(-radius, radius + 1)]
+ for j in range(-radius, radius + 1)])
+ kernel = -kernel / (2 * sigma ** 2)
+ kernel = np.exp(kernel)
+ kernel = kernel / np.sum(kernel)
+ return kernel
diff --git a/filters/kernel/shift.py b/filters/kernel/shift.py
new file mode 100644
index 0000000..4e1e497
--- /dev/null
+++ b/filters/kernel/shift.py
@@ -0,0 +1,49 @@
+# Copyright 2016 (c) Egor Tensin <Egor.Tensin@gmail.com>
+# This file is part of the "Simple image filters" project.
+# For details, see https://github.com/egor-tensin/filters.
+# Distributed under the MIT License.
+
+from enum import Enum
+
+import numpy as np
+
+class Direction(Enum):
+ NORTH = 'N'
+ NORTH_EAST = 'NE'
+ NORTH_WEST = 'NW'
+ SOUTH = 'S'
+ SOUTH_EAST = 'SE'
+ SOUTH_WEST = 'SW'
+ EAST = 'E'
+ WEST = 'W'
+
+ def gen_kernel(self, distance):
+ radius = distance
+ size = 2 * radius + 1
+ kernel = np.zeros((size, size))
+ x, y = radius, radius
+
+ if self is Direction.NORTH:
+ x = -1
+ elif self is Direction.NORTH_EAST:
+ x, y = -1, 0
+ elif self is Direction.EAST:
+ y = 0
+ elif self is Direction.SOUTH_EAST:
+ x, y = 0, 0
+ elif self is Direction.SOUTH:
+ x = 0
+ elif self is Direction.SOUTH_WEST:
+ x, y = 0, -1
+ elif self is Direction.WEST:
+ y = -1
+ elif self is Direction.NORTH_WEST:
+ x, y = -1, -1
+ else:
+ raise NotImplementedError('unsupported direction: ' + str(self))
+
+ kernel[x, y] = 1
+ return kernel
+
+ def __str__(self):
+ return self.value