-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconvolution-example.py
61 lines (50 loc) · 1.71 KB
/
convolution-example.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
59
60
61
#!/bin/python
"""
USAGE:
convolution-example.py <image>
"""
import cv2
import math
import numpy as np
import docopt
kernel = np.array([[1,2,1],
[2,4,2],
[1,2,1]])
def upsample(img, kernel, r):
upsample_img = np.zeros((img.shape[0]*r, img.shape[1]*r, 3), dtype='uint8')
kernel_h = kernel.shape[0]/r
kernel_w = kernel.shape[1]/r
for j in range(0, upsample_img.shape[0]):
for i in range(0, upsample_img.shape[1]):
l_0 = j/r
k_0 = i/r
s = np.array([0,0,0], dtype='uint16')
scale_factor = 0
for l in range(l_0, l_0 + kernel_h):
for k in range(k_0, k_0 + kernel_w):
try:
s += kernel[(r*l)-j][(r*k)-i] * img[l][k]
scale_factor += kernel[(r*l)-j][(r*k)-i]
except IndexError:
pass
s_scaled = s/scale_factor
upsample_img[j][i] = s_scaled.astype('uint8')
return upsample_img
def upscale_naive():
img2 = np.zeros((img1.shape[0]*2, img1.shape[1]*2, 3), dtype='uint8')
for y in range(0, img1.shape[0]):
for x in range(0, img1.shape[1]):
img2[y*2][x*2] = img1[y][x]
for y in range(0, img2.shape[0] - 3):
for x in range(0, img2.shape[1] - 3):
s = np.array([0,0,0], dtype='uint8')
for j in range(0, 3):
for i in range(0, 3):
s += kernel[j][i] * img2[y+j][x+i]
img2[y][x] = s
return img2
arguments = docopt.docopt(__doc__)
img1 = cv2.imread(arguments['<image>'])
cv2.imshow("Window1", img1)
cv2.imshow("Window2", upsample(img1, kernel, 2))
cv2.waitKey()