gpt优化事件处理速度
1、优化时间戳累加时间
# 原代码
for j in range(evs_duration_num):
img_conf[int(y_array[j]), int(x_array[j])] += int(time_array[j])
# 现代码
import numpy as np
y_array = [1, 2, 2, 4, 3]
x_array = [1, 2, 2, 4, 3]
time_array = [10, 11, 12, 13, 14]
img_conf = np.zeros(shape=(5,5), dtype=np.float64)
print(img_conf)
xy_column_stack = np.column_stack((y_array, x_array))
print(f"xy_column_stack: \n{xy_column_stack}")
unique_indices, inverse_indices = np.unique(xy_column_stack, axis=0, return_inverse=True)
print(f"unique_indices: \n{unique_indices}")
print(f"inverse_indices: \n{inverse_indices}")
time_array_sum = np.bincount(inverse_indices, weights=time_array).astype(float)
print(f"time_array_sum: \n{time_array_sum}")
img_conf[unique_indices[:, 0], unique_indices[:, 1]] = time_array_sum
print(img_conf)
2、 生成map图
# 原代码
for polarity, xx, yy in zip(p_list, x_list, y_list):
yy, xx = int(yy), int(xx)
# 正事件为红色,负事件为蓝色,numpy:BGR
if polarity == 1:
map[yy][xx][0] = 0
map[yy][xx][1] = 0
map[yy][xx][2] = 255
elif polarity == 0:
map[yy][xx][0] = 255
map[yy][xx][1] = 0
map[yy][xx][2] = 0
else:
raise BaseException(f"极性错误!({xx},{yy}) {polarity} {save_map_path}")
# 现代码
# 创建一个全白色 (RGB: 255, 255, 255) 的图像,数据类型为uint8
map = np.ones((height, width, 3), dtype=np.uint8) * 255
# 将坐标和极性转换为numpy数组
x_list = np.array(x_list, dtype=int)
y_list = np.array(y_list, dtype=int)
p_list = np.array(p_list, dtype=int)
# 使用向量化操作设置颜色
# 极性为1的坐标设置为红色
red_mask = p_list == 1
map[y_list[red_mask], x_list[red_mask], 0] = 0
map[y_list[red_mask], x_list[red_mask], 1] = 0
map[y_list[red_mask], x_list[red_mask], 2] = 255
# 极性为0的坐标设置为蓝色
blue_mask = p_list == 0
map[y_list[blue_mask], x_list[blue_mask], 0] = 255
map[y_list[blue_mask], x_list[blue_mask], 1] = 0
map[y_list[blue_mask], x_list[blue_mask], 2] = 0
# 保存图像
cv2.imwrite(str(save_map_path), map)
'''
[[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]
[0. 0. 0. 0. 0.]]
xy_column_stack:
[[1 1]
[2 2]
[2 2]
[4 4]
[3 3]]
unique_indices:
[[1 1]
[2 2]
[3 3]
[4 4]]
inverse_indices:
[0 1 1 3 2]
time_array_sum:
[10. 23. 14. 13.]
[[ 0. 0. 0. 0. 0.]
[ 0. 10. 0. 0. 0.]
[ 0. 0. 23. 0. 0.]
[ 0. 0. 0. 14. 0.]
[ 0. 0. 0. 0. 13.]]
'''