-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathregion_proposal.py
71 lines (63 loc) · 1.95 KB
/
region_proposal.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
# Python version: 2.7.x
import skimage.data
import skimage.io
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import numpy as np
import selectivesearch
from skimage import transform,data
from overlap import overlap
def main():
# loading image
from skimage import io
img = io.imread('example/dog (1).JPEG')
# img = skimage.data.astronaut()
io.imshow(img)
endpoint = np.arange(4)
cut_photo = np.arange(154587)
# perform selective search
img_lbl, regions = selectivesearch.selective_search(
img, scale=500, sigma=0.9, min_size=10)
candidates = set()
for r in regions:
repeted = False
# excluding same rectangle (with different segments)
if r['rect'] in candidates:
continue
# excluding regions smaller than 2000 pixels
if r['size'] < 2000:
continue
# distorted rects
x, y, w, h = r['rect']
a1 = x
a2 = y
a3 = w
a4 = h
if w / h > 1.2 or h / w > 1.2:
continue
# remove the repeated area
for x, y, w, h in candidates:
if overlap(a1,a2,a3,a4,x,y,w,h) > 0.9:
repeted = True
break
if repeted == False:
candidates.add(r['rect'])
# save the new photo
i = 1
for x, y, w, h in candidates:
print x, y, w, h
cut_area = img[y:y+h,x:x+w,:]
io.imsave('C:\Users\eric\selectivesearch\segements\\' + str(i) +'.jpg',cut_area)
i = i+1
out = transform.resize(cut_area, (227, 227))
temp1 = np.array([x,y,w,h])
temp2 = out
temp2 = np.array(temp2,dtype=np.float32)
temp2 = temp2.reshape(1,154587)
endpoint = np.vstack((endpoint,temp1))
cut_photo = np.vstack((cut_photo,temp2))
# save the np.array
np.save("cut_photo.npy", cut_photo)
np.save("endpoint_4.npy", endpoint)
if __name__ == "__main__":
main()