diff --git a/src/filters.py b/src/filters.py index 1f1de0e..e07fafe 100644 --- a/src/filters.py +++ b/src/filters.py @@ -4,7 +4,7 @@ """ import numpy as np -import matplotlib.pyplot as plt +#import matplotlib.pyplot as plt import cv2 as cv @@ -15,26 +15,14 @@ class filter: self.output_file = output_file self.img = img -# Used when no other filters are present, plots original image, -# may not be necessary -class filter_none(filter): - def __init__(self, input_file, output_file, img): - super().__init__(input_file, output_file, img) - - def apply(self): - plt.imshow(self.img) - print("No filter selected") - class average(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.float32) / 25 + kernel = np.ones((3, 3), np.float32) / 9 self.img = cv.filter2D(self.img, -1, kernel) - plt.imshow(self.img) - print("Applying average filter") class blur(filter): @@ -42,9 +30,7 @@ class blur(filter): super().__init__(input_file, output_file, img) def apply(self): - self.img = cv.blur(self.img, (5, 5)) - plt.imshow(self.img) - print("Applying normal blur") + self.img = cv.blur(self.img, (3, 3)) class gaussian(filter): @@ -52,9 +38,7 @@ class gaussian(filter): super().__init__(input_file, output_file, img) def apply(self): - self.img = cv.GaussianBlur(self.img, (5, 5), 0) - plt.imshow(self.img) - print("Applying Gaussian blur") + self.img = cv.GaussianBlur(self.img, (3, 3), 0) class median(filter): @@ -62,9 +46,35 @@ class median(filter): super().__init__(input_file, output_file, img) def apply(self): - self.img = cv.medianBlur(self.img, 5) - plt.imshow(self.img) - print("Applying median blur") + self.img = cv.medianBlur(self.img, 1) + + +class bilateral(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + self.img = cv.bilateralFilter(self.img, 1, 75, 75) + + +class denoise(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + pass + #does not work + #img = cv.cvtColor(self.img, cv.COLOR_GRAY2BGR) + #self.img = cv.fastNlMeansDenoisingColored(img, h=3) + + +class sharpen(filter): + def __init__(self, input_file, output_file, img): + super().__init__(input_file, output_file, img) + + def apply(self): + kernel = np.array([[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) + self.img = cv.filter2D(self.img, ddepth=-1, kernel=kernel) class erode(filter): @@ -72,10 +82,8 @@ class erode(filter): super().__init__(input_file, output_file, img) def apply(self): - kernel = np.ones((5, 5), np.uint8) + kernel = np.ones((3, 3), np.uint8) self.img = cv.erode(self.img, kernel, 1) - plt.imshow(self.img) - print("Applying erosion") class dilate(filter): @@ -83,10 +91,8 @@ class dilate(filter): super().__init__(input_file, output_file, img) def apply(self): - kernel = np.ones((5, 5), np.uint8) + kernel = np.ones((3, 3), np.uint8) self.img = cv.dilate(self.img, kernel, 1) - plt.imshow(self.img) - print("Applying dilatation") # Erosion, then dilatation @@ -97,8 +103,6 @@ class opening(filter): 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("Applying opening") # Dilatation, then erosion @@ -109,5 +113,3 @@ class closing(filter): 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("Applying closing")