-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathsquare_decision.py
137 lines (93 loc) · 4.58 KB
/
square_decision.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
import sys
import numpy as np
def gen_fixed_coords(coordDict, translate_x, translate_y, translate_x_small, translate_y_small):
fixedCoords = {}
for key in coordDict.keys():
fixedCoords[(key[0]-translate_x, key[1]-translate_y)] = (coordDict[key][0] - translate_x_small, coordDict[key][1] - translate_y_small)
return fixedCoords
def gen_scale_factor(coordDict):
(xLarge, yLarge) = list(coordDict.keys())[1]
(xSmall, ySmall) = coordDict[list(coordDict.keys())[1]]
largeDist = np.sqrt((xLarge ** 2 + yLarge **2))
smallDist = np.sqrt((xSmall ** 2 + ySmall ** 2))
scaleFactor = largeDist / smallDist
return scaleFactor
# scaledCoords = {}
# for key in coordDict.keys():
# scaledCoords[list(key)] = np.array([coordDict[key][0] * scaleFactor, coordDict[key][1] * scaleFactor])
# return scaledCoords
def gen_theta(fixedCoords):
largerCoords = list(fixedCoords.keys())
theta = np.arctan2(fixedCoords[list(fixedCoords.keys())[1]][0], fixedCoords[list(fixedCoords.keys())[1]][1]) - np.arctan2(list(fixedCoords.keys())[1][0], list(fixedCoords.keys())[1][1])
return theta
def gen_transform_matrix(translate_x=0, translate_y=0, theta=0, scale_factor=1, translate_x_large=0, translate_y_large=0):
translate_matrix = np.matrix([[1, 0, translate_x], [0, 1, translate_y], [0, 0, 1]])
rotation_matrix = np.matrix([[np.cos(theta), -1*np.sin(theta), 0], [np.sin(theta), np.cos(theta), 0], [0, 0, 1]])
scale_matrix = np.matrix([[scale_factor, 0, 0], [0, scale_factor, 0], [0, 0, 1]])
translate_matrix_2 = np.matrix([[1, 0, translate_x_large], [0, 1, translate_y_large], [0, 0, 1]])
#scale, rotate, translate but in reverse so translate, rotate, scale is the order to multiply
#transform_matrix = np.matmul(translate_matrix_2, translate_matrix)
transform_matrix = translate_matrix
#transform_matrix = np.matmul(rotation_matrix, transform_matrix)
transform_matrix = np.matmul(scale_matrix, transform_matrix)
transform_matrix = np.matmul(rotation_matrix, transform_matrix)
#print("\nTransform matrix before multiplying by scale: ", transform_matrix, "\n")
return np.matmul(translate_matrix_2, transform_matrix)
def gen_aggregate_matrix( toMap, local):
return np.matmul(toMap, local)
#20x20 grid
"""
1,1 | 2,1 | 3,1 | ... | 1, 1
...
...
1,20 | 2,20 | 3,20 | ... | 20, 20
"""
def guess(x_coord: float, y_coord: float) -> int:
###
#TODO: Make guess correspond to the actual image, eg define the drid in terms of coordinate system, in fact, define entire image in terms of coordinate system
###
foundX = False
foundY = False
xGuess = 13
yGuess = 13
for i in range(1, 20):
if( 250 * i > float(x_coord) and not foundX):
xGuess = i
foundX = True
if(250 * i > float(y_coord) and not foundY):
yGuess = i
foundY = True
return(xGuess, yGuess)
def gen_coords(x_coord: float, y_coord: float, transformation_matrices):
"""
Takes coordinates as input, outputs location relative to the original image
param x_coord: x coordinate from image we want to convert into original coordinate system
param y_coord: y coordinate from image we want to convert into original coordinate system
param tramsformation_matrices: array transformation matrices in order of application (smallest to second smallest, etc) until we are back on image with orignal grid.
"""
a = np.matrix([x_coord, y_coord, 0.0])
for matrix in transformation_matrices:
a = np.matmul(a, matrix)
return(a.item(0,0), a.item(0,1))
#simple test method
if __name__ == "__main__":
for index in range(len(sys.argv)):
print(sys.argv[index])
print(guess(float(sys.argv[1]), float(sys.argv[2])))
print("\nSanity check for some matrix math\n")
testCoords = np.matrix([[1],[ 1], [1]])
#scale
scaleTest = np.matrix([[2, 0, 0], [0, 2, 0], [0, 0, 1]])
testCoords = np.matmul(scaleTest, testCoords);
#rotate
rotateTest = np.matrix([[0, 1, 0], [-1, 0, 0], [0, 0, 1]])
testCoords = np.matmul(rotateTest, testCoords);
print(testCoords)
#translate
translateTest = np.matrix([[1, 0, 3], [0, 1, 4], [0, 0, 1]])
testCoords = np.matmul(translateTest, testCoords)
print("Test coords: ", testCoords)
tester = gen_transform_matrix(3, 4, np.pi*3/2, 2)
print(tester)
print("Test transform: ", np.matmul(tester, [[1],[ 1], [1]]))
#We don't have to worry about the preceding translation to the origin because it's applied to every point within the image