Skip to content

Commit 7250eb4

Browse files
authored
Add files via upload
1 parent e70ef1b commit 7250eb4

9 files changed

Lines changed: 266 additions & 0 deletions

File tree

blurlab/BoxBlur.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import numpy as np
2+
from PIL import Image
3+
import cv2
4+
5+
boxKernelDims = [3,5,7,9]
6+
7+
def BoxBlur_random(img):
8+
kernelidx = np.random.randint(0, len(boxKernelDims))
9+
kerneldim = boxKernelDims[kernelidx]
10+
return BoxBlur(img, kerneldim)
11+
12+
def BoxBlur(img, dim):
13+
imgarray = np.array(img, dtype="float32")
14+
kernel = BoxKernel(dim)
15+
# convolved = convolve2d(imgarray, kernel, mode='same', fillvalue=255.0).astype("uint8")
16+
convolved = cv2.filter2D(imgarray, -1, kernel).astype("uint8")
17+
img = Image.fromarray(convolved)
18+
19+
return img
20+
21+
def BoxKernel(dim):
22+
kernelwidth = dim
23+
kernel = np.ones((kernelwidth, kernelwidth), dtype=np.float32)
24+
normalizationFactor = np.count_nonzero(kernel)
25+
kernel = kernel / normalizationFactor
26+
return kernel

blurlab/DefocusBlur.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# -*- coding: utf-8 -*-
2+
import numpy as np
3+
from PIL import Image
4+
from skimage.draw import disk
5+
6+
import cv2
7+
8+
defocusKernelDims = [3,5,7,9]
9+
10+
def DefocusBlur_random(img):
11+
kernelidx = np.random.randint(0, len(defocusKernelDims))
12+
kerneldim = defocusKernelDims[kernelidx]
13+
return DefocusBlur(img, kerneldim)
14+
15+
def DefocusBlur(img, dim):
16+
imgarray = np.array(img, dtype="float32")
17+
kernel = DiskKernel(dim)
18+
# convolved = convolve2d(imgarray, kernel, mode='same', fillvalue=255.0).astype("uint8")
19+
convolved = cv2.filter2D(imgarray, -1, kernel).astype("uint8")
20+
img = Image.fromarray(convolved)
21+
return img
22+
23+
24+
def DiskKernel(dim):
25+
kernelwidth = dim
26+
kernel = np.zeros((kernelwidth, kernelwidth), dtype=np.float32)
27+
circleCenterCoord = dim / 2
28+
circleRadius = circleCenterCoord +1
29+
30+
rr, cc = disk((circleCenterCoord, circleCenterCoord), circleRadius-1)
31+
kernel[rr, cc] = 1
32+
33+
if(dim == 3 or dim == 5):
34+
kernel = Adjust(kernel, dim)
35+
36+
normalizationFactor = np.count_nonzero(kernel)
37+
kernel = kernel / normalizationFactor
38+
return kernel
39+
40+
def Adjust(kernel, kernelwidth):
41+
kernel[0,0] = 0
42+
kernel[0,kernelwidth-1]=0
43+
kernel[kernelwidth-1,0]=0
44+
kernel[kernelwidth-1, kernelwidth-1] =0
45+
return kernel

blurlab/GaussianBlur.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import numpy as np
2+
from PIL import ImageFilter
3+
4+
gaussianbandwidths = [0.5, 1, 1.5, 2, 2.5, 3, 3.5]
5+
6+
def GaussianBlur_random(img):
7+
gaussianidx = np.random.randint(0, len(gaussianbandwidths))
8+
gaussianbandwidth = gaussianbandwidths[gaussianidx]
9+
return GaussianBlur(img, gaussianbandwidth)
10+
11+
def GaussianBlur(img, bandwidth):
12+
img = img.filter(ImageFilter.GaussianBlur(bandwidth))
13+
return img

