-
Notifications
You must be signed in to change notification settings - Fork 1
/
pchip.py
89 lines (70 loc) · 2.86 KB
/
pchip.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import numpy as np
from scipy.interpolate import PchipInterpolator as pchip
import cv2
from concurrent.futures import ProcessPoolExecutor as PPE
import sys
PATH = str(sys.argv[1])
MAGNIFICATION = int(sys.argv[2])
img = cv2.imread(PATH)
height = img.shape[0]
width = img.shape[1]
magnified_height = height * MAGNIFICATION
magnified_width = width * MAGNIFICATION
tolerance = 1.0 / MAGNIFICATION
# Duplicate the right and bottom of img by 1 pixel each
new_img = np.zeros((height + 1, width + 1, 3), dtype=np.uint8)
new_img[0:height, 0:width] = img
new_img[height, 0:width] = img[height - 1, 0:width]
new_img[0:height, width] = img[0:height, width - 1]
new_img[height, width] = img[height - 1, width - 1]
img = new_img
new_height = height + 1
new_width = width + 1
# ------------horizon interpolation------------
# blue
horizon_blue_func = np.zeros(new_height, dtype=object)
for i in range(new_height):
horizon_blue_func[i] = pchip(np.arange(0, new_width), img[i, :, 0])
# green
horizon_green_func = np.zeros(new_height, dtype=object)
for i in range(new_height):
horizon_green_func[i] = pchip(np.arange(0, new_width), img[i, :, 1])
# red
horizon_red_func = np.zeros(new_height, dtype=object)
for i in range(new_height):
horizon_red_func[i] = pchip(np.arange(0, new_width), img[i, :, 2])
def ImageGenerator(i):
row = np.zeros((1, magnified_width, 3), np.uint8)
for j in np.arange(0, width, tolerance):
row[0, round(j * MAGNIFICATION), 0] = horizon_blue_func[i](j)
row[0, round(j * MAGNIFICATION), 1] = horizon_green_func[i](j)
row[0, round(j * MAGNIFICATION), 2] = horizon_red_func[i](j)
return row
img = np.concatenate(
list(PPE().map(ImageGenerator, np.arange(0, new_height))), axis=0
)
print("finish horizon interpolation")
# ------------vertical interpolation------------
vertical_blue_func = np.zeros(magnified_width, dtype=object)
for i in range(magnified_width):
vertical_blue_func[i] = pchip(np.arange(0, new_height), img[:, i, 0])
vertical_green_func = np.zeros(magnified_width, dtype=object)
for i in range(magnified_width):
vertical_green_func[i] = pchip(np.arange(0, new_height), img[:, i, 1])
vertical_red_func = np.zeros(magnified_width, dtype=object)
for i in range(magnified_width):
vertical_red_func[i] = pchip(np.arange(0, new_height), img[:, i, 2])
def ImageGenerator(i):
row = np.zeros((1, magnified_height, 3), np.uint8)
for j in np.arange(0, height, tolerance):
row[0, round(j* MAGNIFICATION), 0] = vertical_blue_func[i](j)
row[0, round(j* MAGNIFICATION), 1] = vertical_green_func[i](j)
row[0, round(j* MAGNIFICATION), 2] = vertical_red_func[i](j)
return row
img = np.concatenate(
list(PPE().map(ImageGenerator, np.arange(0, magnified_width))), axis=0
)
img = img.transpose(1, 0, 2)
print("finish vertical interpolation")
cv2.imwrite("pchip_generated.png", img)
print("generation success")