From cc5e39e53c7e4d7b326647351b035c7ef01c06e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rostislav=20L=C3=A1n?= Date: Sun, 25 Dec 2022 17:50:22 +0100 Subject: [PATCH] Added unsharp mask filter --- src/filters.py | 50 ++++++++++++++++++++++++++++++++++---------------- 1 file changed, 34 insertions(+), 16 deletions(-) diff --git a/src/filters.py b/src/filters.py index fc1b849..b4769d8 100644 --- a/src/filters.py +++ b/src/filters.py @@ -13,20 +13,23 @@ class filter: ''' Parent class for all the filters ''' + def __init__(self, img): self.img = img class convolve(filter): ''' Convolve using custom kernel, - if no kernel is given, use default 3x3 kernel for averaging''' + if no kernel is given, use default 3x3 kernel for averaging + ''' + def __init__(self, img): super().__init__(img) def apply(self, params): - # Set default values ksize = int(params["ksize"]) if params["ksize"] else 3 - kernel = np.array(params["kernel"]) if params["kernel"] else np.ones((ksize, ksize), np.float32) / np.sqrt(ksize) + kernel = np.array(params["kernel"]) if params["kernel"] else np.ones( + (ksize, ksize), np.float32) / np.sqrt(ksize) #print("with params: " + " ksize: " + str(ksize) + " kernel: \n" + str(kernel)) self.img = cv.filter2D(self.img, -1, kernel) @@ -37,7 +40,6 @@ class blur(filter): super().__init__(img) def apply(self, params): - # Set default values if(params["anchor"]): try: anchor = tuple(map(int, params["anchor"].split(','))) @@ -47,16 +49,15 @@ class blur(filter): anchor = (-1, -1) ksize = int(params["ksize"]) if params["ksize"] else 3 - #print("with params: " + " ksize: " + str(ksize) + " anchor: " + str(anchor)) self.img = cv.blur(self.img, ksize=(ksize, ksize), anchor=anchor) + class gaussian(filter): def __init__(self, img): super().__init__(img) def apply(self, params): - # Set default values ksize = int(params["ksize"]) if params["ksize"] else 3 sigmaX = float(params["sigmaX"]) if params["sigmaX"] else 0 sigmaY = float(params["sigmaY"]) if params["sigmaY"] else 0 @@ -70,11 +71,10 @@ class median(filter): super().__init__(img) def apply(self, params): - # Set default values ksize = int(params["ksize"]) if params["ksize"] else 3 #print("with params: " + " ksize: " + str(ksize)) - self.img = cv.medianBlur(self.img, ksize) + self.img = cv.medianBlur(np.uint8(self.img), ksize) class bilateral(filter): @@ -96,10 +96,11 @@ class denoise(filter): super().__init__(img) def apply(self, params): - # Set default values h = int(params["h"]) if params["h"] else 20 - tWS = int(params["templateWindowSize"]) if params["templateWindowSize"] else 7 - sWS = int(params["searchWindowSize"]) if params["searchWindowSize"] else 21 + tWS = int(params["templateWindowSize"] + ) if params["templateWindowSize"] else 7 + sWS = int(params["searchWindowSize"] + ) if params["searchWindowSize"] else 21 #print("with params: " + " h: " + str(h) + " tWS: " + str(tWS) + " sWS: " + str(sWS)) self.img = np.uint8(self.img) @@ -111,7 +112,6 @@ class sharpen(filter): super().__init__(img) def apply(self, params): - # Set default values kernel = np.matrix(params["kernel"]) if params["kernel"] else np.array( [[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) @@ -119,17 +119,36 @@ class sharpen(filter): self.img = cv.filter2D(self.img, ddepth=-1, kernel=kernel) +class unsharp_mask(filter): + ''' Unsharp mask filter. + + First blur the image a little bit, then calculate Laplacian of the image to get the edges. + Scale the Laplacian and subtract it from the original image. + ''' + + def __init__(self, img): + super().__init__(img) + + def apply(self, params): + strength = float(params["strength"]) if params["strength"] else 1.0 + ksize = int(params["ksize"]) if params["ksize"] else 3 + blurred = cv.medianBlur(np.uint8(self.img), ksize) + lap = cv.Laplacian(blurred, cv.CV_32F) + self.img = blurred - strength*lap + + class morph(filter): ''' General morphological operations. - Can be used with MORPH_OPEN, MORPH_CLOSE, MORPH_DILATE, MORPH_ERODE as op. + Can be used with MORPH_OPEN, MORPH_CLOSE, MORPH_DILATE, MORPH_ERODE and more as 'op'. ''' + def __init__(self, img): super().__init__(img) def apply(self, params): - # Set default values - kernel = np.matrix(params["kernel"]) if params["kernel"] else np.ones((3, 3), np.uint8) + kernel = np.matrix(params["kernel"]) if params["kernel"] else np.ones( + (3, 3), np.uint8) iterations = int(params["iterations"]) if params["iterations"] else 1 op = getattr(cv, params["op"]) if params["op"] else cv.MORPH_OPEN if(params["anchor"]): @@ -140,7 +159,6 @@ class morph(filter): else: anchor = (-1, -1) - #print("with params: " + " kernel: \n" + str(kernel) + " anchor: " + str(anchor) + " iterations: " + str(iterations) + " op: " + str(op)) self.img = cv.morphologyEx( self.img, op=op, kernel=kernel, anchor=anchor, iterations=iterations)