|
|
|
@ -8,12 +8,15 @@ import matplotlib.pyplot as plt
|
|
|
|
|
import cv2 as cv
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Parent class for all the filters
|
|
|
|
|
class filter:
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
self.input_file = input_file
|
|
|
|
|
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)
|
|
|
|
@ -23,7 +26,7 @@ class filter_none(filter):
|
|
|
|
|
print("No filter selected")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_average(filter):
|
|
|
|
|
class average(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
@ -31,40 +34,40 @@ class filter_average(filter):
|
|
|
|
|
kernel = np.ones((5, 5), np.float32) / 25
|
|
|
|
|
self.img = cv.filter2D(self.img, -1, kernel)
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Average")
|
|
|
|
|
print("Applying average filter")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_blur(filter):
|
|
|
|
|
class blur(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
|
def apply(self):
|
|
|
|
|
self.img = cv.blur(self.img, (5, 5))
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Normal blur")
|
|
|
|
|
print("Applying normal blur")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_gaussian(filter):
|
|
|
|
|
class gaussian(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
|
def apply(self):
|
|
|
|
|
self.img = cv.GaussianBlur(self.img, (5, 5), 0)
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Gaussian blur")
|
|
|
|
|
print("Applying Gaussian blur")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_median(filter):
|
|
|
|
|
class 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")
|
|
|
|
|
print("Applying median blur")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_erode(filter):
|
|
|
|
|
class erode(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
@ -72,10 +75,10 @@ class filter_erode(filter):
|
|
|
|
|
kernel = np.ones((5, 5), np.uint8)
|
|
|
|
|
self.img = cv.erode(self.img, kernel, 1)
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Erosion")
|
|
|
|
|
print("Applying erosion")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_dilate(filter):
|
|
|
|
|
class dilate(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
@ -83,10 +86,11 @@ class filter_dilate(filter):
|
|
|
|
|
kernel = np.ones((5, 5), np.uint8)
|
|
|
|
|
self.img = cv.dilate(self.img, kernel, 1)
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Dilatation")
|
|
|
|
|
print("Applying dilatation")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class filter_opening(filter):
|
|
|
|
|
# Erosion, then dilatation
|
|
|
|
|
class opening(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
@ -94,9 +98,11 @@ class filter_opening(filter):
|
|
|
|
|
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
|
|
|
|
|
self.img = cv.morphologyEx(self.img, cv.MORPH_CLOSE, kernel)
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Opening")
|
|
|
|
|
print("Applying opening")
|
|
|
|
|
|
|
|
|
|
class filter_closing(filter):
|
|
|
|
|
|
|
|
|
|
# Dilatation, then erosion
|
|
|
|
|
class closing(filter):
|
|
|
|
|
def __init__(self, input_file, output_file, img):
|
|
|
|
|
super().__init__(input_file, output_file, img)
|
|
|
|
|
|
|
|
|
@ -104,4 +110,4 @@ class filter_closing(filter):
|
|
|
|
|
kernel = cv.getStructuringElement(cv.MORPH_RECT, (3, 3))
|
|
|
|
|
self.img = cv.morphologyEx(self.img, cv.MORPH_OPEN, kernel)
|
|
|
|
|
plt.imshow(self.img)
|
|
|
|
|
print("Closing")
|
|
|
|
|
print("Applying closing")
|
|
|
|
|