File size: 1,537 Bytes
7ebf5dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
# -*- encoding: utf-8 -*-

"""
@File    :   event.py
@Time    :   2025/9/3 15:25:00
@Author  :   lh9171338
@Version :   1.0
@Contact :   2909171338@qq.com
"""

import numpy as np


class Event:
    """
    Event

    Args:
        filename (str | None): path to the event file
        events (dict | None): event data
    """
    def __init__(self, filename=None, events=None, **kwargs):
        assert filename is not None or events is not None
        if filename is not None:
            events = np.load(filename)
            events = dict(**events)
        events["x"] = np.array(events["x"], dtype="int16")
        events["y"] = np.array(events["y"], dtype="int16")
        events["p"] = np.array(events["p"], dtype="int16")
        events["t"] = np.array(events["t"], dtype="int32")

        self.filename = filename
        self.events = events

    def event2image(self):
        """
        Convert event to image

        Args:
            None

        Returns:
            image (numpy.ndarray): event image
        """
        width, height = self.events["image_size"]
        xs = self.events["x"]
        ys = self.events["y"]
        ps = self.events["p"]

        event = np.zeros((height, width, 2), dtype="float32")
        np.add.at(event, (ys, xs, ps), 1)
        np.add.at(event, (ys, xs, ps), 1)
        neg = event[:, :, 0]
        pos = event[:, :, 1]
        image = np.stack((neg, np.zeros_like(neg), pos), axis=-1)
        image = (((image / image.max()) ** 0.5) * 255).astype(np.uint8)
        return image