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