-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselective_search.py
More file actions
363 lines (299 loc) · 11.7 KB
/
selective_search.py
File metadata and controls
363 lines (299 loc) · 11.7 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
import os
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import pandas as pd
import scipy.misc
import skimage.segmentation
import skimage.feature
from copy import copy
def image_segmentation(img_8bit, scale = 1.0, sigma = 0.8, min_size = 50):
'''
J.R.R. Uijlings's hierarchical grouping algorithm
== input ==
img_8bit : shape = (height, width, 3),
8-bits degital image (each digit ranges between 0 - 255)
== output ==
img : shape = (height, width, 4)
'''
# convert the image to range between 0 and 1
img_float = skimage.util.img_as_float(img_8bit)
im_mask = skimage.segmentation.felzenszwalb(
img_float,
scale = scale,
sigma = sigma,
min_size = min_size)
img = np.dstack([img_8bit,im_mask])
return(img)
def extract_region(img):
'''
For each segmented region,
extract smallest rectangle regions covering the smallest segmented region.
== input ==
img : (height, width, N channel)
N channel = [R, G, B , L], L = label for the region
only the L channel is used in this analysis
== output ==
dictionary R :
{0: {'labels': [0], 'max_x': 131, 'max_y': 74, 'min_x': 0, 'min_y': 0},
1: {'labels': [1], 'max_x': 189, 'max_y': 37, 'min_x': 75, 'min_y': 0},
2: {'labels': [2], 'max_x': 282, 'max_y': 38, 'min_x': 162, 'min_y': 0},
3: {'labels': [3], 'max_x': 499, 'max_y': 23, 'min_x': 247, 'min_y': 0},
4: {'labels': [4], 'max_x': 499, 'max_y': 74, 'min_x': 428, 'min_y': 13},
5: {'labels': [5], 'max_x': 441, 'max_y': 76, 'min_x': 333, 'min_y': 16},
6: {'labels': [6], 'max_x': 348, 'max_y': 80, 'min_x': 62, 'min_y': 21},
'''
## only use the segmentation output
img_segment = img[:,:,3]
R = {}
for y, i in enumerate(img_segment): ## along vertical axis
for x, l in enumerate(i): ## along horizontal axis
# initialize a new region
if l not in R:
R[l] = {"min_x": np.Inf,
"min_y": np.Inf,
"max_x": 0,
"max_y": 0,
"labels": [l]}
# bounding box
if R[l]["min_x"] > x:
R[l]["min_x"] = x
if R[l]["min_y"] > y:
R[l]["min_y"] = y
if R[l]["max_x"] < x:
R[l]["max_x"] = x
if R[l]["max_y"] < y:
R[l]["max_y"] = y
## remove region if it does not have positive height or positive width
Rcopy = copy(R)
for key in R.keys():
r = R[key]
if (r["min_x"] == r["max_x"]) or (r["min_y"] == r["max_y"]):
del Rcopy[key]
return(Rcopy)
def calc_texture_gradient(img):
"""
calculate texture gradient for entire image
The original SelectiveSearch algorithm proposed Gaussian derivative
for 8 orientations, but we use LBP instead.
output will be [height(*)][width(*)]
"""
ret = np.zeros(img.shape[:3])
for colour_channel in (0, 1, 2):
ret[:, :, colour_channel] = skimage.feature.local_binary_pattern(
img[:, :, colour_channel], 8, 1.0)
return ret
def calc_hsv(img):
hsv = skimage.color.rgb2hsv(img[:,:,:3])
return(hsv)
def calc_hist(img, minhist=0, maxhist=1):
"""
calculate colour histogram for each region
the size of output histogram will be BINS * COLOUR_CHANNELS(3)
number of bins is 25 as same as [uijlings_ijcv2013_draft.pdf]
extract HSV
len(hist) = BINS * 3
hist[:BINS] = [0, 10, 20, 0,...,0] meaning that
there are 10 pixels that have values between (maxhist - minhist)/BINS*1 and (maxhist - minhist)/BINS*2
there are 10 pixels that have values between (maxhist - minhist)/BINS*2 and (maxhist - minhist)/BINS*3
"""
BINS = 25
hist = np.array([])
for colour_channel in range(3):
# extracting one colour channel
c = img[:, colour_channel]
# calculate histogram for each colour and join to the result
hist = np.concatenate(
[hist] + [np.histogram(c, BINS,
# The lower and upper range of the bins.
(minhist, maxhist))[0]])
# L1 normalize
hist = hist / len(img)
return hist
def augment_regions_with_histogram_info(tex_grad, img, R,hsv,tex_trad):
for k, v in list(R.items()):
## height and width axies are flattened.
## masked_pixel.shape = (N pixel with this mask ID , 3)
masked_pixels = hsv[img[:, :, 3] == k]
R[k]["size"] = len(masked_pixels / 4)
R[k]["hist_c"] = calc_hist(masked_pixels,minhist=0, maxhist=1)
# texture histogram
R[k]["hist_t"] = calc_hist(tex_grad[img[:, :, 3] == k],minhist=0, maxhist=2**8-1)
return(R)
def extract_neighbours(regions):
'''
check if two regions intersect
'''
def intersect(a, b):
if (a["min_x"] < b["min_x"] < a["max_x"] and a["min_y"] < b["min_y"] < a["max_y"]) or\
(a["min_x"] < b["max_x"] < a["max_x"] and a["min_y"] < b["max_y"] < a["max_y"]) or\
(a["min_x"] < b["min_x"] < a["max_x"] and a["min_y"] < b["max_y"] < a["max_y"]) or\
(a["min_x"] < b["max_x"] < a["max_x"] and a["min_y"] < b["min_y"] < a["max_y"]):
return True
return False
R = list(regions.items())
neighbours = []
for cur, a in enumerate(R[:-1]):
for b in R[cur + 1:]:
if intersect(a[1], b[1]):
neighbours.append((a, b))
return neighbours
def _sim_colour(r1, r2):
"""
calculate the sum of histogram intersection of colour
"""
return sum([min(a, b) for a, b in zip(r1["hist_c"], r2["hist_c"])])
def _sim_texture(r1, r2):
"""
calculate the sum of histogram intersection of texture
"""
return sum([min(a, b) for a, b in zip(r1["hist_t"], r2["hist_t"])])
def _sim_size(r1, r2, imsize):
"""
calculate the size similarity over the image
"""
return 1.0 - (r1["size"] + r2["size"]) / float(imsize)
def _sim_fill(r1, r2, imsize):
"""
calculate the fill similarity over the image
"""
bbsize = (
(max(r1["max_x"], r2["max_x"]) - min(r1["min_x"], r2["min_x"]))
* (max(r1["max_y"], r2["max_y"]) - min(r1["min_y"], r2["min_y"]))
)
return 1.0 - (bbsize - r1["size"] - r2["size"]) / imsize
def calc_sim(r1, r2, imsize):
return (_sim_colour(r1, r2) +\
_sim_texture(r1, r2) +\
_sim_size(r1, r2, imsize) +\
_sim_fill(r1, r2, imsize))
def calculate_similarlity(img,neighbours,verbose=False):
# calculate initial similarities
imsize = img.shape[0] * img.shape[1]
S = {}
for (ai, ar), (bi, br) in neighbours:
S[(ai, bi)] = calc_sim(ar, br, imsize)
if verbose:
print("S[({:2.0f}, {:2.0f})]={:3.2f}".format(ai,bi,S[(ai, bi)]))
return(S)
def merge_regions(r1, r2):
'''
Suppose r1 and r2 look like:
(minx1, miny1)
_____________________
| |
| (minx2,maxy2) |
| ________|____
| | | |
| | | |
| |________|____|(maxx2, maxy2)
|___________________|(maxx1, maxy1)
Then for merged region,
minx : minx1
miny : miny1
maxx : maxx2
maxy : maxy1
size : the number of pixels in the two regions (intersection is counted twice?)
'''
new_size = r1["size"] + r2["size"]
rt = {
"min_x": min(r1["min_x"], r2["min_x"]),
"min_y": min(r1["min_y"], r2["min_y"]),
"max_x": max(r1["max_x"], r2["max_x"]),
"max_y": max(r1["max_y"], r2["max_y"]),
"size": new_size,
"hist_c": (r1["hist_c"] * r1["size"] + r2["hist_c"] * r2["size"]) / new_size,
"hist_t": (r1["hist_t"] * r1["size"] + r2["hist_t"] * r2["size"]) / new_size,
"labels": r1["labels"] + r2["labels"]
}
return rt
def merge_regions_in_order(S,R,imsize, verbose=False):
'''
== Input ==
S : similarity dictionary
R : dictionary of proposed regions
== Output ==
regions : list of regions
'''
# hierarchal search
while S != {}:
# Step 1: get highest similarity pair of regions from the similarlity dictionary
i, j = sorted(S.items(), key=lambda i: i[1])[-1][0]
# Step 2: marge the region pair and add to the region dictionary
t = max(R.keys()) + 1.0
R[t] = merge_regions(R[i], R[j])
# Step 3: from the similarity dictionary,
# remove all the pair of regions where one of the regions is selected in Step 1
key_to_delete = []
for k, v in list(S.items()):
if (i in k) or (j in k):
key_to_delete.append(k)
for k in key_to_delete:
del S[k]
# Step 4: calculate similarity with new merged region and the regions and its intersecting region
# (intersecting region is the region that are to be deleted)
for k in key_to_delete:
if k != (i,j):
if k[0] in (i, j):
n = k[1]
else:
n = k[0]
S[(t, n)] = calc_sim(R[t], R[n], imsize)
if verbose:
print("{} regions".format(len(R)))
## finally return list of region proposal
regions = []
for k, r in list(R.items()):
regions.append({
'rect': (
r['min_x'], # min x
r['min_y'], # min y
r['max_x'] - r['min_x'], # width
r['max_y'] - r['min_y']),# height
'size': r['size'],
'labels': r['labels']
})
return(regions)
def get_region_proposal(img_8bit,min_size = 500):
img = image_segmentation(img_8bit,min_size = min_size)
R = extract_region(img)
tex_grad = calc_texture_gradient(img)
hsv = calc_hsv(img)
R = augment_regions_with_histogram_info(tex_grad, img, R,hsv,tex_grad)
del tex_grad, hsv
neighbours = extract_neighbours(R)
S = calculate_similarlity(img,neighbours)
regions = merge_regions_in_order(S,R,imsize = img.shape[0] * img.shape[1])
return(regions)
def get_IOU(xmin1,ymin1,xmax1,ymax1,
xmin2,ymin2,xmax2,ymax2):
'''
(minx1, miny1)
_____________________
| |
| (minx2,maxy2) |
| ________|____
| | | |
| | | |
| |________|____|(maxx2, maxy2)
|___________________|(maxx1, maxy1)
'''
def get_wha(xmin1,xmax1,ymin1,ymax1):
width1 = xmax1 - xmin1
height1 = ymax1 - ymin1
area1 = width1 * height1
return(width1,height1,area1)
width1,height1,area1 = get_wha(xmin1,xmax1,ymin1,ymax1)
width2,height2,area2 = get_wha(xmin2,xmax2,ymin2,ymax2)
int_xmin = np.max([xmin1,xmin2])
int_ymin = np.max([ymin1,ymin2])
int_xmax = np.min([xmax1,xmax2])
int_ymax = np.min([ymax1,ymax2])
int_width = int_xmax - int_xmin
int_height = int_ymax - int_ymin
int_area = int_width*int_height
if (int_width < 0) or (int_height < 0):
IOU = 0
else:
IOU = int_area / float(area1 + area2 - int_area)
return(IOU)