-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmain.py
254 lines (210 loc) · 8.62 KB
/
main.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
245
246
247
248
249
250
251
252
253
254
# -*- coding: utf-8 -*-
# Copyright (C) 2017 Carlos Pérez Ramil
# This file is part of Thumbstick Deadzones project.
# The Thumbstick Deadzones project is free software: you can redistribute it
# and/or modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# The Thumbstick Deadzones project is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with the Thumbstick Deadzones project.
# If not, see <http://www.gnu.org/licenses/>.
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
# UTILS
def map_range(v, old_min, old_max, new_min, new_max):
return (new_min + (new_max - new_min) * (v - old_min) / (old_max - old_min))
# DEADZONE TYPES
def dz_none(stick_input, deadzone):
return stick_input[0], stick_input[1]
def dz_axial_x(stick_input, deadzone):
x_val = stick_input[0] if abs(stick_input[0]) > deadzone else 0
return x_val, 0
def dz_axial_y(stick_input, deadzone):
y_val = stick_input[1] if abs(stick_input[1]) > deadzone else 0
return 0, y_val
def dz_axial(stick_input, deadzone):
x_val = stick_input[0] if abs(stick_input[0]) > deadzone else 0
y_val = stick_input[1] if abs(stick_input[1]) > deadzone else 0
return x_val, y_val
def dz_sloped_axial_x(stick_input, deadzone):
deadzone_x = deadzone * abs(stick_input[1])
x_val = stick_input[0] if abs(stick_input[0]) > deadzone_x else 0
return x_val, 0
def dz_sloped_axial_y(stick_input, deadzone):
deadzone_y = deadzone * abs(stick_input[0])
y_val = stick_input[1] if abs(stick_input[1]) > deadzone_y else 0
return 0, y_val
def dz_sloped_axial(stick_input, deadzone):
deadzone_x = deadzone * abs(stick_input[1])
deadzone_y = deadzone * abs(stick_input[0])
x_val = stick_input[0] if abs(stick_input[0]) > deadzone_x else 0
y_val = stick_input[1] if abs(stick_input[1]) > deadzone_y else 0
return x_val, y_val
def dz_radial(stick_input, deadzone):
input_magnitude = np.linalg.norm(stick_input)
if input_magnitude < deadzone:
return 0, 0
else:
return stick_input[0], stick_input[1]
def dz_scaled_axial_x(stick_input, deadzone):
x_val = 0
sign = np.sign(stick_input[0])
if abs(stick_input[0]) > deadzone:
x_val = sign * map_range(abs(stick_input[0]), deadzone, 1, 0, 1)
return x_val, 0
def dz_scaled_axial_y(stick_input, deadzone):
y_val = 0
sign = np.sign(stick_input[1])
if abs(stick_input[1]) > deadzone:
y_val = sign * map_range(abs(stick_input[1]), deadzone, 1, 0, 1)
return 0, y_val
def dz_scaled_axial(stick_input, deadzone):
x_val = 0
y_val = 0
sign = np.sign(stick_input)
if abs(stick_input[0]) > deadzone:
x_val = sign[0] * map_range(abs(stick_input[0]), deadzone, 1, 0, 1)
if abs(stick_input[1]) > deadzone:
y_val = sign[1] * map_range(abs(stick_input[1]), deadzone, 1, 0, 1)
return x_val, y_val
def dz_sloped_scaled_axial_x(stick_input, deadzone):
x_val = 0
deadzone_x = deadzone * abs(stick_input[1])
sign = np.sign(stick_input[0])
if abs(stick_input[0]) > deadzone_x:
x_val = sign * map_range(abs(stick_input[0]), deadzone_x, 1, 0, 1)
return x_val, 0
def dz_sloped_scaled_axial_y(stick_input, deadzone):
y_val = 0
deadzone_y = deadzone * abs(stick_input[0])
sign = np.sign(stick_input[1])
if abs(stick_input[1]) > deadzone_y:
y_val = sign * map_range(abs(stick_input[1]), deadzone_y, 1, 0, 1)
return 0, y_val
def dz_sloped_scaled_axial(stick_input, deadzone, n=1):
x_val = 0
y_val = 0
deadzone_x = deadzone * np.power(abs(stick_input[1]), n)
deadzone_y = deadzone * np.power(abs(stick_input[0]), n)
sign = np.sign(stick_input)
if abs(stick_input[0]) > deadzone_x:
x_val = sign[0] * map_range(abs(stick_input[0]), deadzone_x, 1, 0, 1)
if abs(stick_input[1]) > deadzone_y:
y_val = sign[1] * map_range(abs(stick_input[1]), deadzone_y, 1, 0, 1)
return x_val, y_val
def dz_scaled_radial(stick_input, deadzone):
input_magnitude = np.linalg.norm(stick_input)
if input_magnitude < deadzone:
return 0, 0
else:
input_normalized = stick_input / input_magnitude
# Formula:
# max_value = 1
# min_value = 0
# retval = input_normalized * (min_value + (max_value - min_value) * ((input_magnitude - deadzone) / (max_value - deadzone)))
retval = input_normalized * map_range(input_magnitude, deadzone, 1, 0, 1)
return retval[0], retval[1]
def dz_hybrid(stick_input, deadzone):
# First, check that input does not fall within deadzone
input_magnitude = np.linalg.norm(stick_input)
if input_magnitude < deadzone:
return 0, 0
# Then apply a scaled_radial transformation
partial_output = dz_scaled_radial(stick_input, deadzone)
# Then apply a sloped_scaled_axial transformation
final_output = dz_sloped_scaled_axial(partial_output, deadzone)
return final_output
def dz_exp(stick_input, deadzone, n=3):
partial_output = dz_scaled_radial(stick_input, deadzone)
input_magnitude = np.linalg.norm(partial_output)
if input_magnitude == 0:
return 0, 0
input_normalized = partial_output / input_magnitude
return input_normalized * np.power(input_magnitude, n)
def dz_scaled_radial_inner_and_outer(stick_input, inner_deadzone, outer_deadzone=0.15):
input_magnitude = np.linalg.norm(stick_input)
if input_magnitude < inner_deadzone:
return 0, 0
elif input_magnitude > (1 - outer_deadzone):
return stick_input / input_magnitude
else:
input_normalized = stick_input / input_magnitude
retval = input_normalized * map_range(input_magnitude, inner_deadzone, 1 - outer_deadzone, 0, 1)
return retval[0], retval[1]
################################################################################
# INPUT PARAMETERS
height = 400
width = 400
center = (height/2, width/2)
deadzone = 0.2
deadzone_function = dz_hybrid
mode = 'rgb'
def generate_gray_image():
# Base blank image
img = np.full((height, width), 0, np.uint8)
# Draw gradient and deadzone
for i in range(height):
for j in range(width):
# Simulate stick input
fake_stick_input = np.array([j - center[1], i - center[0]], dtype=float)
fake_stick_input /= (height / 2)
# Clamp fake input to stick boundaries
magnitude = np.linalg.norm(fake_stick_input)
if magnitude > 1.0:
img[i,j] = 0
else:
# Compute deadzoned value
n, m = deadzone_function(fake_stick_input, deadzone)
dz_magnitude = np.linalg.norm([n,m])
img[i,j] = map_range(dz_magnitude, 0, 1, 0, 255)
# Print image
plt.imshow(img, cmap=plt.cm.gray, vmin=0, vmax=255,
aspect='equal', interpolation='bilinear')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.gca().invert_yaxis()
plt.show()
return img
def generate_rgb_image():
# Base blank image
img = np.full((height, width, 3), 0, np.uint8)
# Draw gradient and deadzone
for i in range(height):
for j in range(width):
# Simulate stick input
fake_stick_input = np.array([j - center[1], i - center[0]], dtype=float)
fake_stick_input /= (height / 2)
# Clamp fake input to stick boundaries
magnitude = np.linalg.norm(fake_stick_input)
if magnitude > 1.0:
img[i,j,0] = 0
else:
# Compute deadzoned value
n, m = deadzone_function(fake_stick_input, deadzone)
img[i,j,0] = map_range(abs(m), 0, 1, 0, 255)
img[i,j,2] = map_range(abs(n), 0, 1, 0, 255)
# Print image
plt.imshow(cv.cvtColor(img, cv.COLOR_BGR2RGB), vmin=0, vmax=255,
aspect='equal', interpolation='bilinear')
plt.xticks([]), plt.yticks([]) # to hide tick values on X and Y axis
plt.gca().invert_yaxis()
plt.show()
return img
def main():
img = None
if mode == 'gray':
img = generate_gray_image()
elif mode == 'rgb':
img = generate_rgb_image()
# Listen for input
command = input()
if command == 's':
print("File name: ")
filename = input()
cv.imwrite(filename, img)
if __name__ == '__main__':
main()