-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseam_carving.py
177 lines (160 loc) · 5.35 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
"""seam-carving.py
Written by Husam Alsayed Ahmad
AI Engineer
August 2021
"""
import numpy as np
import os
import cv2
import copy
from numba import jit
import argparse
from PIL import Image
import sys
import numpy as np
from scipy.ndimage.filters import convolve
parser = argparse.ArgumentParser(description = 'reduce the image with seam-carving algorithm')
parser.add_argument('-image', type = str, help = 'path of an image')
parser.add_argument('-wcut', type = int, help = 'the number of pixels to remove in the width')
parser.add_argument('-hcut', type = int, help = 'the number of pixels to remove in the height')
args = parser.parse_args()
EPS = 1e-8
def calc_energy(img):
filter_du = np.array([
[1.0, 2.0, 1.0],
[0.0, 0.0, 0.0],
[-1.0, -2.0, -1.0],
])
filter_du = np.stack([filter_du] * 3, axis=2)
filter_dv = np.array([
[1.0, 0.0, -1.0],
[2.0, 0.0, -2.0],
[1.0, 0.0, -1.0],
])
filter_dv = np.stack([filter_dv] * 3, axis=2)
img = img.astype('float32')
convolved = np.sqrt(convolve(img, filter_du)**2 + (convolve(img, filter_dv)**2))
energy_map = convolved.sum(axis=2)
return energy_map
@jit(nopython=True)
def get_least_energy_columns(dp,arr,i,j):
if i == arr.shape[0]:
return 0
if j == arr.shape[1]:
return 1e12
if dp[i][j] != -1:
return dp[i][j]
ret = 1e12
for k in range(-1,2):
if j + k < 0 or j + k >= arr.shape[1]:
continue
ret = min(ret, get_least_energy_columns(dp,arr,i + 1,j + k) + arr[i,j])
dp[i][j] = ret
return ret
@jit(nopython=True)
def build_least_energy_columns(dp, lst,arr,i,j):
if i == arr.shape[0]:
return 0
if j == arr.shape[1]:
return 1e12
ret = 1e12
for k in range(-1,2):
if j + k < 0 or j + k >= arr.shape[1]:
continue
ret = min(ret, get_least_energy_columns(dp, arr,i + 1,j + k) + arr[i,j])
for k in range(-1,2):
if j + k < 0 or j + k >= arr.shape[1]:
continue
av = get_least_energy_columns(dp, arr,i + 1,j + k) + arr[i,j]
if abs(av - ret) < EPS:
lst[i] = j + k
build_least_energy_columns(dp, lst,arr,i + 1,j + k)
break
@jit(nopython=True)
def get_least_energy_rows(dp,arr,i,j):
if j == arr.shape[1]:
return 0
if i == arr.shape[0]:
return 1e12
if dp[i][j] != -1:
return dp[i][j]
ret = 1e12
for k in range(-1,2):
if i + k < 0 or i + k >= arr.shape[0]:
continue
ret = min(ret, get_least_energy_rows(dp,arr,i + k,j + 1) + arr[i,j])
dp[i][j] = ret
return ret
@jit(nopython=True)
def build_least_energy_rows(dp, lst,arr,i,j):
if j == arr.shape[1]:
return 0
if i == arr.shape[0]:
return 1e12
ret = 1e12
for k in range(-1,2):
if i + k < 0 or i + k >= arr.shape[0]:
continue
ret = min(ret, get_least_energy_rows(dp, arr,i + k, j + 1) + arr[i,j])
for k in range(-1,2):
if i + k < 0 or i + k >= arr.shape[0]:
continue
av = get_least_energy_rows(dp, arr,i + k,j + 1) + arr[i,j]
if abs(av - ret) < EPS:
lst[j] = i + k
build_least_energy_rows(dp, lst,arr,i + k,j + 1)
break
@jit(nopython=True)
def re_initialize(image_shape):
dp = np.zeros(shape = (image_shape[0],image_shape[1] ))
for i in range(dp.shape[0]):
for j in range(dp.shape[1]):
dp[i,j] = -1
return dp
def get_deleted_lst(sobel_img,columns = True):
ans = 1e12
dp = re_initialize(sobel_img.shape)
if columns:
lst = np.zeros(shape = (sobel_img.shape[0],),dtype='int')
for i in range(sobel_img.shape[1]):
v = get_least_energy_columns(dp, sobel_img, 0, i)
if v < ans:
ans = v
lst = np.zeros(shape = (sobel_img.shape[0],),dtype='int')
build_least_energy_columns(dp, lst, sobel_img, 0, i)
else:
lst = np.zeros(shape = (sobel_img.shape[1],),dtype='int')
for i in range(sobel_img.shape[0]):
v = get_least_energy_rows(dp, sobel_img, i, 0)
if v < ans:
ans = v
lst = np.zeros(shape = (sobel_img.shape[1],),dtype='int')
build_least_energy_rows(dp, lst, sobel_img, i, 0)
return lst
def get_new_image(original_image,num_deleted_columns,num_deleted_rows):
for i in range(num_deleted_columns):
sobel_image = calc_energy(original_image)
lst = get_deleted_lst(sobel_image)
cutted_image = np.zeros(shape = (original_image.shape[0],original_image.shape[1] - 1,original_image.shape[2]))
for index, value in enumerate(lst):
new_image_row = np.concatenate( [ original_image[index,:value,:] , original_image[index, value + 1:,:]] )
cutted_image[index,:,:] = new_image_row
original_image = copy.deepcopy(cutted_image)
for i in range(num_deleted_rows):
sobel_image = calc_energy(original_image)
lst = get_deleted_lst(sobel_image,False)
cutted_image = np.zeros(shape = (original_image.shape[0] - 1,original_image.shape[1],original_image.shape[2]))
for index, value in enumerate(lst):
new_image_col = np.concatenate( [ original_image[:value,index,:] , original_image[value + 1:, index,:]] )
cutted_image[:,index,:] = new_image_col
original_image = copy.deepcopy(cutted_image)
return original_image
if __name__ == '__main__':
image = cv2.imread(args.image)
name = args.image.split('/')[-1]
new_image = get_new_image(image,100,100)
folder_name = './outputs'
if not os.path.exists(folder_name):
os.mkdir(folder_name)
cv2.imwrite(f'{folder_name}/{name}',new_image)