File size: 5,515 Bytes
b004d6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
# import matlab.engine
from .SSP.ssp import get_ssp_depth

c = 3e8


class SinglePhotonImaging:
    def __init__(self, lr: int, lc: int, tp: float):
        """

        初始化深度估计器



        参数:

            lr: 图像行数

            lc: 图像列数

            tp: 时间bin持续时间 (秒)

        """
        self.lr = lr
        self.lc = lc
        self.tp = tp

    def _spad2tof(self, spad: np.ndarray) -> np.ndarray:
        """

        将稀疏SPAD数据转换为时间飞行(TOF)对象数组 [内部方法]



        参数:

            spad: 稀疏SPAD数据矩阵 [N, M]



        返回:

            np.ndarray: TOF对象数组 [lr, lc],每个元素为时间bin数组

        """
        rows, cols = spad.nonzero()
        values = spad.data
        tof = np.empty((self.lr, self.lc), dtype=np.ndarray)

        for i in range(self.lr * self.lc):
            indices = np.where(rows == i)[0]
            ph = []
            for j in indices:
                ph += [int(cols[j])] * int(values[j])

            ph = np.array([ph]).T

            x = i % self.lc
            y = i // self.lc

            tof[x, y] = ph

        return tof

    def _time_to_depth(self, time_data: np.ndarray, time_bin: float) -> np.ndarray:
        """统一时间到深度转换"""
        return time_data * c / 2 * time_bin

    def max_hist(self, spads: np.ndarray) -> np.ndarray:
        """

        通过滑动窗口均值法获取深度图



        参数:

            spads: 稀疏SPAD数据矩阵 [N, T]



        返回:

            np.ndarray: 深度图矩阵 [lc, lr] (单位:米)

        """
        print("Max Hist Processing...")

        spads_dense = spads.todense()
        max_histogram_mean = np.empty(self.lr * self.lc, dtype=float)

        for i in range(self.lr * self.lc):
            data = np.ascontiguousarray(spads_dense[i, :]).ravel()
            max_index = np.argmax(data)
            max_histogram_mean[i] = max_index

        depth = self._time_to_depth(
            max_histogram_mean.reshape(self.lr, self.lc).T, self.tp
        )

        return depth

    # def shin(self, spads: np.ndarray) -> np.ndarray:
    #     """
    #     使用Shin算法估计深度图

    #     参数:
    #         spads: 稀疏SPAD数据矩阵 [N, T]

    #     返回:
    #         np.ndarray: 深度图矩阵 [lc, lr] (单位:米)
    #     """
    #     print("Shin Processing...")

    #     tofs = self._spad2tof(spads)
    #     eng = matlab.engine.start_matlab()
    #     eng.addpath(r"matlab\fcns_Shin")
    #     matlab_tofs = eng.eval("cell(1, 3136)", nargout=1)
    #     matlab_tofs = [tof for row in tofs for tof in row]

    #     tof_shin = np.array(eng.cal_Shin(matlab_tofs))
    #     eng.quit()

    #     depth = self._time_to_depth(tof_shin, self.tp)
    #     return depth

    # def rapp(
    #     self, spads: np.ndarray, signal_per_pixel: float, frame_num: int
    # ) -> np.ndarray:
    #     """
    #     使用Rapp算法估计深度图

    #     参数:
    #         spads: 稀疏SPAD数据矩阵 [N, T]
    #         signal_per_pixel: 每像素平均信号光子数
    #         frame_num: 总帧数

    #     返回:
    #         np.ndarray: 深度图矩阵 [lc, lr] (单位:米)
    #     """
    #     print("Rapp Processing...")

    #     binnum = spads.shape[1]
    #     tofs = self._spad2tof(spads)

    #     sbr = 0.2
    #     perframenum = 50.0
    #     numFrames = perframenum * frame_num
    #     eng = matlab.engine.start_matlab()
    #     eng.addpath(r"matlab\fcns_Rapp")

    #     matlab_tofs = eng.eval("cell(1, 3136)", nargout=1)
    #     matlab_tofs = [tof for row in tofs for tof in row]

    #     tof_rapp = np.array(
    #         eng.cal_Rapp_py(
    #             matlab_tofs,
    #             signal_per_pixel,
    #             numFrames,
    #             sbr,
    #             float(self.tp),
    #             float(binnum),
    #         )
    #     )

    #     eng.quit()

    #     depth = self._time_to_depth(tof_rapp, self.tp)

    #     return depth

    # def li(self, spads: np.ndarray) -> np.ndarray:
    #     """
    #     使用Li算法估计深度图

    #     参数:
    #         spads: 稀疏SPAD数据矩阵 [N, T]

    #     返回:
    #         np.ndarray: 深度图矩阵 [lc, lr] (单位:米)
    #     """
    #     print("Li Processing...")
    #     eng = matlab.engine.start_matlab()
    #     eng.addpath(r"matlab\fcns_Li", nargout=0)

    #     # 直接使用原始SPAD数据
    #     spads = spads.todense()
    #     tof_li = np.array(
    #         eng.cal_Li_py(
    #             spads,
    #             float(self.tp),
    #             float(spads.shape[1]),
    #             nargout=1,
    #         )
    #     )
    #     eng.quit()

    #     depth = self._time_to_depth(tof_li, self.tp)

    #     return depth

    def ssp(self, spads: np.ndarray) -> np.ndarray:
        """

        使用SSP算法估计深度图



        参数:

            spads: 稀疏SPAD数据矩阵 [N, T]



        返回:

            np.ndarray: 深度图矩阵 [lc, lr] (单位:米)

        """
        # print("SSP Processing...")
        tp = self.tp
        tr = spads.shape[1]
        depth = get_ssp_depth(spads, tr, tp, self.lr, self.lc)

        return depth