From 3c0d15f8836f462e926cb1e7e10daef9d2a6bb5f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rostislav=20L=C3=A1n?= Date: Wed, 7 Dec 2022 23:44:58 +0100 Subject: [PATCH] Added more opencv image processing filters. --- src/filters.py | 61 ++++++++++++++++++++++++++++++++++++++++++++++---- src/main.py | 10 +++++++++ 2 files changed, 67 insertions(+), 4 deletions(-) diff --git a/src/filters.py b/src/filters.py index 3e22cc3..7b4bdca 100644 --- a/src/filters.py +++ b/src/filters.py @@ -28,10 +28,10 @@ class filter_average(filter): super().__init__(input_file, output_file, img) def apply(self): - kernel = np.ones((5, 5), np.float32)/25 + kernel = np.ones((5, 5), np.float32) / 25 self.img = cv.filter2D(self.img, -1, kernel) plt.imshow(self.img) - print("Average filter") + print("Average") class filter_blur(filter): @@ -41,7 +41,7 @@ class filter_blur(filter): def apply(self): self.img = cv.blur(self.img, (5, 5)) plt.imshow(self.img) - print("Blurring filter") + print("Normal blur") class filter_gaussian(filter): @@ -51,4 +51,57 @@ class filter_gaussian(filter): def apply(self): self.img = cv.GaussianBlur(self.img, (5, 5), 0) plt.imshow(self.img) - print("Gaussian filter") + print("Gaussian blur") + + +class filter_median(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + self.img = cv.medianBlur(self.img, 5) + plt.imshow(self.img) + print("Median blur") + + +class filter_erode(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + kernel = np.ones((5, 5), np.uint8) + self.img = cv.erode(self.img, kernel, 1) + plt.imshow(self.img) + print("Erosion") + + +class filter_dilate(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + kernel = np.ones((5, 5), np.uint8) + self.img = cv.dilate(self.img, kernel, 1) + plt.imshow(self.img) + print("Dilatation") + + +class filter_opening(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) + self.img = cv.morphologyEx(self.img, cv.MORPH_CLOSE, kernel) + plt.imshow(self.img) + print("Opening") + +class filter_closing(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3)) + self.img = cv.morphologyEx(self.img, cv.MORPH_OPEN, kernel) + plt.imshow(self.img) + print("Closing") diff --git a/src/main.py b/src/main.py index d424219..8226387 100644 --- a/src/main.py +++ b/src/main.py @@ -64,6 +64,16 @@ class apply_filters: return flt.filter_blur elif filter_name == "gaussian": return flt.filter_gaussian + elif filter_name == "median": + return flt.filter_median + elif filter_name == "erode": + return flt.filter_erode + elif filter_name == "dilate": + return flt.filter_dilate + elif filter_name == "opening": + return flt.filter_opening + elif filter_name == "closing": + return flt.filter_closing else: raise ValueError("Invalid filter name")