blurlab/LineDictionary.py

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
class LineDictionary:
2+
def __init__(self):
3+
self.lines = {}
4+
self.Create3x3Lines()
5+
self.Create5x5Lines()
6+
self.Create7x7Lines()
7+
self.Create9x9Lines()
8+
return
9+
10+
def Create3x3Lines(self):
11+
lines = {}
12+
lines[0] = [1,0,1,2]
13+
lines[45] = [2,0,0,2]
14+
lines[90] = [0,1,2,1]
15+
lines[135] = [0,0,2,2]
16+
self.lines[3] = lines
17+
return
18+
19+
def Create5x5Lines(self):
20+
lines = {}
21+
lines[0] = [2,0,2,4]
22+
lines[22.5] = [3,0,1,4]
23+
lines[45] = [0,4,4,0]
24+
lines[67.5] = [0,3,4,1]
25+
lines[90] = [0,2,4,2]
26+
lines[112.5] = [0,1,4,3]
27+
lines[135] = [0,0,4,4]
28+
lines[157.5]= [1,0,3,4]
29+
self.lines[5] = lines
30+
return
31+
32+
def Create7x7Lines(self):
33+
lines = {}
34+
lines[0] = [3,0,3,6]
35+
lines[15] = [4,0,2,6]
36+
lines[30] = [5,0,1,6]
37+
lines[45] = [6,0,0,6]
38+
lines[60] = [6,1,0,5]
39+
lines[75] = [6,2,0,4]
40+
lines[90] = [0,3,6,3]
41+
lines[105] = [0,2,6,4]
42+
lines[120] = [0,1,6,5]
43+
lines[135] = [0,0,6,6]
44+
lines[150] = [1,0,5,6]
45+
lines[165] = [2,0,4,6]
46+
self.lines[7] = lines
47+
return
48+
49+
def Create9x9Lines(self):
50+
lines = {}
51+
lines[0] = [4,0,4,8]
52+
lines[11.25] = [5,0,3,8]
53+
lines[22.5] = [6,0,2,8]
54+
lines[33.75] = [7,0,1,8]
55+
lines[45] = [8,0,0,8]
56+
lines[56.25] = [8,1,0,7]
57+
lines[67.5] = [8,2,0,6]
58+
lines[78.75] = [8,3,0,5]
59+
lines[90] = [8,4,0,4]
60+
lines[101.25] = [0,3,8,5]
61+
lines[112.5] = [0,2,8,6]
62+
lines[123.75] = [0,1,8,7]
63+
lines[135] = [0,0,8,8]
64+
lines[146.25] = [1,0,7,8]
65+
lines[157.5] = [2,0,6,8]
66+
lines[168.75] = [3,0,5,8]
67+
self.lines[9] = lines
68+
return

blurlab/LinearMotionBlur.py

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
# -*- coding: utf-8 -*-
2+
import math
3+
import numpy as np
4+
from PIL import Image
5+
from skimage.draw import line
6+
7+
from .LineDictionary import LineDictionary
8+
import cv2
9+
10+
lineLengths =[3,5,7,9]
11+
lineTypes = ["full", "right", "left"]
12+
13+
lineDict = LineDictionary()
14+
15+
def LinearMotionBlur_random(img):
16+
lineLengthIdx = np.random.randint(0, len(lineLengths))
17+
lineTypeIdx = np.random.randint(0, len(lineTypes))
18+
lineLength = lineLengths[lineLengthIdx]
19+
lineType = lineTypes[lineTypeIdx]
20+
lineAngle = randomAngle(lineLength)
21+
return LinearMotionBlur(img, lineLength, lineAngle, lineType)
22+
23+
def LinearMotionBlur(img, dim, angle, linetype):
24+
imgarray = np.array(img, dtype="float32")
25+
kernel = LineKernel(dim, angle, linetype)
26+
convolved = cv2.filter2D(imgarray, -1, kernel).astype("uint8")
27+
img = Image.fromarray(convolved)
28+
return img
29+
30+
def LineKernel(dim, angle, linetype):
31+
kernelwidth = dim
32+
kernelCenter = int(math.floor(dim/2))
33+
angle = SanitizeAngleValue(kernelCenter, angle)
34+
kernel = np.zeros((kernelwidth, kernelwidth), dtype=np.float32)
35+
lineAnchors = lineDict.lines[dim][angle]
36+
if(linetype == 'right'):
37+
lineAnchors[0] = kernelCenter
38+
lineAnchors[1] = kernelCenter
39+
if(linetype == 'left'):
40+
lineAnchors[2] = kernelCenter
41+
lineAnchors[3] = kernelCenter
42+
rr,cc = line(lineAnchors[0], lineAnchors[1], lineAnchors[2], lineAnchors[3])
43+
kernel[rr,cc]=1
44+
normalizationFactor = np.count_nonzero(kernel)
45+
kernel = kernel / normalizationFactor
46+
return kernel
47+
48+
def SanitizeAngleValue(kernelCenter, angle):
49+
numDistinctLines = kernelCenter * 4
50+
angle = math.fmod(angle, 180.0)
51+
validLineAngles = np.linspace(0,180, numDistinctLines, endpoint = False)
52+
angle = nearestValue(angle, validLineAngles)
53+
return angle
54+
55+
def nearestValue(theta, validAngles):
56+
idx = (np.abs(validAngles-theta)).argmin()
57+
return validAngles[idx]
58+
59+
def randomAngle(kerneldim):
60+
kernelCenter = int(math.floor(kerneldim/2))
61+
numDistinctLines = kernelCenter * 4
62+
validLineAngles = np.linspace(0,180, numDistinctLines, endpoint = False)
63+
angleIdx = np.random.randint(0, len(validLineAngles))
64+
return int(validLineAngles[angleIdx])

