Skip to content

Commit

Permalink
Merge pull request #18 from prakass1/master
Browse files Browse the repository at this point in the history
improving glass blur using numba
  • Loading branch information
michaelisc authored Mar 31, 2021
2 parents d03ee68 + 952dc3e commit d4ce97e
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 10 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
build/*
*egg-info/
*.egg-info/*
dist/*
*__pycache__*
*.ipynb_checkpoints*
*.pyc
.vscode
.venv
26 changes: 16 additions & 10 deletions imagecorruptions/corruptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import warnings
import os
from pkg_resources import resource_filename
from numba import njit, prange



Expand Down Expand Up @@ -149,6 +150,20 @@ def _motion_blur(x, radius, sigma, angle):
blurred = blurred + kernel[i] * shifted
return blurred

# Numba nopython compilation to shuffle_pixles
@njit()
def _shuffle_pixels_njit_glass_blur(d0,d1,x,c):

# locally shuffle pixels
for i in range(c[2]):
for h in range(d0 - c[1], c[1], -1):
for w in range(d1 - c[1], c[1], -1):
dx, dy = np.random.randint(-c[1], c[1], size=(2,))
h_prime, w_prime = h + dy, w + dx
# swap
x[h, w], x[h_prime, w_prime] = x[h_prime, w_prime], x[h, w]
return x

# /////////////// End Corruption Helpers ///////////////


Expand Down Expand Up @@ -196,21 +211,12 @@ def glass_blur(x, severity=1):

x = np.uint8(
gaussian(np.array(x) / 255., sigma=c[0], multichannel=True) * 255)
x_shape = np.array(x).shape

# locally shuffle pixels
for i in range(c[2]):
for h in range(x_shape[0] - c[1], c[1], -1):
for w in range(x_shape[1] - c[1], c[1], -1):
dx, dy = np.random.randint(-c[1], c[1], size=(2,))
h_prime, w_prime = h + dy, w + dx
# swap
x[h, w], x[h_prime, w_prime] = x[h_prime, w_prime], x[h, w]
x = _shuffle_pixels_njit_glass_blur(np.array(x).shape[0],np.array(x).shape[1],x,c)

return np.clip(gaussian(x / 255., sigma=c[0], multichannel=True), 0,
1) * 255


def defocus_blur(x, severity=1):
c = [(3, 0.1), (4, 0.5), (6, 0.5), (8, 0.5), (10, 0.5)][severity - 1]

Expand Down
6 changes: 6 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
numpy >= 1.16
Pillow >= 5.4.1
scikit-image >= 0.15
opencv-python >= 3.4.5
scipy >= 1.2.1
numba >= 0.53.0
5 changes: 5 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
with open("README.md", "r") as fh:
long_description = fh.read()

# Can be used to load install_require
# with open('requirements.txt', 'r') as freq:
# required = freq.read().splitlines()


setuptools.setup(
name="imagecorruptions",
Expand All @@ -20,6 +24,7 @@
'scikit-image >= 0.15',
'opencv-python >= 3.4.5',
'scipy >= 1.2.1',
'numba >= 0.53.0'
],
include_package_data=True,
classifiers=[
Expand Down
17 changes: 17 additions & 0 deletions test_demo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from imagecorruptions import corrupt, get_corruption_names
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
import time
image = np.asarray(Image.open('test_image.jpg'))
#image = np.ones((427, 640, 3), dtype=np.uint8)

# corrupted_image = corrupt(img, corruption_name='gaussian_blur', severity=1)

for corruption in get_corruption_names('blur'):
tic = time.time()
for severity in range(5):
corrupted = corrupt(image, corruption_name=corruption, severity=severity+1)
plt.imshow(corrupted)
plt.show()
print(corruption, time.time() - tic)
Binary file added test_image.jpg
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 d4ce97e

Please sign in to comment.