forked from andrewdcampbell/seam-carving
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseam_carving.py
executable file
·247 lines (188 loc) · 6.78 KB
/
seam_carving.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# USAGE:
# python seam_carving.py (-resize) -im IM -out OUT [-dx DX]
# Examples:
# python seam_carving.py -resize -im demos/ratatouille.jpg -out ratatouille_resize.jpg
#
# python seam_carving.py -remove -im demos/eiffel.jpg -out eiffel_remove.jpg
#
import numpy as np
import cv2
import argparse
from numba import jit
from scipy import ndimage as ndi
import Pose_Detection as pos
SEAM_COLOR = np.array([255, 200, 200]) # seam visualization color (BGR)
SHOULD_DOWNSIZE = True # if True, downsize image for faster carving
DOWNSIZE_WIDTH = 500 # resized image width if SHOULD_DOWNSIZE is True
USE_FORWARD_ENERGY = True # if True, use forward energy algorithm
########################################
# UTILITY CODE
########################################
def resize(image, width):
dim = None
h, w = image.shape[:2]
dim = (width, int(h * width / float(w)))
return cv2.resize(image, dim)
def rotate_image(image, clockwise):
k = 1 if clockwise else 3
return np.rot90(image, k)
########################################
# ENERGY FUNCTIONS
########################################
@jit
def forward_energy(im):
"""
Forward energy algorithm as described in "Improved Seam Carving for Video Retargeting"
by Rubinstein, Shamir, Avidan.
Vectorized code adapted from
https://github.com/axu2/improved-seam-carving.
"""
h, w = im.shape[:2]
im = cv2.cvtColor(im.astype(np.uint8), cv2.COLOR_BGR2GRAY).astype(np.float64)
energy = np.zeros((h, w))
m = np.zeros((h, w))
U = np.roll(im, 1, axis=0)
L = np.roll(im, 1, axis=1)
R = np.roll(im, -1, axis=1)
cU = np.abs(R - L)
cL = np.abs(U - L) + cU
cR = np.abs(U - R) + cU
for i in range(1, h):
mU = m[i-1]
mL = np.roll(mU, 1)
mR = np.roll(mU, -1)
mULR = np.array([mU, mL, mR])
cULR = np.array([cU[i], cL[i], cR[i]])
mULR += cULR
argmins = np.argmin(mULR, axis=0)
m[i] = np.choose(argmins, mULR)
energy[i] = np.choose(argmins, cULR)
# vis = visualize(energy)
# cv2.imwrite("forward_energy_demo.jpg", vis)
return energy
########################################
# SEAM HELPER FUNCTIONS
########################################
@jit
def add_seam(im, seam_idx):
"""
Add a vertical seam to a 3-channel color image at the indices provided
by averaging the pixels values to the left and right of the seam.
Code adapted from https://github.com/vivianhylee/seam-carving.
"""
h, w = im.shape[:2]
output = np.zeros((h, w + 1, 3))
for row in range(h):
col = seam_idx[row]
for ch in range(3):
if col == 0:
p = np.average(im[row, col: col + 2, ch])
output[row, col, ch] = im[row, col, ch]
output[row, col + 1, ch] = p
output[row, col + 1:, ch] = im[row, col:, ch]
else:
p = np.average(im[row, col - 1: col + 1, ch])
output[row, : col, ch] = im[row, : col, ch]
output[row, col, ch] = p
output[row, col + 1:, ch] = im[row, col:, ch]
return output
@jit
def remove_seam(im, boolmask):
h, w = im.shape[:2]
boolmask3c = np.stack([boolmask] * 3, axis=2)
return im[boolmask3c].reshape((h, w - 1, 3))
@jit
def get_minimum_seam(im):
"""
DP algorithm for finding the seam of minimum energy. Code adapted from
https://karthikkaranth.me/blog/implementing-seam-carving-with-python/
"""
h, w = im.shape[:2]
energyfn = forward_energy
M = energyfn(im)
backtrack = np.zeros_like(M, dtype=np.int)
# populate DP matrix
for i in range(1, h):
for j in range(0, w):
if j == 0:
idx = np.argmin(M[i - 1, j:j + 2])
backtrack[i, j] = idx + j
min_energy = M[i-1, idx + j]
else:
idx = np.argmin(M[i - 1, j - 1:j + 2])
backtrack[i, j] = idx + j - 1
min_energy = M[i - 1, idx + j - 1]
M[i, j] += min_energy
# backtrack to find path
seam_idx = []
boolmask = np.ones((h, w), dtype=np.bool)
j = np.argmin(M[-1])
for i in range(h-1, -1, -1):
boolmask[i, j] = False
seam_idx.append(j)
j = backtrack[i, j]
seam_idx.reverse()
return np.array(seam_idx), boolmask
########################################
# MAIN ALGORITHM
########################################
def seams_removal(im, num_remove):
for _ in range(num_remove):
seam_idx, boolmask = get_minimum_seam(im)
im = remove_seam(im, boolmask)
return im
def seams_insertion(im, num_add):
seams_record = []
temp_im = im.copy()
for _ in range(num_add):
seam_idx, boolmask = get_minimum_seam(temp_im)
seams_record.append(seam_idx)
temp_im = remove_seam(temp_im, boolmask)
seams_record.reverse()
for _ in range(num_add):
seam = seams_record.pop()
im = add_seam(im, seam)
# update the remaining seam indices
for remaining_seam in seams_record:
remaining_seam[np.where(remaining_seam >= seam)] += 2
return im
########################################
# MAIN DRIVER FUNCTIONS
########################################
def seam_carve(im, dx):
im = im.astype(np.float64)
h, w = im.shape[:2]
assert h > 0 and w + dx > 0 and dx <= w
output = im
if dx < 0:
output = seams_removal(output, -dx)
elif dx > 0:
output = seams_insertion(output, dx)
return output
if __name__ == '__main__':
ap = argparse.ArgumentParser()
group = ap.add_mutually_exclusive_group(required=True)
group.add_argument("-resize", action='store_true')
ap.add_argument("-im1", help="Path to image", required=True)
ap.add_argument("-im2", help="Path to image", required=True)
ap.add_argument("-out", help="Output file name", required=True)
ap.add_argument("-dx", help="Number of horizontal seams to add/subtract", type=int, default=0)
args = vars(ap.parse_args())
IM1_PATH, IM2_PATH, OUTPUT_NAME = args["im1"], args["im2"], args["out"]
im1 = cv2.imread(IM1_PATH)
assert im is not None
im2 = cv2.imread(IM1_PATH)
assert im is not None
if no args["dx"]:
dx = pos.detectPose(im1, im2, pos.pose_image, draw=True, display=True)
else:
dx = args["dx"]
assert dx is not None
# downsize image for faster processing
h, w = im.shape[:2]
if SHOULD_DOWNSIZE and w > DOWNSIZE_WIDTH:
im = resize(im, width=DOWNSIZE_WIDTH)
# image resize mode
if args["resize"]:
output = seam_carve(im, dx)
cv2.imwrite(OUTPUT_NAME, output)