Added unsharp mask filter

master
Rostislav Lán 2 years ago
parent 3f17c9607d
commit cc5e39e53c

@ -13,20 +13,23 @@ class filter:
''' '''
Parent class for all the filters Parent class for all the filters
''' '''
def __init__(self, img): def __init__(self, img):
self.img = img self.img = img
class convolve(filter): class convolve(filter):
''' Convolve using custom kernel, ''' 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): def __init__(self, img):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values
ksize = int(params["ksize"]) if params["ksize"] else 3 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)) #print("with params: " + " ksize: " + str(ksize) + " kernel: \n" + str(kernel))
self.img = cv.filter2D(self.img, -1, kernel) self.img = cv.filter2D(self.img, -1, kernel)
@ -37,7 +40,6 @@ class blur(filter):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values
if(params["anchor"]): if(params["anchor"]):
try: try:
anchor = tuple(map(int, params["anchor"].split(','))) anchor = tuple(map(int, params["anchor"].split(',')))
@ -47,16 +49,15 @@ class blur(filter):
anchor = (-1, -1) anchor = (-1, -1)
ksize = int(params["ksize"]) if params["ksize"] else 3 ksize = int(params["ksize"]) if params["ksize"] else 3
#print("with params: " + " ksize: " + str(ksize) + " anchor: " + str(anchor)) #print("with params: " + " ksize: " + str(ksize) + " anchor: " + str(anchor))
self.img = cv.blur(self.img, ksize=(ksize, ksize), anchor=anchor) self.img = cv.blur(self.img, ksize=(ksize, ksize), anchor=anchor)
class gaussian(filter): class gaussian(filter):
def __init__(self, img): def __init__(self, img):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values
ksize = int(params["ksize"]) if params["ksize"] else 3 ksize = int(params["ksize"]) if params["ksize"] else 3
sigmaX = float(params["sigmaX"]) if params["sigmaX"] else 0 sigmaX = float(params["sigmaX"]) if params["sigmaX"] else 0
sigmaY = float(params["sigmaY"]) if params["sigmaY"] else 0 sigmaY = float(params["sigmaY"]) if params["sigmaY"] else 0
@ -70,11 +71,10 @@ class median(filter):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values
ksize = int(params["ksize"]) if params["ksize"] else 3 ksize = int(params["ksize"]) if params["ksize"] else 3
#print("with params: " + " ksize: " + str(ksize)) #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): class bilateral(filter):
@ -96,10 +96,11 @@ class denoise(filter):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values
h = int(params["h"]) if params["h"] else 20 h = int(params["h"]) if params["h"] else 20
tWS = int(params["templateWindowSize"]) if params["templateWindowSize"] else 7 tWS = int(params["templateWindowSize"]
sWS = int(params["searchWindowSize"]) if params["searchWindowSize"] else 21 ) 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)) #print("with params: " + " h: " + str(h) + " tWS: " + str(tWS) + " sWS: " + str(sWS))
self.img = np.uint8(self.img) self.img = np.uint8(self.img)
@ -111,7 +112,6 @@ class sharpen(filter):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values
kernel = np.matrix(params["kernel"]) if params["kernel"] else np.array( kernel = np.matrix(params["kernel"]) if params["kernel"] else np.array(
[[0, -1, 0], [-1, 5, -1], [0, -1, 0]]) [[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) 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): class morph(filter):
''' General morphological operations. ''' 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): def __init__(self, img):
super().__init__(img) super().__init__(img)
def apply(self, params): def apply(self, params):
# Set default values kernel = np.matrix(params["kernel"]) if params["kernel"] else np.ones(
kernel = np.matrix(params["kernel"]) if params["kernel"] else np.ones((3, 3), np.uint8) (3, 3), np.uint8)
iterations = int(params["iterations"]) if params["iterations"] else 1 iterations = int(params["iterations"]) if params["iterations"] else 1
op = getattr(cv, params["op"]) if params["op"] else cv.MORPH_OPEN op = getattr(cv, params["op"]) if params["op"] else cv.MORPH_OPEN
if(params["anchor"]): if(params["anchor"]):
@ -140,7 +159,6 @@ class morph(filter):
else: else:
anchor = (-1, -1) anchor = (-1, -1)
#print("with params: " + " kernel: \n" + str(kernel) + " anchor: " + str(anchor) + " iterations: " + str(iterations) + " op: " + str(op)) #print("with params: " + " kernel: \n" + str(kernel) + " anchor: " + str(anchor) + " iterations: " + str(iterations) + " op: " + str(op))
self.img = cv.morphologyEx( self.img = cv.morphologyEx(
self.img, op=op, kernel=kernel, anchor=anchor, iterations=iterations) self.img, op=op, kernel=kernel, anchor=anchor, iterations=iterations)

Loading…
Cancel
Save