forked from smaranjitghose/ArtCV
-
Notifications
You must be signed in to change notification settings - Fork 1
/
motion_blur.py
61 lines (49 loc) · 1.71 KB
/
motion_blur.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Import the dependencies
import argparse
import cv2 #image processing lib
import numpy as np #matrix manipulation
from time import sleep #for slowing down the process to make progress visible
from tqdm import tqdm as tqdm # for progess bar
def motionBlur(img):
# default kernel size.
size = 70
# Size of kernel is directly proportional to effect of blur
# Create the Horizontal and Vertical kernel.
Hkernel = np.zeros((size, size))
Vkernel = np.copy(Hkernel)
# Fill the middle row with ones.
Hkernel[int((size - 1)/2), :] = np.ones(size)
Vkernel[:, int((size - 1)/2)] = np.ones(size)
# Normalize the value under range of 0-1
Hkernel /= size
Vkernel /= size
# Apply the vertical kernel.
v = cv2.filter2D(img, -1, Vkernel)
# Apply the horizontal kernel.
h = cv2.filter2D(img, -1, Hkernel)
# Create Diagonal filter kernel
Dkernel = np.copy(Hkernel)
i=0 #initialize index matching var.
for x in tqdm(Dkernel):
for j in range(0,size):
if(j==i):
x[i]=1
i+=1
sleep(0.01) # to make the progress visible
# Normalize.
Dkernel /= size
# Apply the Diagonal kernel.
d = cv2.filter2D(img, -1, Dkernel)
return v,h,d
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="path to input image")
args = vars(ap.parse_args())
img = Image.open(args['image'])
print("Work in Progress.")
# Save the outputs.
v,h,d=motionBlur(img)
cv2.imwrite('assets/horizontalBlur.jpg', v)
cv2.imwrite('assets/verticalBlur.jpg', h)
cv2.imwrite('assets/diagonalBlur.jpg', d)
print("Work done successfully!")