gpt优化事件处理速度

1、优化时间戳累加时间

python 复制代码
# 原代码
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图

python 复制代码
# 原代码
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.]]
'''
相关推荐
每天吃的很好的Ruby37 分钟前
报错ValueError: sampler option is mutually exclusive with shuffle
人工智能·pytorch·python
清水白石00838 分钟前
Python 性能优化全景解析:当 Big O 骗了你——深挖常数开销、内存与解释器黑盒
开发语言·python·性能优化
oi..41 分钟前
python Get/Post请求练习
开发语言·经验分享·笔记·python·程序人生·安全·网络安全
速易达网络1 小时前
python地图商城可视化系统
python
@fai1 小时前
PyQt6 Graphic进阶实战:打造一个视觉恒定的可缩放矩形框
python·pyqt
luanma1509801 小时前
PHP vs C#:30字秒懂两大语言核心差异
android·开发语言·python·php·laravel
Channing Lewis1 小时前
Python 全局变量调用了一个函数,如何实现每次使用时都运行一次函数获取最新的结果
开发语言·python
浅墨cgz1 小时前
查找并删除源目录中与目标目录重复的文件
python
云姜.1 小时前
YAML简单使用
python
喵手1 小时前
Python爬虫实战:手把手教你Python 自动化构建志愿服务岗位结构化数据库!
爬虫·python·自动化·数据采集·爬虫实战·零基础python爬虫教学·志愿服务岗位结构数据库打造