blurlab/PsfBlur.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# -*- coding: utf-8 -*-
2+
import numpy as np
3+
import pickle
4+
from PIL import Image
5+
import cv2
6+
import os.path
7+
8+
pickledPsfFilename =os.path.join(os.path.dirname( __file__),"psf.pkl")
9+
10+
with open(pickledPsfFilename, 'rb') as pklfile:
11+
psfDictionary = pickle.load(pklfile, encoding='latin1')
12+
13+
14+
def PsfBlur(img, psfid):
15+
imgarray = np.array(img, dtype="float32")
16+
kernel = psfDictionary[psfid]
17+
convolved = cv2.filter2D(imgarray, -1, kernel).astype("uint8")
18+
img = Image.fromarray(convolved)
19+
return img
20+
21+
def PsfBlur_random(img):
22+
psfid = np.random.randint(0, len(psfDictionary))
23+
return PsfBlur(img, psfid)
24+
25+

blurlab/RandomizedBlur.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import numpy as np
2+
from .BoxBlur import BoxBlur_random
3+
from .DefocusBlur import DefocusBlur_random
4+
from .GaussianBlur import GaussianBlur_random
5+
from .LinearMotionBlur import LinearMotionBlur_random
6+
from .PsfBlur import PsfBlur_random
7+
8+
blurFunctions = {"0": BoxBlur_random, "1": DefocusBlur_random, "2": GaussianBlur_random, "3": LinearMotionBlur_random, "4": PsfBlur_random}
9+
10+
def RandomizedBlur(img):
11+
blurToApply = blurFunctions[str(np.random.randint(0, len(blurFunctions)))]
12+
return blurToApply(img)

blurlab/__init__.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from .BoxBlur import BoxBlur, BoxBlur_random
2+
from .DefocusBlur import DefocusBlur, DefocusBlur_random
3+
from .GaussianBlur import GaussianBlur, GaussianBlur_random
4+
from .LinearMotionBlur import LinearMotionBlur, LinearMotionBlur_random
5+
from .PsfBlur import PsfBlur, PsfBlur_random
6+
from .RandomizedBlur import RandomizedBlur
7+
8+
__all__ = ["BoxBlur", "BoxBlur_random",
9+
"DefocusBlur", "DefocusBlur_random",
10+
"GaussianBlur", "GaussianBlur_random",
11+
"LinearMotionBlur", "LinearMotionBlur_random",
12+
"PsfBlur", "PsfBlur_random",
13+
"RandomizedBlur"]

blurlab/psf.pkl

145 KB
Binary file not shown.

0 commit comments

Comments
 (0)