diff --git a/Bildfaltung/BorderlineBehavior.py b/Bildfaltung/BorderBehavior.py similarity index 52% rename from Bildfaltung/BorderlineBehavior.py rename to Bildfaltung/BorderBehavior.py index 4dc7e06d28442eae2ef7c857498b1f8adb9fffc5..392d0655e76693b9accb91625e3848005afa3de1 100644 --- a/Bildfaltung/BorderlineBehavior.py +++ b/Bildfaltung/BorderBehavior.py @@ -1,7 +1,8 @@ from abc import ABC, abstractmethod class BorderBehavior(ABC): + @abstractmethod - def getPixelValue(i, j, image): - #CONTENT - return Integer + def getPixelValue(self, i, j, image): + pass + diff --git a/Bildfaltung/ClampingBorderBehavior.py b/Bildfaltung/ClampingBorderBehavior.py new file mode 100644 index 0000000000000000000000000000000000000000..bf23ee22b464998b839cebe26475a6a15f4a5ba5 --- /dev/null +++ b/Bildfaltung/ClampingBorderBehavior.py @@ -0,0 +1,24 @@ +from BorderBehavior import BorderBehavior +''' +ClampingBorderBehavior, welche für (i, j) ausserhalb des Bildes, +wie in Abbildung 2 unten dargestellt, den Wert des am nächsten +gelegenen Randpixels zurückgibt (2p) +''' + + +def clamp(num, min_value, max_value): + num = max(min(num, max_value), min_value) + return num + +class ClampingBorderBehavior(BorderBehavior): + + def getPixelValue(self, i, j, image): + + try: + return image[i][j] + except IndexError: + clamped_i = clamp(i, 0, image.shape[1]-1) + clamped_j = clamp(j, 0, image.shape[0]-1) + return image[clamped_i][clamped_j] + + diff --git a/Bildfaltung/KernelFactory.py b/Bildfaltung/KernelFactory.py new file mode 100644 index 0000000000000000000000000000000000000000..0901e12c0542267468fb173ac355eef01ba14310 --- /dev/null +++ b/Bildfaltung/KernelFactory.py @@ -0,0 +1,24 @@ +class KernelFactory: + + @staticmethod + def createVerticalPrewittKernel(): + verticalPrewittKernel = [[-1, -1, -1], + [0, 0, 0], + [1, 1, 1]] + return verticalPrewittKernel + + @staticmethod + def createHorizontalPrewittKernel(): + horizontalPrewittKernel = [[-1, 0, 1], + [-1, 0, 1], + [-1, 0, 1]] + return horizontalPrewittKernel + + @staticmethod + def createBoxFilter(size): + value = 1/(size*size) + boxFilter = [[value for x in range(size)] for y in range(size)] + + return boxFilter + + diff --git a/Bildfaltung/ZeroPaddingBorderBehavior.py b/Bildfaltung/ZeroPaddingBorderBehavior.py index cadc2f0a304061160cb7f0c05cc426e8a72a1124..48b7aeb05baf3b17d4352936d58df7cef7c87f4e 100644 --- a/Bildfaltung/ZeroPaddingBorderBehavior.py +++ b/Bildfaltung/ZeroPaddingBorderBehavior.py @@ -1,3 +1,19 @@ -class ZeroPaddingBorderBehavior: +from BorderBehavior import BorderBehavior +''' +ZeroPaddingBorderBehavior, welche für (i, j) ausserhalb des +Bildes, wie in Abbildung 2 oben dargestellt, den Wert 0 zurückgibt +(2p). +''' + +class ZeroPaddingBorderBehavior(BorderBehavior): + + def getPixelValue(self, i, j, image): + try: + return image[i][j] + except IndexError: + return 0 + + + + - def get_pixel_value(i, j, img): diff --git a/Bildfaltung/__pycache__/BorderBehavior.cpython-310.pyc b/Bildfaltung/__pycache__/BorderBehavior.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..7a919095ae9017d104c5846ef7bdaf9cac22d9f7 Binary files /dev/null and b/Bildfaltung/__pycache__/BorderBehavior.cpython-310.pyc differ diff --git a/Bildfaltung/__pycache__/ClampingBorderBehavior.cpython-310.pyc b/Bildfaltung/__pycache__/ClampingBorderBehavior.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..43a303511bdcafa64288b3725d9c70978953e005 Binary files /dev/null and b/Bildfaltung/__pycache__/ClampingBorderBehavior.cpython-310.pyc differ diff --git a/Bildfaltung/__pycache__/KernelFactory.cpython-310.pyc b/Bildfaltung/__pycache__/KernelFactory.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..66ad584ce838b1fa84b7a5fbd9875f888ed4136a Binary files /dev/null and b/Bildfaltung/__pycache__/KernelFactory.cpython-310.pyc differ diff --git a/Bildfaltung/__pycache__/ZeroPaddingBorderBehavior.cpython-310.pyc b/Bildfaltung/__pycache__/ZeroPaddingBorderBehavior.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..36c1eb5b7789b3435a960975f3d6ae3990801e06 Binary files /dev/null and b/Bildfaltung/__pycache__/ZeroPaddingBorderBehavior.cpython-310.pyc differ diff --git a/Bildfaltung/main.py b/Bildfaltung/main.py index d40e22e1868ab19a13a4f8520d696e963e4f67b0..61e12d0ab1bd127f86c9663ad3067b403f591263 100644 --- a/Bildfaltung/main.py +++ b/Bildfaltung/main.py @@ -1,17 +1,40 @@ +from ClampingBorderBehavior import ClampingBorderBehavior from Image import Image import numpy as np +from KernelFactory import KernelFactory +from ZeroPaddingBorderBehavior import ZeroPaddingBorderBehavior path = "E:/Downloads/test.pgm" path1 = "E:/Downloads/test1.pgm" array = np.random.randint(256, size=(4, 4)) +def clamp(num, min_value, max_value): + num = max(min(num, max_value), min_value) + return num + if __name__ == '__main__': print(array) - img = Image() - img.writeToFile(array, path1) - print(img.readFromFile(path1)) + #img = Image() + #img.writeToFile(array, path1) + #print(img.readFromFile(path1)) + test = ClampingBorderBehavior() + print(test.getPixelValue(1, 4, array)) + + + + + + + #print(KernelFactory.createBoxFilter(5)) + + + + + + +