-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseq.py
124 lines (99 loc) · 2.6 KB
/
seq.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
import sys
sys.path.append("/home/appl/opencv-2.4.6.1/lib/python2.6/site-packages")
import cv2
from common import anorm, getsize
import os, random
def read_csv(filename):
f = open(filename)
lines = f.readlines()
f.close()
res = []
for line in lines:
tm = line.split(",")
tmp = []
tmp.append(tm[0].strip("\t"))
tmp.append(tm[1].strip("\t"))
res.append(tmp)
return res
def return_random_point(num):
res = []
for i in xrange(num):
tmp = []
ran_x = random.randint(0,1538)
ran_y = random.randint(0,1376)
tmp.append(ran_x)
tmp.append(ran_y)
res.append(tmp)
return res
def cutout(img ,csv, file_name, size):
print img
src = cv2.imread(img, 1)
i = 0
##size of capture
half = size/2
print half
for point in csv:
dst = src[int(point[1])-half:int(point[1])+half, int(point[0])-half:int(point[0])+half]
i += 1
tmp_name = file_name.split("/")
tmp = tmp_name[-1] + str(i) +"_random.jpg"
try:
if len(dst[0]) == size and len(dst) == size:
cv2.imwrite(tmp, dst)
except:
print "Not found"
def set_path(img):
name = img.split("/")
return name[-1][0:8] + ".tiff"
def get_distance(y, x, qy, qx):
tmp_y = (y-qy)*(y-qy)
tmp_x = (x-qx)*(x-qx)
return (tmp_y+tmp_x)
def check_label(csv,yt,xt,size):
center_y = yt + size/2
center_x = xt + size/2
for pos in csv:
distance = get_distance(center_y, center_x, int(pos[1]), int(pos[0]))
if distance < 1060:
print "distance close"
return 1
return 0
def cutout_seq(img, csv, size):
print img
src = cv2.imread(img, 1)
quart = size/3
x_pix = len(src[0])
y_pix = len(src)
x_time = x_pix/quart
y_time = y_pix/quart
j = 0
for yt in xrange(y_time):
i = 0
for xt in xrange(x_time):
dst = src[yt*quart: yt*quart+size, xt*quart:xt*quart+size]
tmp_name = img.split("/")
try:
if len(dst[0]) == size and len(dst) == size:
if check_label(csv, yt*quart, xt*quart, size) == int(1):
tmp = tmp_name[-1] + str(i) + "_" + str(j) +"_seq_true.jpg"
tmp_f = "true/" + tmp
cv2.imwrite(tmp_f, dst)
else:
tmp = tmp_name[-1] + str(i) + "_" + str(j) +"_seq_false.jpg"
tmp_f = "false/" + tmp
cv2.imwrite(tmp_f, dst)
except:
print "Not found"
i += 1
j += 1
if __name__ == "__main__":
argvs = sys.argv
filename = argvs[1]
img = argvs[2] + set_path(argvs[1])
#img = argvs[2]
print img
print "------"
csv = read_csv(filename)
#csv = return_random_point(5)
#cutout(img, csv, filename, 90)
cutout_seq(img, csv, 90)