-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel_check.py
More file actions
163 lines (117 loc) · 5.36 KB
/
pixel_check.py
File metadata and controls
163 lines (117 loc) · 5.36 KB
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
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import numpy as np
import datetime
from gradient import *
import sys, time
sys.path.append("../catkin_ws/src/scr_control/scripts/lights")
sys.path.append("../catkin_ws/src/scr_control/scripts/time_of_flight")
sys.path.append("../catkin_ws/src/scr_control/scripts/color_sensors")
import SCR_OctaLight_client as light_control
import SCR_TOF_client as tof
import SCR_COS_client as cos
def tof_pixel_check(tof_id, loop_count=10, freq=0.1, threshold=500, bar_chart=True, grid=True, bar_fn="ToF_bar_chart.png", grid_fn="ToF_grid_figure.png"):
tof_data_list = tof.get_distances(tof_id)
change_freq_matrix = [[[] for j in range(len(tof_data_list[0]))] for i in range(len(tof_data_list))]
# store the current data in the ToF matrix
tof_origin = tof_data_list
# detect the new data from the ToF sensor in the loop
# and return the changes of data
for itr_number in range(0, loop_count):
# create a new matrix to store the changes of data
data_change_matrix = [[0]*len(tof_data_list[0]) for i in range(len(tof_data_list))]
# read the new data matrix
tof_data_list = tof.get_distances(int(tof_id))
# calculate and store the changes
for i in range(0, len(tof_data_list)):
for j in range(0, len(tof_data_list[0])):
data_change_matrix[i][j] = abs(tof_data_list[i][j] - tof_origin[i][j])
if itr_number != 0:
(change_freq_matrix[i][j]).append(data_change_matrix[i][j])
tof_origin = tof_data_list
time.sleep(freq)
# calcualte the average changes for each pixel
mean_change = []
for i in range(0, len(change_freq_matrix)):
for j in range(0, len(change_freq_matrix[0])):
each_pixel_change = []
mean_change_one_pixel = sum(change_freq_matrix[i][j])/len(change_freq_matrix[i][j])
maximum_change = max(change_freq_matrix[i][j])
each_pixel_change.append(mean_change_one_pixel)
each_pixel_change.append(maximum_change)
each_pixel_change.append(i)
each_pixel_change.append(j)
mean_change.append(each_pixel_change)
# sort the mean_change matrix
sorted_mean_change = sorted(mean_change, key=lambda x: -x[0])
# Plot the bar chart
if bar_chart:
# plot the maximum_change and mean_change amoung these pixels
plot_max_list_x = []
plot_max_list_y = []
for d in sorted_mean_change:
plot_max_list_x.append(str(d[2])+str(d[3]))
plot_max_list_y.append(int(d[1])) # insert the max_changes
plt.bar(plot_max_list_x, plot_max_list_y)
# Add labels
plt.xlabel('Pixel locations')
plt.ylabel('Average changes')
plt.title('Average changes of ToF sensor {}'.format(tof_id))
# Save the chart
plt.savefig(bar_fn)
# Plot the grid
if grid:
# Create a new martix to store the max changes values
max_change_matrix = [[0 for j in range(20)] for i in range(25)]
for i in range(0, len(change_freq_matrix)):
for j in range(0, len(change_freq_matrix[0])):
max_change_matrix[i][j] = max(change_freq_matrix[i][j])
# Create a new figure and axis object
fig, ax = plt.subplots()
# Plot the matrix as an image
im = ax.imshow(max_change_matrix, cmap='coolwarm')
# Add a colorbar
cbar = ax.figure.colorbar(im, ax=ax)
# Loop over the matrix and add text annotations
for i in range(25):
for j in range(20):
text = ax.text(j, i, max_change_matrix[i][j],
ha="center", va="center",
fontsize = 5,
fontweight='bold' if max_change_matrix[i][j] >= threshold else 'normal',
color="black" if max_change_matrix[i][j] < threshold else "white")
# Set the axis labels
ax.set_xticks(range(20))
ax.set_yticks(range(25))
# Set the tick labels
ax.set_xticklabels(range(1, 26))
ax.set_yticklabels(range(1, 21))
# Set the axis labels
ax.set_xlabel("Column")
ax.set_ylabel("Row")
# Set the title
ax.set_title("ToF Matrix Visualization")
# Add the threshold value as text outside the matrix
ax.text(1.32, 1.0, "Threshold:\n {}".format(threshold),
transform=ax.transAxes,
fontsize=10,
ha='left',
va='center')
# Save the figure to a file
plt.savefig(grid_fn)
# Return the matrix of selected pixel
selected_pixel = np.zeros((25,20))
for i in range(0, len(max_change_matrix)):
for j in range(0, len(max_change_matrix[0])):
if max_change_matrix[i][j] < threshold:
selected_pixel[i, j] = True
else:
selected_pixel[i, j] = False
# Set pixels to False manually
for i in range(22, 25):
selected_pixel[i, 19] = False
selected_pixel[23, 18] = False
return selected_pixel
if __name__ == "__main__":
pixel_pick_mat = tof_pixel_check(0, bar_chart=False,loop_count=100)