generated from github/welcome-to-github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SeamCarving.py
211 lines (161 loc) · 5.79 KB
/
SeamCarving.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
# -*- coding: utf-8 -*-
"""
Created on Sun Jul 21 13:23:58 2019
@author: svupper
"""
#from PIL import Image
from matplotlib import pyplot as plt
from skimage import filters
from tqdm import tqdm
import numpy as np
import matplotlib.image as mpimg
import time
import argparse
class SeamCarvingJob():
def __init__(self):
pass
def load_argparse(self):
argparser = argparse.ArgumentParser(description='Seam Carving')
argparser.add_argument('--input', help='Input image file', required=True)
argparser.add_argument('--output', help='Output image file', required=True)
argparser.add_argument('--nb_iter', help='Number of iterations', default=1, type=int)
args : argparse.Namespace = argparser.parse_args()
self.input :str = args.input
self.output :str = args.output
self.nb_iter :int = args.nb_iter
def load_image(self):
self.image : np.ndarray = mpimg.imread(self.input)
self.gray : np.ndarray = rgb2gray(self.image)
def save_image(self):
mpimg.imsave(self.output, self.image)
def rgb2gray(rgb : np.ndarray) -> np.ndarray:
return np.dot(rgb[...,:3], [0.2989, 0.5870, 0.1140])
def x_dynamic(image):
dyn=np.copy(image)
for i in range(1,len(image[:,1])):
for j in range(0,len(image[1,:])):
if j==0:
dyn[i][j]=image[i][j]+np.min((dyn[i-1][j],dyn[i-1][j+1]))
elif j==(image.shape[1]-1):
dyn[i][j]=image[i][j]+np.min((dyn[i-1][j-1],dyn[i-1][j]))
else:
dyn[i][j]=image[i][j]+np.min((dyn[i-1][j-1],dyn[i-1][j],dyn[i-1][j+1]))
return dyn
def get_seam(image : np.ndarray, dyn_image : np.ndarray) -> np.array:
seam=np.array([],dtype=int)
i=dyn_image.shape[0]-1
j=np.argmin(dyn_image[-1,:])
seam=np.append(seam,j)
for i in reversed(range(1,dyn_image.shape[0])):
if j==0:
a=np.argmin((dyn_image[i-1,j],dyn_image[i-1,j+1]))
if a==0:
j=j
seam=np.append(seam,j)
else:
j=j+1
seam=np.append(seam,j)
elif j==(dyn_image.shape[1]-1):
a=np.argmin((dyn_image[i-1,j-1],dyn_image[i-1,j]))
if a==0:
j=j-1
seam=np.append(seam,j)
else:
j=j
seam=np.append(seam,j)
else:
a=np.argmin((dyn_image[i-1,j-1],dyn_image[i-1,j],dyn_image[i-1,j+1]))
if a==0:
j=j-1
seam=np.append(seam,j)
elif a==1:
j=j
seam=np.append(seam,j)
else:
j=j+1
seam=np.append(seam,j)
return seam
def carving(image : np.ndarray, dyn_image : np.ndarray, seam : np.array):
image_s=np.zeros_like(dyn_image[:,0:(dyn_image.shape[1]-1)])
seam=seam[::-1]
j=seam[-1]
if j==0:
image_s[-1,:]=image[-1,1:(image.shape[1])]
elif j==(image.shape[1]-1):
image_s[-1,:]=image[-1,0:-1]
else:
if j==1:
image_s[-1,:]=np.concatenate((image[-1,0],image[-1,(j+1):(image.shape[1])]),axis=None)
elif j==(dyn_image.shape[1]-2):
image_s[-1,:]=np.concatenate((image[-1,0:j],image[-1,-1]),axis=None)
else:
image_s[-1,:]=np.concatenate((image[-1,0:j],image[-1,(j+1):(image.shape[1])]),axis=None)
for i in reversed(range(dyn_image.shape[0]-1)):
j=seam[i]
if j==0:
image_s[i,:]=image[i,1:(image.shape[1])]
elif j==(image.shape[1]-1):
image_s[i,:]=image[i,0:-1]
else:
if j==1:
image_s[i,:]=np.concatenate((image[i,0],image[i,(j+1):(image.shape[1])]),axis=None)
elif j==(dyn_image.shape[1]-2):
image_s[i,:]=np.concatenate((image[i,0:j],image[i,-1]),axis=None)
else:
image_s[i,:]=np.concatenate((image[i,0:j],image[i,(j+1):(image.shape[1])]),axis=None)
return image_s
def displayGradient(image : np.ndarray):
# plt.gray()
# plt.figure(figsize=(10,10))
plt.subplot(221)
plt.imshow(image)
plt.xticks([])
plt.yticks([])
plt.title('original', size=20)
plt.subplot(222)
edges_x = filters.sobel_h(image)
plt.imshow(edges_x)
plt.xticks([])
plt.yticks([])
plt.title('sobel_x', size=20)
plt.subplot(223)
edges_y = filters.sobel_v(image)
plt.imshow(edges_y)
plt.xticks([])
plt.yticks([])
plt.title('sobel_y', size=20)
plt.subplot(224)
edges = filters.sobel(image)
plt.imshow(edges)
plt.title('sobel', size=20)
plt.xticks([])
plt.yticks([])
plt.show()
def display(image : np.ndarray):
plt.figure()
plt.imshow(image)
plt.xticks([])
plt.yticks([])
plt.show()
def seamCarving(nb_iter : int = 2, image : np.ndarray = None) -> np.ndarray:
for _ in tqdm(range(0, nb_iter)):
gray_image = rgb2gray(image)
energy = filters.sobel(gray_image)
x_dyn=x_dynamic(energy)
seam=get_seam(gray_image,x_dyn)
seam=seam[::-1]
# empty image with x,y-1,z dimension based on the original image
image_rgb = np.zeros((image.shape[0], image.shape[1]-1, 3), dtype=np.uint8)
for n_dim in range(3):
image_rgb[:,:,n_dim] = carving(image[:,:,n_dim], x_dyn, seam)
image = image_rgb
return image
def cli():
job = SeamCarvingJob()
job.load_argparse()
job.load_image()
job.image = seamCarving(job.nb_iter, job.image)
job.save_image()
if __name__ == '__main__':
cli()
print('done')