-
Notifications
You must be signed in to change notification settings - Fork 0
/
q3_functions.py
244 lines (176 loc) · 5.03 KB
/
q3_functions.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
#!/usr/bin/env python
# coding: utf-8
import numpy as np
import random
import cv2
import matplotlib.pyplot as plt
from detect import detect
from detect_col import detect_col
import random
import time
# generate base image
def genMat(size):
return np.array([[[255]*3]*size*10]*size*10)
#generate random points for experiments
def getPoints(random_nums, size):
points = []
x = 0
t = [x]
for i in range(9):
x = x + size
t.append(x)
for i in t:
for j in t:
points.append((i, j))
return points
# shape map for feature search
def shape_map_f(M, req_points):
for i in req_points:
flag, corners = detect(M[i[0]:i[0]+72, i[1]:i[1]+72,:])
if flag == 1:
#print('detected triangle at point', i)
break
# color map for feature search
def color_map_f(M, req_points):
for i in req_points:
flag = detect_col(M[i[0]:i[0]+72, i[1]:i[1]+72,:])
if flag == 1:
#print('blue color detected at point',i)
break
# generate image containg different shape of color for feature search
def genFeature(random_nums, size=72,in_shape = True,in_col = False):
req_points = []
M = genMat(size)
points = getPoints(random_nums, size)
sel_list = random.sample(range(100), random_nums)
x = points[sel_list[0]][0]
y = points[sel_list[0]][1]
req_points.append((x,y))
dec = .6
if in_shape == True:
if dec >.5:
M[x:x+72, y:y+72, :] = red_tri
for i in sel_list[1:]:
x = points[i][0]
y = points[i][1]
M[x:x+72, y:y+72] = red_sqr
req_points.append((x,y))
else:
M[x:x+72, y:y+72, :] = blue_tri
for i in sel_list[1:]:
x = points[i][0]
y = points[i][1]
M[x:x+72, y:y+72] = blue_sqr
req_points.append((x,y))
if in_col == True:
if dec > .5:
M[x:x+72, y:y+72, :] = blue_sqr
for i in sel_list[1:]:
x = points[i][0]
y = points[i][1]
M[x:x+72, y:y+72] = red_sqr
req_points.append((x,y))
else:
M[x:x+72, y:y+72, :] = red_tri
for i in sel_list[1:]:
x = points[i][0]
y = points[i][1]
M[x:x+72, y:y+72] = blue_tri
req_points.append((x,y))
#make an thresholding function
return M, req_points,in_col
# imaages
red_tri = cv2.resize(cv2.cvtColor(cv2.imread('red_tri.png'), cv2.COLOR_BGR2RGB), (72, 72))
blue_tri = cv2.resize(cv2.cvtColor(cv2.imread('blue_tri.png'), cv2.COLOR_BGR2RGB), (72, 72))
red_sqr = cv2.resize(cv2.cvtColor(cv2.imread('red_sqr.png'), cv2.COLOR_BGR2RGB), (72, 72))
blue_sqr = cv2.resize(cv2.cvtColor(cv2.imread('blue_sqr.png'), cv2.COLOR_BGR2RGB), (72, 72))
# making shape of different color for conjucture search
def make_shape(shape_odd,color_odd):
if shape_odd == 0:
if color_odd ==0:
return red_sqr
else:
return blue_sqr
else:
if color_odd == 0:
return red_tri
else:
return blue_tri
# shape map for conjucture search
def shape_map(M, req_points):
flags = []
for i in req_points:
flag, corners = detect(M[i[0]:i[0]+72, i[1]:i[1]+72,:])
if flag == 0:
flags.append(0)
if flag == 1:
flags.append(1)
return flags
# color map for conjucture map
def color_map(M, req_points):
flags =[]
for i in req_points:
flag = detect_col(M[i[0]:i[0]+72, i[1]:i[1]+72,:])
if flag == 0:
flags.append(0)
if flag == 1:
flags.append(1)
return flags
# making image for conjucture search
def conjucmake(random_nums, size=72,shape_odd = 0,color_odd = 0): # 0 == red 0 == square,
req_points = []
M = genMat(size)
points = getPoints(random_nums, size)
sel_list = random.sample(range(100), random_nums)
x,y = points[sel_list.pop()]
req_points.append((x,y))
M[x:x+72, y:y+72] = make_shape(shape_odd,color_odd)
#print(shape_odd,color_odd)
random_nums -=1
kk = int(random_nums/2)
k = random.randint(0,kk)
random_nums -= k
#print(k)
#print(sel_list)
for i in range(k):
pp = sel_list.pop()
x,y = points[pp]
M[x:x+72, y:y+72] = make_shape(shape_odd,1 -color_odd)
#print(1-shape_odd,color_odd)
req_points.append((x,y))
k = random.randint(0,random_nums)
#print(k)
for i in range(k):
pp = sel_list.pop()
x,y = points[pp]
M[x:x+72, y:y+72] = make_shape(1-shape_odd,1-color_odd)
#print(shape_odd,color_odd)
req_points.append((x,y))
#print(random_nums)
for i in range(k,random_nums):
pp = sel_list.pop()
x,y = points[pp]
M[x:x+72, y:y+72] = make_shape(1-shape_odd,color_odd)
#print(1-shape_odd,color_odd)
req_points.append((x,y))
return M,req_points
def runconj(M,req_points_,shape_odd = 0,color_odd = 0):
random.shuffle(req_points_ ) # points where to look for conjucture search
shape_flag = shape_map(M,req_points_) # making shape map for the image
color_flag = color_map(M,req_points_) # making color map for the image
for i in range(len(shape_flag)):
time.sleep(0.035)
if shape_flag[i] == shape_odd:
if color_flag[i] ==color_odd: # searching for the odd feature like search for red sqaure or blue triangle
break
if shape_odd == 0:
if color_odd ==0:
k = 'Red Square'
else:
k = 'Blue Square'
else:
if color_odd == 0:
k = 'Red Triangle'
else:
k = 'Blue Triangle'
#print(k,'is detected at',req_points_[i])