-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
129 lines (95 loc) · 3.06 KB
/
main.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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import cv2 as cv
import numpy as np
def nothing(x):
pass
def resize(h, w, img):
img_h = len(img)
img_w = len(img[0])
#print(img_h, img_w)
e = energy(img)
if h < img_h:
e = np.transpose(e, (1, 0, 2))
img = np.transpose(img, (1, 0, 2))
trace = findMinPath(e)
img = remove(trace, img)
e = np.transpose(e, (1, 0, 2))
img = np.transpose(img, (1, 0, 2))
if h > img_h:
pass #do after
if w < img_w:
trace = findMinPath(e)
img = remove(trace, img)
if w > img_w:
pass #do after
return img
def colorPath(trace, img, color = (0, 0, 255)):
r, c, _ = img.shape
for i in range(r):
img[i][trace[i]] = color
return img
def remove(trace, img):
r, c, _ = img.shape
newImg = np.zeros((r, c - 1, 3), dtype=np.uint8)
for i in range(r):
newImg[i] = np.delete(img[i], trace[i], axis = 0)
return newImg
def energy(img):
img = np.array(img, dtype=int)
img1 = np.delete(img, 0, 0)
img2 = np.delete(img, 0, 1)
img1 = np.insert(img1, len(img1) - 1, [127, 127, 127], axis = 0)
img2 = np.insert(img2, len(img2[0]) - 1, [127, 127, 127], axis = 1)
return abs(img - img1) + abs(img - img2)
def findMinPath(e):
r, c, _ = e.shape
dp = np.zeros((r, c), dtype=int)
dp[0] = np.sum(e[0], axis = 1)
for i in range(1, r):
dp[i][:-1] = np.minimum(dp[i - 1][:-1], dp[i - 1][1:])
dp[i][-1] = dp[i - 1][-1]
dp[i][1:] = np.minimum(dp[i][1:], dp[i - 1][:-1])
dp[i] += np.sum(e[i], axis = 1)
trace = np.zeros(r, dtype=int)
last = 0
for i in range(1, c):
if dp[r - 1][i] < dp[r - 1][last]:
last = i
trace[r - 1] = last
for i in range(r - 1, 0, -1):
last = trace[i]
minVal = dp[i - 1][last]
trace[i - 1] = last
if last > 0:
minVal = min(minVal, dp[i - 1][last - 1])
if minVal == dp[i - 1][last - 1]:
trace[i - 1] = last - 1
if last < c - 1:
minVal = min(minVal, dp[i - 1][last + 1])
if minVal == dp[i - 1][last + 1]:
trace[i - 1] = last + 1
#trace[i] = trace[i] + i * c
return trace
def main():
root = 'img/'
imgPath = root + 'img1.png'
#imgPath = root + 'img2.jpg'
print(imgPath)
img = cv.imread(imgPath)
cv.namedWindow('image')
cv.namedWindow('slider', cv.WINDOW_NORMAL)
cv.resizeWindow('slider', 512, 64)
cv.createTrackbar('Height', 'slider', len(img), 2 * len(img), nothing)
cv.createTrackbar('Width', 'slider', len(img[0]), 2 * len(img[0]), nothing)
cv.imshow('slider', np.full(1, 255, dtype=np.uint8))
while (True):
h = cv.getTrackbarPos('Height', 'slider')
w = cv.getTrackbarPos('Width', 'slider')
if h != len(img) or w != len(img[0]):
img = resize(h, w, img)
cv.imshow('image', img)
pressKey = cv.waitKey(1) & 0xFF
if pressKey == ord('q'):
break
cv.destroyAllWindows()
if __name__ == "__main__":
main()