-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
182 lines (147 loc) · 6.4 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
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
"""
Seam Carving for Content Aware Image Resizing and Object Removal
Usage: python main.py -i image_input.jpg -o image_output.jpg -w 100
Author: Ruixi Lin, Xudong Shen, and TAs of CS5340
Date: 11-29-2019
Version: v0.1
References:
[1] Avidan, S., & Shamir, A. (2007, August). Seam carving for content-aware image resizing. In ACM Transactions on graphics (TOG) (Vol. 26, No. 3, p. 10). ACM.
"""
import argparse
import cv2
import numpy as np
class SeamCarver:
'''
Note: This is only a one-dimensional SeamCarver! It only reduces the width.
'''
def __init__(self, filename, out_height, out_width):
self.out_height = out_height
self.out_width = out_width
# read in image
self.in_image = cv2.imread(filename).astype(np.float64)
self.in_height, self.in_width = self.in_image.shape[: 2]
# keep tracking resulting image
self.out_image = np.copy(self.in_image)
# some large number
self.constant = 10000000
# start carving
self.seams_carving()
def seams_carving(self):
delta_col = int(self.out_width - self.in_width)
if delta_col < 0:
self.seams_remove_col(delta_col * -1)
else:
raise ValueError('Currently this SeamCarver only reduces the width.')
def seams_remove_col(self, num_pixel):
for dummy in range(num_pixel):
energy_map = self.calc_energy_map()
seam_idx = self.viterbi(energy_map)
self.delete_seam(seam_idx)
def calc_energy_map(self):
b, g, r = cv2.split(self.out_image)
b_energy = np.absolute(cv2.Scharr(b, -1, 1, 0)) + np.absolute(cv2.Scharr(b, -1, 0, 1))
g_energy = np.absolute(cv2.Scharr(g, -1, 1, 0)) + np.absolute(cv2.Scharr(g, -1, 0, 1))
r_energy = np.absolute(cv2.Scharr(r, -1, 1, 0)) + np.absolute(cv2.Scharr(r, -1, 0, 1))
return b_energy + g_energy + r_energy
def delete_seam(self, seam_idx):
m, n = self.out_image.shape[: 2]
output = np.zeros((m, n - 1, 3))
for row in range(m):
col = seam_idx[row]
output[row, :, 0] = np.delete(self.out_image[row, :, 0], [col])
output[row, :, 1] = np.delete(self.out_image[row, :, 1], [col])
output[row, :, 2] = np.delete(self.out_image[row, :, 2], [col])
self.out_image = np.copy(output)
def save_result(self, filename):
cv2.imwrite(filename, self.out_image.astype(np.uint8))
def viterbi(self,energy_map):
'''
Arguments:
energy_map: 111x164 int matrix
Outputs:
seam_idx: a list of the column indexes in each row, length is 111
'''
# initialize size
tem_height, tem_width = self.out_image.shape[:2]
# initialize variables
seam_idx = np.zeros(tem_height).astype(int)
phi = np.zeros([tem_height, tem_width]).astype(int)
w_old = np.zeros(tem_width)
w_current = np.zeros(tem_width)
# define some simple functions
def p_transition(next_z, z):
if abs(next_z-z) <= 1:
return 1
else:
return self.constant
# forward passing
for row in range(tem_height):
if row == 0:
w_current = energy_map[row,:]
w_old = np.copy(w_current)
else:
for k in range(tem_width):
if k == 0:
for i in [0, 1]:
if i == 0:
Min = p_transition(k,i) + w_old[i]
Min_index = i
else:
B = p_transition(k,i) + w_old[i]
if B < Min:
Min = B
Min_index = i
elif k == (tem_width - 1):
for i in [tem_width - 2, tem_width - 1]:
if i == tem_width - 2:
Min = p_transition(k,i) + w_old[i]
Min_index = i
else:
B = p_transition(k,i) + w_old[i]
if B < Min:
Min = B
Min_index = i
else:
for i in [k-1, k, k+1]:
if i == k-1:
Min = p_transition(k,i) + w_old[i]
Min_index = i
else:
B = p_transition(k,i) + w_old[i]
if B < Min:
Min = B
Min_index = i
w_current[k] = energy_map[row,k] + Min
phi[row,k] = Min_index
w_old = np. copy(w_current)
# backtracking
for row in range(tem_height-1, -1, -1):
if row == tem_height-1:
for k in range(tem_width):
if k == 0:
Min = w_current[k]
seam_idx[row] = k
else:
if w_current[k] < Min:
Min = w_current[k]
seam_idx[row] = k
else:
seam_idx[row] = phi[row+1,seam_idx[row+1]]
return seam_idx
if __name__ == '__main__':
'''
Note: This is only a one-dimensional SeamCarver. It only reduces the width.
'''
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--input', help = 'input image name/path')
parser.add_argument('-o', '--output', help = 'output image name/path')
parser.add_argument('-w', '--width_out', type=int, help = 'output width')
args = parser.parse_args()
height_in, width_in = cv2.imread(args.input).astype(np.float64).shape[: 2]
print('Original image size: ', height_in, width_in)
if args.width_out >= width_in:
raise ValueError("output width must be smaller than input width. Currently this SeamCarver only supports reducing weight.")
print('Carving...')
height_out = height_in
obj = SeamCarver(args.input, height_out, args.width_out)
obj.save_result(args.output)