diff --git a/src/filters.py b/src/filters.py index 917f8a6..e9cf5a2 100644 --- a/src/filters.py +++ b/src/filters.py @@ -31,7 +31,7 @@ class gaussian(img_filter): Easier to use than opencv version. ''' - def __init__(self, img): + def __init__(self, img): super().__init__(img) def apply(self, params): @@ -41,6 +41,7 @@ class gaussian(img_filter): self.img = skiflt.gaussian( self.img, sigma=sigma, preserve_range=True) + self.img = np.uint8(self.img) class median(img_filter): @@ -56,7 +57,9 @@ class median(img_filter): # Used kernel is disk of size ksize ksize = int(params["ksize"]) if params["ksize"] else 3 + self.img = skiflt.median(self.img, footprint=skimorph.disk(ksize)) + self.img = np.uint8(self.img) class bilateral(img_filter): @@ -175,6 +178,8 @@ class block_match(img_filter): self.img = bm3d.bm3d(self.img, sigma_psd=sigma, stage_arg=bm3d.BM3DStages.ALL_STAGES) + self.img = np.uint8(self.img) + class unsharp_mask_scikit(img_filter): @@ -199,6 +204,30 @@ class unsharp_mask_scikit(img_filter): amount=amount, channel_axis=None) self.img = np.uint8(self.img * 255.0) # converting back to uint + +class unsharp_mask_pil(img_filter): + ''' Unsharp mask filter from PIL. + ''' + + def __init__(self, img): + super().__init__(img) + + def apply(self, params): + + # Blur radius + radius = int(params["radius"]) if params["radius"] else 2 + + # Unsharp strength in percent + percent = int(params["percent"]) if params["percent"] else 150 + + # Threshold controls the minimum brightness change that will be sharpened + threshold = int(params["threshold"]) if params["threshold"] else 3 + + self.img = np.uint8(self.img) + tmp = Image.fromarray(self.img) + tmp = tmp.filter(ImageFilter.UnsharpMask(radius, percent, threshold)) + self.img = np.uint8(np.asarray(tmp)) + # ------------------- EDGE DETECTION FILTERS -------------------# @@ -213,6 +242,8 @@ class farid(img_filter): def apply(self, _): self.img = skiflt.farid(self.img) + self.img = np.uint8(self.img) + # ------------------ RIDGE EXTRACTION FILTERS ------------------# @@ -270,6 +301,8 @@ class invert(img_filter): def apply(self, _): self.img = cv.bitwise_not(self.img) + self.img = np.uint8(self.img) + class scale_values(img_filter): @@ -290,6 +323,7 @@ class scale_values(img_filter): self.img = cv.bitwise_not(tmp.astype(np.uint8)) coef = 255 / np.max(tmp) tmp = tmp * coef + self.img = np.uint8(tmp) class binarize(img_filter): @@ -300,6 +334,8 @@ class binarize(img_filter): threshold = int(params["threshold"]) if params["threshold"] else 128 self.img = cv.threshold(self.img, threshold, 255, cv.THRESH_BINARY)[1] + self.img = np.uint8(self.img) + class binarize_otsu(img_filter): @@ -310,6 +346,8 @@ class binarize_otsu(img_filter): def apply(self, _): self.img = cv.threshold(self.img, 0, 255, cv.THRESH_BINARY + cv.THRESH_OTSU)[1] + self.img = np.uint8(self.img) + class add_margin(img_filter): @@ -325,6 +363,8 @@ class add_margin(img_filter): self.img = cv.copyMakeBorder( self.img, margin, margin, margin, margin, cv.BORDER_CONSTANT, value=color) self.height, self.width = self.img.shape + self.img = np.uint8(self.img) + # ---------------------- MORPHOLOGICAL OPS --------------------------# @@ -345,6 +385,8 @@ class erode(img_filter): self.img = cv.morphologyEx( np.uint8(self.img), op=cv.MORPH_ERODE, kernel=kernel) + self.img = np.uint8(self.img) + class dilate(img_filter): ''' General morphological operations from OpenCV. @@ -363,32 +405,4 @@ class dilate(img_filter): self.img = cv.morphologyEx( np.uint8(self.img), op=cv.MORPH_DILATE, kernel=kernel) - - -# ---------------------- OLD --------------------------# - - -# TODO: REVISE, REMOVE unused filters - -class unsharp_mask_pil(img_filter): - ''' Unsharp mask filter from PIL. - ''' - - def __init__(self, img): - super().__init__(img) - - def apply(self, params): - - # Blur radius - radius = int(params["radius"]) if params["radius"] else 2 - - # Unsharp strength in percent - percent = int(params["percent"]) if params["percent"] else 150 - - # Threshold controls the minimum brightness change that will be sharpened - threshold = int(params["threshold"]) if params["threshold"] else 3 - self.img = np.uint8(self.img) - tmp = Image.fromarray(self.img) - tmp = tmp.filter(ImageFilter.UnsharpMask(radius, percent, threshold)) - self.img = np.asarray(tmp)