-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathseamCarving.py
More file actions
188 lines (166 loc) · 5.98 KB
/
seamCarving.py
File metadata and controls
188 lines (166 loc) · 5.98 KB
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
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 28 00:36:24 2016
@author: yl
"""
from __future__ import unicode_literals
from imgProcessConfig import *
from imgProcessTools import *
from imgProcessTools import (Axes3D, FormatStrFormatter, Fun, HIS_BACKGROUND_COLOR, HIS_MAX, LinearLocator, MAX_FILTER_R, SHOW_SHAPE, View, allFilter, applyColorTable, arange, array, avgFilter, base64, base64Img, bg, bilateralFilter, blackToWhiteVidoEffects, c, cProfile, cm, crun, cv2, da, draw3dSurface, filterr, g, gaussCore, getColorTables, getFilterImg, getLine, gussFilter, ind, interpolate, io, log, loga, mapp, math, maxFilter, mdeianFilter, normalizing, np, os, plt, polt3dSurface, py3, pyv, r, random, roundInt, run, saveAvi, scipy, show, sk, skimage, smallImg, stand, step, sys, tan, unicode_literals)
from skimage import filters
from skimage import transform
import numpy as np
cvSobel = filters.sobel
r = random(3,5)
#show([img,mag])
def getSobel(rgb,mask=None):
img = rgb.astype(int)
tmp = np.mean(np.abs(img[:-1,:-1]-img[1:,:-1])+np.abs(img[:-1,:-1]-img[:-1,1:]),2)
mag = np.append(tmp,tmp[-1:],0)
mag = np.append(mag,mag[:,-1:],1)
if mask is not None:
mag+= mask
return mag.astype(int)
#mag = mySobel(img)
#show([mag,magg])
def reduceOneLine(img,mag,mask=None):
m, n = mag.shape
expand = lambda row: np.append(np.append(row[:1],row),row[-1:])
accu = [mag[0]]
for ind in range(1,m):
tmp = np.zeros((n,3),mag.dtype)
row = expand(accu[ind-1])
tmp[...,0] = row[:-2]
tmp[...,1] = row[1:-1]
tmp[...,2] = row[2:]
add = np.min(tmp,1)
accu += [add+mag[ind]]
accu = np.array(accu)
accui = np.append([[accu.max()+1]]*m,accu,1)
_ind = np.argmin(accui[-1])
ind = _ind - 1
pop = lambda ind,row: np.append(row[:ind],row[ind+1:],0).astype(row.dtype)
newimg = [pop(ind,img[-1])]
if mask is not None:
newmask = [pop(ind,mask[-1])]
for r in range(m-1)[::-1]:
d = np.argmin(accui[r][ind:ind+3])-1
ind = ind+d
newimg = [pop(ind,img[r])]+newimg
if mask is not None:
newmask = [pop(ind,mask[r])]+newmask
newimg = np.array(newimg)
if mask is not None:
# print newmask
return newimg.astype(img.dtype),np.array(newmask)
return newimg.astype(img.dtype),None
def seamCarve(img,deln,mask=None,each=False,f=None,row=None):
if mask is not None:
if isinstance(mask[0][0],float):
mask -= skimage.filters.threshold_li(mask)
mask *= 512
mask.astype(int)
else:
mask[mask==1]=512
mask[mask==-1]=-512
if row:
img=np.transpose(img,[1,0,2])
mask = None if mask is None else mask.T
newimg = img
for i in range(deln):
mag = getSobel(newimg,mask)
newimg,mask = reduceOneLine(newimg,mag,mask)
if f and i % each==0:
f(newimg,i+1,row)
if row:
newimg=np.transpose(newimg,[1,0,2])
return newimg
def reduceOneLineWithHed(img,mag,mask=None):
m, n = mag.shape
expand = lambda row: np.append(np.append(row[:1],row),row[-1:])
accu = [mag[0]]
for ind in range(1,m):
tmp = np.zeros((n,3),mag.dtype)
row = expand(accu[ind-1])
tmp[...,0] = row[:-2]
tmp[...,1] = row[1:-1]
tmp[...,2] = row[2:]
add = np.min(tmp,1)
accu += [add+mag[ind]]
accu = np.array(accu)
accui = np.append([[accu.max()+1]]*m,accu,1)
_ind = np.argmin(accui[-1])
ind = _ind - 1
pop = lambda ind,row: np.append(row[:ind],row[ind+1:],0).astype(row.dtype)
newimg = [pop(ind,img[-1])]
hed = mag
hed = [pop(ind,mag[-1])]
if mask is not None:
newmask = [pop(ind,mask[-1])]
for r in range(m-1)[::-1]:
d = np.argmin(accui[r][ind:ind+3])-1
ind = ind+d
newimg = [pop(ind,img[r])]+newimg
hed = [pop(ind,mag[r])]+hed
if mask is not None:
newmask = [pop(ind,mask[r])]+newmask
newimg = np.array(newimg)
hed = np.array(hed)
if mask is not None:
# print newmask
return newimg.astype(img.dtype),hed,np.array(newmask)
return newimg.astype(img.dtype),hed,None
def seamCarveWithHed(img,deln,mask=None,each=False,f=None,row=None,hed=None):
if mask is not None:
if isinstance(mask[0][0],float):
mask -= skimage.filters.threshold_li(mask)
mask *= 512
mask.astype(int)
else:
mask[mask==1]=512
mask[mask==-1]=-512
if row:
img=np.transpose(img,[1,0,2])
mask = None if mask is None else mask.T
newimg = img
mag = hed.T if row else hed
for i in range(deln):
newimg,mag,mask = reduceOneLineWithHed(newimg,mag,mask)
if f and i % each==0:
f(newimg,i+1,row)
if row:
newimg=np.transpose(newimg,[1,0,2])
return newimg
def addBlack(img,i,row):
new = np.append(img,[[[0,0,0]]*i]*img.shape[0],1).astype(img.dtype)
# new = np.append(img,[[[0,0,0] for _ in range(i)] for __ in range(img.shape[0])],1)
new = np.transpose(new,[1,0,2]) if row else new
return new
scShow = lambda img,i,row: show(addBlack(img,i,row))
def seamCarveBlack(img,deln,mask=None,each=False,f=None,row=None,):
imgNoBlack = seamCarve(img,deln,mask=mask,each=each,f=f,row=row,)
imgNoBlack = np.transpose(imgNoBlack,[1,0,2]) if row else imgNoBlack
newimg = addBlack(imgNoBlack,deln,row)
return newimg
#sc = transform.seam_carve(rgb,mag,'vertical',deln)
#show(sc)
#%%
if __name__ == '__main__':
from imgProcess import *
rgb = da.astronaut()
img = rgb.copy()
img = g['img']
sal = g['sal']
deln = 100
mask = np.zeros(img.shape[:2],int)
# mask[:150] = -1
# mask = None
mask = sal
row = True
newimg = seamCarveBlack(img,deln,mask,deln//3,scShow,row)
hed = g['hed']
# newimg2 = seamCarveWithHed(img,deln,mask,deln//3,scShow,row,hed=hed)
show(newimg)
# show(newimg2)
show([hed,getSobel(img)])
pass