-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSaliencyOptimization.py
250 lines (190 loc) · 6.53 KB
/
SaliencyOptimization.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
248
249
250
import sys
import cv2
import numpy as np
import math
import networkx as nx
import matplotlib.pyplot as plt
from slic_segmentation import *
from skimage.util import img_as_float
from skimage.color import rgb2gray
from skimage.color import gray2rgb
from skimage.color import rgb2lab
from skimage.segmentation import slic
import scipy.spatial.distance
def S(x1,x2,geodesic,sigma_clr=10):
return math.exp(-pow(geodesic[x1,x2],2)/(2*sigma_clr*sigma_clr))
def compute_saliency_cost(smoothness,w_bg,wCtr):
n = len(w_bg)
A = np.zeros((n,n))
b = np.zeros((n))
for x in xrange(0,n):
A[x,x] = 2 * w_bg[x] + 2 * (wCtr[x])
b[x] = 2 * wCtr[x]
for y in xrange(0,n):
A[x,x] += 2 * smoothness[x,y]
A[x,y] -= 2 * smoothness[x,y]
x = np.linalg.solve(A, b)
return x
def path_length(path,G):
dist = 0.0
for i in xrange(1,len(path)):
dist += G[path[i - 1]][path[i]]['weight']
return dist
def make_graph(grid):
# get unique labels
vertices = np.unique(grid)
# map unique labels to [1,...,num_labels]
reverse_dict = dict(zip(vertices,np.arange(len(vertices))))
grid = np.array([reverse_dict[x] for x in grid.flat]).reshape(grid.shape)
# create edges
down = np.c_[grid[:-1, :].ravel(), grid[1:, :].ravel()]
right = np.c_[grid[:, :-1].ravel(), grid[:, 1:].ravel()]
all_edges = np.vstack([right, down])
all_edges = all_edges[all_edges[:, 0] != all_edges[:, 1], :]
all_edges = np.sort(all_edges,axis=1)
num_vertices = len(vertices)
edge_hash = all_edges[:,0] + num_vertices * all_edges[:, 1]
# find unique connections
edges = np.unique(edge_hash)
# undo hashing
edges = [[vertices[x%num_vertices],
vertices[x/num_vertices]] for x in edges]
return vertices, edges
def get_saliency_rbd(img_path):
# Saliency map calculation based on:
# Saliency Optimization from Robust Background Detection, Wangjiang Zhu, Shuang Liang, Yichen Wei and Jian Sun, IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2014
img = cv2.imread(img_path)
if len(img.shape) != 3: # got a grayscale image
img = gray2rgb(img)
img_lab = img_as_float(rgb2lab(img))
img_rgb = img_as_float(img)
img_gray = img_as_float(rgb2gray(img))
# p=SLIC(img_rgb,250,10);
# segments_slic = p.iterate_times(5)
segments_slic=slic(img_rgb, n_segments=250, compactness=10, sigma=1, enforce_connectivity=False)
# print 'hello'
num_segments = len(np.unique(segments_slic))
nrows, ncols = segments_slic.shape
max_dist = math.sqrt(nrows*nrows + ncols*ncols)
grid = segments_slic
(vertices,edges) = make_graph(grid)
gridx, gridy = np.mgrid[:grid.shape[0], :grid.shape[1]]
centers = dict()
colors = dict()
distances = dict()
boundary = dict()
for v in vertices:
centers[v] = [gridy[grid == v].mean(), gridx[grid == v].mean()]
colors[v] = np.mean(img_lab[grid==v],axis=0)
x_pix = gridx[grid == v]
y_pix = gridy[grid == v]
if np.any(x_pix == 0) or np.any(y_pix == 0) or np.any(x_pix == nrows - 1) or np.any(y_pix == ncols - 1):
boundary[v] = 1
else:
boundary[v] = 0
G = nx.Graph()
#buid the graph
for edge in edges:
pt1 = edge[0]
pt2 = edge[1]
color_distance = scipy.spatial.distance.euclidean(colors[pt1],colors[pt2])
G.add_edge(pt1, pt2, weight=color_distance )
#add a new edge in graph if edges are both on boundary
for v1 in vertices:
if boundary[v1] == 1:
for v2 in vertices:
if boundary[v2] == 1:
color_distance = scipy.spatial.distance.euclidean(colors[v1],colors[v2])
G.add_edge(v1,v2,weight=color_distance)
geodesic = np.zeros((len(vertices),len(vertices)),dtype=float)
spatial = np.zeros((len(vertices),len(vertices)),dtype=float)
smoothness = np.zeros((len(vertices),len(vertices)),dtype=float)
adjacency = np.zeros((len(vertices),len(vertices)),dtype=float)
sigma_clr = 10.0
sigma_bndcon = 1.0
sigma_spa = 0.25
mu = 0.1
all_shortest_paths_color = nx.shortest_path(G,source=None,target=None,weight='weight')
for v1 in vertices:
for v2 in vertices:
if v1 == v2:
geodesic[v1,v2] = 0
spatial[v1,v2] = 0
smoothness[v1,v2] = 0
else:
geodesic[v1,v2] = path_length(all_shortest_paths_color[v1][v2],G)
spatial[v1,v2] = scipy.spatial.distance.euclidean(centers[v1],centers[v2]) / max_dist
smoothness[v1,v2] = math.exp( - (geodesic[v1,v2] * geodesic[v1,v2])/(2.0*sigma_clr*sigma_clr)) + mu
for edge in edges:
pt1 = edge[0]
pt2 = edge[1]
adjacency[pt1,pt2] = 1
adjacency[pt2,pt1] = 1
for v1 in vertices:
for v2 in vertices:
smoothness[v1,v2] = adjacency[v1,v2] * smoothness[v1,v2]
area = dict()
len_bnd = dict()
bnd_con = dict()
w_bg = dict()
ctr = dict()
wCtr = dict()
for v1 in vertices:
area[v1] = 0
len_bnd[v1] = 0
ctr[v1] = 0
for v2 in vertices:
d_app = geodesic[v1,v2]
d_spa = spatial[v1,v2]
w_spa = math.exp(- ((d_spa)*(d_spa))/(2.0*sigma_spa*sigma_spa))
area_i = S(v1,v2,geodesic)
area[v1] += area_i
len_bnd[v1] += area_i * boundary[v2]
ctr[v1] += d_app * w_spa
bnd_con[v1] = len_bnd[v1] / math.sqrt(area[v1])
w_bg[v1] = 1.0 - math.exp(- (bnd_con[v1]*bnd_con[v1])/(2*sigma_bndcon*sigma_bndcon))
for v1 in vertices:
wCtr[v1] = 0
for v2 in vertices:
d_app = geodesic[v1,v2]
d_spa = spatial[v1,v2]
w_spa = math.exp(- (d_spa*d_spa)/(2.0*sigma_spa*sigma_spa))
wCtr[v1] += d_app * w_spa * w_bg[v2]
# normalise value for wCtr
min_value = min(wCtr.values())
max_value = max(wCtr.values())
minVal = [key for key, value in wCtr.iteritems() if value == min_value]
maxVal = [key for key, value in wCtr.iteritems() if value == max_value]
for v in vertices:
wCtr[v] = (wCtr[v] - min_value)/(max_value - min_value)
img_disp1 = img_gray.copy()
img_disp2 = img_gray.copy()
x = compute_saliency_cost(smoothness,w_bg,wCtr)
for v in vertices:
img_disp1[grid == v] = x[v]
img_disp2 = img_disp1.copy()
sal = np.zeros((img_disp1.shape[0],img_disp1.shape[1],3))
sal = img_disp2
sal_max = np.max(sal)
sal_min = np.min(sal)
sal = 255 * ((sal - sal_min) / (sal_max - sal_min))
return sal
if __name__ == '__main__':
if(len(argv)!=2):
print 'python Saliency_Optimization #ImageName'
filename=sys.argv[1];
rbd = get_saliency_rbd(filename).astype('uint8')
adaptive_threshold = 2.0 * rbd.mean()
final_binary= (rbd > adaptive_threshold)
# img = cv2.imread(filename)
# cv2.imshow('img',rbd)
# cv2.imwrite('Saliency2_'+filename,rbd)
# cv2.imwrite('Mask2_'+filename,255 * final_binary.astype('uint8'))
# cv2.imshow('binary',255 * final_binary.astype('uint8'))
# cv2.waitKey(0)
# cv2.destroyAllWindows()
fig = plt.figure("gray_image")
ax = fig.add_subplot(1,1,1)
ax.imshow(rbd)
plt.axis("off")
plt.show()