Skip to content

Commit

Permalink
input and output processing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
nrupatunga committed May 8, 2020
1 parent a04fac5 commit b6228d2
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 12 deletions.
25 changes: 13 additions & 12 deletions src/L0Smoothing.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

import cv2
import numpy as np
from scipy.fftpack import fft2, ifft2

from psf2otf import psf2otf

Expand All @@ -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]

Expand All @@ -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]
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Binary file added src/basketball.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/output_python.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions src/run_batch.py
Original file line number Diff line number Diff line change
@@ -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)
Binary file added src/stripes_jet.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit b6228d2

Please sign in to comment.