diff --git a/src/L0Smoothing.py b/src/L0Smoothing.py index 051a781..529cf2a 100644 --- a/src/L0Smoothing.py +++ b/src/L0Smoothing.py @@ -13,6 +13,7 @@ import cv2 import numpy as np +from scipy.fftpack import fft2, ifft2 from psf2otf import psf2otf @@ -32,10 +33,8 @@ def __init__(self, img_path: str, def run(self): """L0 smoothing imlementation""" - img = cv2.imread(self._img_path) - S = cv2.normalize(img, None, alpha=0, beta=1, - norm_type=cv2.NORM_MINMAX, - dtype=cv2.CV_32F) + img = cv2.imread(self._img_path, 0) + S = img / 256 if S.ndim < 3: S = S[..., np.newaxis] @@ -49,7 +48,8 @@ def run(self): psf = np.asarray([[-1], [1]]) otfy = psf2otf(psf, out_size) - Normin1 = np.fft.fft2(np.squeeze(S), axes=(0, 1)) + # Normin1 = np.fft.fft2(np.squeeze(S), axes=(0, 1)) + Normin1 = fft2(np.squeeze(S), axes=(0, 1)) Denormin2 = np.square(abs(otfx)) + np.square(abs(otfy)) if D > 1: Denormin2 = Denormin2[..., np.newaxis] @@ -92,11 +92,13 @@ def run(self): v_diff = np.vstack([first_row, v_diff]) Normin2 = h_diff + v_diff - Normin2 = beta * np.fft.fft2(Normin2, axes=(0, 1)) + # Normin2 = beta * np.fft.fft2(Normin2, axes=(0, 1)) + Normin2 = beta * fft2(Normin2, axes=(0, 1)) FS = np.divide(np.squeeze(Normin1) + np.squeeze(Normin2), Denormin) - S = np.real(np.fft.ifft2(FS, axes=(0, 1))) + # S = np.real(np.fft.ifft2(FS, axes=(0, 1))) + S = np.real(ifft2(FS, axes=(0, 1))) if False: S_new = S * 256 S_new = S_new.astype(np.uint8) @@ -111,13 +113,12 @@ def run(self): if __name__ == "__main__": - img_path = './pflower.jpg' + img_path = './stripes_jet.png' img = cv2.imread(img_path) - S = L0Smoothing(img_path, param_lambda=0.01).run() + S = L0Smoothing(img_path, param_lambda=2.5e-1, param_kappa=1.05).run() S = np.squeeze(S) - S = cv2.normalize(S, None, alpha=0, beta=255, - norm_type=cv2.NORM_MINMAX, - dtype=cv2.CV_32F) + S = np.clip(S, 0, 1) + S = S * 255 S = S.astype(np.uint8) cv2.imshow('Input', img) cv2.imshow('L0-Smooth', S) diff --git a/src/basketball.png b/src/basketball.png new file mode 100644 index 0000000..5027b15 Binary files /dev/null and b/src/basketball.png differ diff --git a/src/output_python.png b/src/output_python.png new file mode 100644 index 0000000..268c00f Binary files /dev/null and b/src/output_python.png differ diff --git a/src/run_batch.py b/src/run_batch.py new file mode 100644 index 0000000..c742652 --- /dev/null +++ b/src/run_batch.py @@ -0,0 +1,51 @@ +""" +File: run_batch.py +Author: Nrupatunga +Email: nrupatunga.s@byjus.com +Github: https://github.com/nrupatunga +Description: run on batch of images +""" +import glob +import os + +import cv2 +import numpy as np +from tqdm import tqdm +import time +import matplotlib.pyplot as plt + +from L0Smoothing import L0Smoothing + +root_dir = \ + '/home/nthere/2020/handwriting-ocr/data/REAL_DATA_JEROME/processed/test-org/' +image_files = glob.glob(os.path.join(root_dir, '**', '*.bmp'), + recursive=True) + +out_dir = './REAL_DATA_JEROME' +if not os.path.exists(out_dir): + os.makedirs(out_dir) + +for i, img_path in tqdm(enumerate(image_files)): + img = cv2.imread(img_path, 0) + fig, axs = plt.subplots(1, 2) + n, _, _ = axs[0].hist(img.ravel() / 255, bins=256, range=(0.0, 0.5), fc='k', ec='k') + ratio = sum(n) / (img.shape[0] * img.shape[1]) + + start = time.time() + S = L0Smoothing(img_path, param_lambda=ratio / 2).run() + print('Time taken for one image: {}'.format(time.time() - start)) + S = np.squeeze(S) + S = np.clip(S, 0, 1) + S = S * 255 + S = S.astype(np.uint8) + out = np.concatenate((img, S), axis=1) + out_path = os.path.join(out_dir, str(i) + '.bmp') + + print('Ratio = {}'.format(ratio)) + axs[1].imshow(out) + axs[0].axis('off') + axs[1].axis('off') + plt.draw() + plt.waitforbuttonpress(0) + plt.close() + # cv2.imwrite(out_path, out) diff --git a/src/stripes_jet.png b/src/stripes_jet.png new file mode 100644 index 0000000..9de3093 Binary files /dev/null and b/src/stripes_jet.png differ